/* 实验4:输入一组整型元素序列,建立一个链队,实现该链队的入队和出队操作。 */ #include<stdio.h> #include<stdlib.h> #define OK 1; #define ERROR 0; typedef int Status; typedef int QElemType; typedef struct QNode{ QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct { QueuePtr front; QueuePtr rear; }LinkQueue; QueuePtr p; //初始化链栈 Status InitQueue(LinkQueue &Q){ Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode)); if(!Q.front) exit(0); Q.front->next = NULL; return OK; } //删除链栈 Status DestroyQueue(LinkQueue &Q){ while(Q.front){ Q.rear = Q.front->next; free(Q.front); Q.front = Q.rear; } return OK; } //进链栈 Status EnQueue(LinkQueue &Q,QElemType e){ p = (QueuePtr)malloc(sizeof(QNode)); if(!p) exit(0); p->data = e; p->next = NULL; Q.rear -> next = p; Q.rear = p; return OK; } //出链栈 Status DeQueue(LinkQueue &Q,QElemType &e){ if(Q.front == Q.rear) return ERROR; p = Q.front ->next; e = p->data; Q.front ->next = p->next; if(Q.rear == p) Q.rear = Q.front; free(p); return OK; } void main(){ LinkQueue q; printf("初始化链队\n"); int i,x; InitQueue(q); printf("初始化链队成功!\n"); printf("请输入入链队的数据:\n"); for(i=1;i<=3;i++) { scanf("%d",&x); EnQueue(q,x); } printf("入链队成功!"); printf("以下为出栈数据:\n"); for(i=1;i<=3;i++) { DeQueue(q,x); printf("%4d",x); } printf("\n"); printf("end!\n"); }
By Mr.Z
原文链接:https://www.f2er.com/datastructure/383224.html