栈与队列--置空/销毁-栈/队列

来源:互联网 发布:软件开发培训班价格 编辑:程序博客网 时间:2024/04/30 06:59

数组栈
完成void MakeEmpty(Stack S)函数,该函数把栈置空(但不释放空间)。
完成Stack DisposeStack(Stack S)函数,该函数销毁数组栈(释放数组空间与栈空间),返回NULL指针。

typedef int ElemType;struct StackRecord;typedef struct StackRecord *Stack;struct StackRecord{    int Capacity; //栈容量    int Top; //栈顶,初始为1    ElemType *Array;};void MakeEmpty(Stack S){    S‐>Top=‐1;}Stack DisposeStack(Stack S){    S‐>Top=‐1;    free(S);    return NULL;}

链栈
完成void MakeEmpty(Stack S)函数,该函数把链栈置空(释放数据结点空间,但不释放头结点),已知S是带头结点的链栈。
完成Stack DisposeStack(Stack S)函数,该函数销毁链栈S,已知S是带头结点的链栈。

typedef int ElemType;struct Node;typedef struct Node * PtrToNode;typedef PtrToNode Stack;struct Node{    ElemType data;    PtrToNode next;};void MakeEmpty(Stack S){    Stack p,q;    p=S‐>next;    while(p)    {        q=p;        p=p‐>next;        free(q);    }    S‐>next=NULL;}Stack DisposeStack(Stack S){    Stack r;    while(S)    {        r=S‐>next;        free(S);        S=r;    }    return NULL;}

数组队列
完成void MakeEmpty(Queue Q)函数,该函数将队列Q置空(不释放空间),其中Q是基于数组的非循环队列。
完成Queue DisposeQueue(Queue Q)函数,该函数销毁队列Q(释放所有空间)并返回NULL指针,其中Q是基于数组的非循环队列。

typedef int ElemType;struct QueueRecord;typedef struct QueueRecord * Queue;struct QueueRecord{    int Capacity; //队列总容量    int Front; //队首 初始值为0    int Rear; //队尾,初始值为1    int Size; //队列中数据数,初始值为0    ElemType *Array;};void MakeEmpty(Queue Q){    Q‐>Front=0;    Q‐>Rear=‐1;    Q‐>Size=0;}Queue DisposeQueue(Queue Q){    free(Q);    return NULL;}

链队列
完成void MakeEmpty(Queue q)函数,该函数将队列q清空(释放队列中所有数据的空间,但不释放队列空间,即把队列清空为空队列),其中q是不带头节点的链表队列。
完成Queue DisposeQueue(Queue q)函数,该函数销毁队列q(释放所有空间),返回NULL指针,其中q是不带头节点的链表队列。

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; //队列中数据数void MakeEmpty(Queue q){    node *p=q‐>front;    node *t;    while(p!=q‐>rear)    {        t=p;        p=p‐>next;        free(t);    }     q‐>front=q‐>rear=NULL;    q‐>size=0;}Queue DisposeQueue(Queue q){    free(q);    return NULL;}
0 0