版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/z861269429/article/details/51773775
数组栈
完成Stack CreateStack(int MaxElements)函数,该函数创建一个栈,MaxElements为与分配的栈空间大小(栈可用空间为Array[0,…MaxElements-1])。
typedef int ElemType;
struct StackRecord;
typedef struct StackRecord *Stack;
struct StackRecord
{
int Capacity; //栈容量
int Top; //栈顶,初始为1
ElemType *Array;
};
Stack CreateStack(int MaxElements)
{
Stack s=(StackRecord *)malloc(sizeof(StackRecord));
s‐>Top=‐1;
s‐>Capacity=MaxElements;
s‐>Array=(ElemType *)malloc(MaxElements*sizeof(ElemType));
return s;
}
链栈
完成Stack CreateStack(void)函数,该函数创建一个栈(空栈,带头结点),并返回栈指针。
typedef int ElemType;
struct Node;
typedef struct Node * PtrToNode;
typedef PtrToNode Stack;
struct Node
{
ElemType data;
PtrToNode next;
};
Stack CreateStack(void)
{
Stack top;
Node *s;
top=NULL;
s=(Node *)malloc(sizeof(Node));
s‐>next=top;
top=s;
return top;
}
数组队列
完成Queue CreateQueue(int MaxElements)函数,该函数创建一个基于数组的队列,并返回队列指针,其中MaxElements为预分配的数组单元数。
typedef int ElemType;
struct QueueRecord;
typedef struct QueueRecord * Queue;
struct QueueRecord
{
int Capacity; //队列总容量
int Front; //队首 初始值为0
int Rear; //队尾,初始值为1
int Size; //队列中数据数,初始值为0
ElemType *Array;
};
Queue CreateQueue(int MaxElements)
{
Queue s=(QueueRecord *)malloc(sizeof(QueueRecord));
s‐>Front=0;
s‐>Rear=‐1;
s‐>Capacity=MaxElements;
s‐>Array=(ElemType *)malloc(MaxElements*sizeof(ElemType));
return s;
}
链队列
完成Queue CreateQueue(void)函数,该函数创建一个基于链表的队列,并返回队列指针,创建的链表队列不带头节点。
typedef int ElemType;
struct node;
typedef struct node Node;
struct queue;
typedef struct queue * Queue;
struct node
{
ElemType data;
Node * next;
};
struct queue
{
Node * front; //队首
Node * rear; //队尾
int size; //队列中数据数
};
Queue CreateQueue(void)
{
Queue q;
q=(queue *)malloc(sizeof(queue));
q‐>front=NULL;
q‐>rear=NULL;
q‐>size=0;
return q;
}