1.队列
struct QueueRecord
{
int Capacity;
int Front;
int Rear;
int Size;
int *Array;
};
typedef struct QueueRecord *Queue;
int IsEmpty(Queue Q)
{
return Q->Size == 0;
}
void MakeEmpty(Queue Q)
{
Q->Size = 0;
Q->Front = 1;
Q->Rear = 0;
}
static int Succ(int value, Queue Q)//入队检查大小合法
{
if (++value == Q->Capacity)
{
value = 0;
}
return value;
}
void Enqueue(int X, Queue Q)
{
if (IsFull(Q))
{
//err
}
else
{
Q->Size++;
Q->Rear = Succ(Q->Rear, Q);
Q->Array[Q->Rear] = X;
}
}
2.栈
struct Node
{
int Element;
ptrToNode Next;
};
int IsEmpty(Stack S)
{
return S->Next == NULL;
}
Stack CreateStack(void)
{
Stack S;
S = (Stack)(malloc(sizeof(struct Node)));
if (S == NULL)
{
return NULL;
}
else
{
S->Next = NULL;
MakeEmpty(S);
return S;
}
}
void MakeEmpty(Stack S)
{
if (S == NULL)
{
//ERR
}
else
{
while (!IsEmpty(S))
{
Pop(S);
}
}
}
void Pop(Stack S)
{
ptrToNode firstCell;
if (IsEmpty(S))
{
//err
}
else
{
firstCell = S->Next;//删除顶端元素(链表第一个),pop出栈
S->Next = S->Next->Next;
free(firstCell);
}
}
void Push(int X, Stack S)
{
ptrToNode first;
if (S == NULL)
{
//err
}
else
{
ptrToNode tmp = (ptrToNode)(malloc(sizeof (struct Node)));
tmp->Element = X;
first = S->Next;
tmp->Next = first;
S->Next = tmp;
}
}
int Top(Stack S)
{
int tmp;
if (S->Next == NULL)
{
return -1;//err
}
else
{
return S->Next->Element;
}
}