最近越来越感觉到c语言指针的强大~~
#include<stdio.h> #include<stdlib.h> #define QElemType int #define OK 1 #define ERROR 0 #define OVERFLOW 0 typedef int Status; //------------单链表------------队列的链式存储结构 typedef struct QNode { QElemType data; struct QNode *next; }QNode,*QueuePtr; typedef struct{ QueuePtr front,rear;//队头指针和队尾指针 }LinkQueue; //-----------------基本函数操作----------------- Status InitQueue(LinkQueue &Q,int n) { Q.front = Q.rear = (QueuePtr)malloc(sizeof(QNode));//构造一个空队列 if(!Q.front) exit(OVERFLOW); Q.front->next = NULL; printf("请输入n个数 "); for(int i = 1; i <= n; i ++)//读入n个数,并依次加入队列 { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); scanf("%d",&p->data); p->next = NULL; Q.rear->next = p; Q.rear = p; } return OK; } //销毁队列Q Status DestroyQueue(LinkQueue &Q) { while(Q.front) { Q.rear = Q.front->next ; free(Q.front); Q.front = Q.rear ; } return OK; } //插入元素e为Q 的新的队尾元素 Status EnQueue(LinkQueue &Q,QElemType &e) { QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); if(!p) exit(OVERFLOW);//存储分配失败 p->data = e; p->next = NULL; Q.rear->next = p; Q.rear = p; return OK; } //若队列不为空,则删除Q的队头元素,用e返回其值,并返回OK,否则返回ERROR Status DeQueue(LinkQueue &Q,QElemType &e) { if(Q.front == Q.rear ) return ERROR; QueuePtr p = (QueuePtr)malloc(sizeof(QNode)); p = Q.front->next ; e = p->data ; Q.front->next = p->next ; free(p); return OK; } void PrintQueue(LinkQueue *Q)//输出队列Q 中的元素 { QueuePtr p = Q->front->next ;//将队列的头元素指针地址赋值给p while(p!=NULL) { printf("%d ",p->data); p = p->next ; } printf(" "); } int main() { LinkQueue Q; int n; printf("请输入n "); scanf("%d",&n);//读入n个数,加入队列 InitQueue(Q,n);//初始化队列 printf("初始化后的队列为: "); PrintQueue(&Q);//输出队列中的元素 int e; printf("请输入一个整数e "); scanf("%d",&e);//读入e EnQueue(Q,e);//插入元素e为Q的新的队尾元素 printf("将e插入队尾后,队列更新为 "); PrintQueue(&Q);//输出插入队尾后队列的值 DeQueue(Q,e);//删除Q的队头元素,用e返回其值 printf("删除队头元素后的队列为 "); PrintQueue(&Q);//输出删除队头后的队列 DestroyQueue(Q);//销毁队列,释放存储空间 return 0; }
循环队列的实现
#include<stdio.h> #include<stdlib.h> typedef int QElemType; #define OVERFLOW 0 #define MAXSIZE 1000 typedef int Status; #define OK 1 #define ERROR 0 typedef struct { QElemType *base; int front; int rear; }SqQueue; Status InitQueue(SqQueue &Q) { int n; printf("请输入n "); scanf("%d",&n); Q.base = (QElemType*)malloc(MAXSIZE*sizeof(QElemType)); if(!Q.base) exit(OVERFLOW); Q.front = Q.rear = 0; printf("请输入n个元素 "); for(int i = 0; i < n; i ++) { scanf("%d",&Q.base[i]); Q.rear = (Q.rear +1)%MAXSIZE; } return OK; } int QueueLength(SqQueue Q) { return (Q.rear - Q.front +MAXSIZE)%MAXSIZE; } Status EnQueue(SqQueue &Q,QElemType e) { if((Q.rear +1)%MAXSIZE==Q.front ) return ERROR; Q.base[Q.rear +1] = e; Q.rear = (Q.rear + 1)%MAXSIZE; return OK; } Status DeQueue(SqQueue &Q,QElemType &e) { if(Q.front == Q.rear ) return ERROR; e = Q.base[Q.front]; Q.front = (Q.front + 1)%MAXSIZE; return OK; } void PrintQueue(SqQueue S) { SqQueue Q = S; while((Q.front+1)%MAXSIZE != Q.rear) { printf("%d ",Q.base[Q.front]); Q.front = (Q.front + 1)%MAXSIZE; } printf(" "); return ; } int main() { SqQueue Q; QElemType e; InitQueue(Q); printf("队列的长度为:%d ",QueueLength(Q)); printf("请输入插入元素的值e: "); scanf("%d",&e); EnQueue(Q, e); printf("插入元素以后的队列值为: "); PrintQueue(Q); DeQueue(Q,e); printf("删除队头元素后的队列值为: "); PrintQueue(Q); return 0; }