-
Notifications
You must be signed in to change notification settings - Fork 0
/
List.c
105 lines (97 loc) · 2.06 KB
/
List.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include <stdio.h>
#include <string.h>
#include "UGL.H"
void DisplayList( LIST *L )
{
if (L == NULL)
printf("<Empty list>\n");
else
while (L != NULL)
{
PrintTok(L->Data);
printf((L = L->Next) == NULL ? "\n" : ", ");
}
}
void ClearList( LIST **L )
{
LIST *tmp;
while (*L != NULL)
{
tmp = *L;
*L = (*L)->Next;
free(tmp);
}
}
void Put( QUEUE *Q, TOK NewData )
{
LIST *NewElement = malloc(sizeof(LIST));
if (NewElement == NULL)
Error("No memory in put ");
NewElement->Data = NewData;
NewElement->Next = NULL;
if (Q->Head == NULL)
Q->Head = Q->Tail = NewElement;
else
Q->Tail->Next = NewElement, Q->Tail = NewElement;
}
int Get( QUEUE *Q, TOK *OldData )
{
LIST *NewElement = malloc(sizeof(LIST));
if (Q->Head == NULL)
return 0;
NewElement = Q->Head->Next;
*OldData = Q->Head->Data;
free(Q->Head);
Q->Head = NewElement;
return 1;
}
void Push( STACK *S, TOK NewData )
{
LIST *NewElement = malloc(sizeof(LIST));
if (NewElement == NULL)
Error("No memory");
NewElement->Data = NewData;
NewElement->Next = S->Top;
S->Top = NewElement;
}
int Pop (STACK *S, TOK *OldData)
{
LIST *NewElement = malloc(sizeof(LIST));
if (S->Top == NULL)
return 0;
NewElement = S->Top->Next;
*OldData = S->Top->Data;
S->Top = NewElement;
return 1;
}
void DisplayStack( STACK *S )
{
DisplayList(S->Top);
}
void DisplayQueue( QUEUE *Q )
{
DisplayList(Q->Head);
}
void ClearStack( STACK *S )
{
ClearList(&S->Top);
}
void ClearQueue( QUEUE *Q )
{
ClearList(&Q->Head);
}
void PrintTok( TOK T )
{
if (T.Id == TOK_OP)
printf("op: %c ", T.Op);
else if (T.Id == TOK_NUM)
printf("integer: %f ", T.Num);
else if (T.Id == TOK_SYMBOL)
printf("symbol: %c ", T.Op);
else if (T.Id == TOK_NAME)
printf("name: %s ", T.Name);
else if (T.Id == TOK_KEYW)
printf ("keyword: %s ", T.Name);
else
printf("UNKNOWN ");
}