C语言 单链队列操作

  1.  
  2. #include <stdio.h>  
  3.   
  4. #define OK 1  
  5. #define ERROR 0  
  6. #define TRUE 1  
  7. #define FALSE 0  
  8.   
  9. typedef int Status;  
  10. typedef char QElemType;  
  11.   
  12. typedef struct QNode{//队列元素结构  
  13.     QElemType data;  
  14.     struct QNode* next;  
  15. }QNode,*QueuePtr;  
  16.   
  17. typedef struct{//链队结构  
  18.     QueuePtr front;//队首指针  
  19.     QueuePtr rear;//队尾指针  
  20. }LinkQueue;  
  21.   
  22. Status InitQueue(LinkQueue *Q);  
  23.     //构造一个空队列Q  
  24. Status DestroyQueue(LinkQueue *Q);  
  25.     //销毁队列Q,Q不再存在  
  26. Status ClearQueue(LinkQueue *Q);  
  27.     //将Q清为空队列  
  28. Status QueueEmpty(LinkQueue Q);  
  29.     //若队列Q为空队列,则返回TRUE,否则返回FALSE  
  30. int QueueLength(LinkQueue Q);  
  31.     //返回队列Q元素个数,即队列长度  
  32. Status GetHead(LinkQueue Q,QElemType *e);  
  33.     //若队列不为空,则用e返回Q的队首元素,并返回OK;否则返回ERROR  
  34. Status EnQueue(LinkQueue *Q,QElemType e);  
  35.     //插入元素e为Q的新的队尾元素  
  36. Status DeQueue(LinkQueue *Q,QElemType *e);  
  37.     //若队列不为空,则删除Q的队首元素,用e返回,并返回OK;否则返回ERROR  
  38. Status QueueTraverse(LinkQueue Q);  
  39.     //从队首至队尾遍历队列Q中的元素  
  40.   
  41. void PrintMenu();  
  42.     //显示菜单提示  
  43. char getOption();  
  44.     //获取用户输入  
  45. void NewNodeEnQueue(LinkQueue *Q);  
  46.     //根据用户输入,将新元素入队  
  47. void DeleteNode(LinkQueue *Q);  
  48.     //元素出队,并显示剩余队中元素  
  49. void ShowHeadNode(LinkQueue *Q);  
  50.     //显示队首元素  
  51. void ShowBye();  
  52.     //显示结束  
  53. void ShowLength(LinkQueue *Q);  
  54.     //显示队列元素个数  
  55. //=============main()================//  
  56. int main()  
  57. {  
  58.     char option;  
  59.     LinkQueue queue;  
  60.     InitQueue(&queue);  
  61.     PrintMenu();  
  62.     option=getOption();  
  63.     while(option!='Q'){  
  64.         switch(option){  
  65.             case 'E'://元素入队  
  66.                 NewNodeEnQueue(&queue);  
  67.                 break;  
  68.             case 'D'://元素出队  
  69.                 DeleteNode(&queue);  
  70.                 break;  
  71.             case 'S'://遍历整个队列  
  72.                 QueueTraverse(queue);  
  73.                 break;  
  74.             case 'H':  
  75.                 ShowHeadNode(&queue);  
  76.                 break;  
  77.             case 'L':  
  78.                 ShowLength(&queue);  
  79.                 break;  
  80.             case 'A':  
  81.                 printf("Invalid input.\n");  
  82.             default:  
  83.                 break;  
  84.         }  
  85.         option=getOption();  
  86.     }  
  87.     ShowBye();  
  88.     return 0;  
  89. }  
  90.   
  91.   
  92. //-------------交互处理-----------------//  
  93. void PrintMenu(){  
  94.     //显示菜单提示  
  95.     printf("------Welcome------\n");  
  96.     printf(" Please input your choice:\n");  
  97.     printf(" E: New element enqueue.\n");  
  98.     printf(" D: Delete an element from the Queue.\n");  
  99.     printf(" S: Show all elements in the Queue.\n");  
  100.     printf(" H: Get the first element in the Queue.\n");  
  101.     printf(" L: Get the length of the Queue.\n");  
  102.     printf(" Q: Quit.\n");  
  103.     printf(" ohters: do nothing.\n");  
  104. }  
  105.   
  106. void ShowBye(){  
  107.     //显示结束  
  108.     printf("------Bye bye------\n");  
  109. }  
  110.   
  111. char getOption(){  
  112.     //获取用户输入  
  113.     char *input,*op;  
  114.     input=(char *)malloc(sizeof(char)*256);  
  115.     printf(">>");  
  116.     gets(input);  
  117.     op=input;  
  118.     if(*(op+1)!='\0'){  
  119.         return 'A';  
  120.     }  
  121.     else  
  122.         return toupper(*op);  
  123. }  
  124.   
  125. void NewNodeEnQueue(LinkQueue *Q){  
  126.     //根据用户输入,将新元素入队  
  127.     char *p;  
  128.     p=(char *)malloc(sizeof(char)*256);  
  129.     printf("Please input a char(only one):");  
  130.     gets(p);  
  131.     if((*p>='a')&&(*p<='z')||(*p>='A')&&(*p<='Z')){  
  132.         EnQueue(Q,*p);  
  133.         printf("New element \'%c\' enqueu.\n",*p);  
  134.     }else  
  135.         printf("Sorry, your input is not accepted.\n");  
  136. }  
  137.   
  138. void DeleteNode(LinkQueue *Q){  
  139.     //元素出队,并显示剩余队中元素  
  140.     QElemType e;  
  141.     if(TRUE==QueueEmpty(*Q)){  
  142.         printf("The Queue is empty.\n");  
  143.         return;  
  144.     }  
  145.     DeQueue(Q,&e);  
  146.     printf("%c delete from the Queue,",e);  
  147.     if(QueueEmpty(*Q)==TRUE)  
  148.         printf("The Queue is empty now.\n");  
  149.     else  
  150.         //printf("Current Queue is:");  
  151.     QueueTraverse(*Q);  
  152.   
  153. }  
  154.   
  155. void ShowHeadNode(LinkQueue *Q){  
  156.     //显示队首元素  
  157.     QElemType e;  
  158.     if(QueueEmpty(*Q)){  
  159.         printf("The Queue is empty.\n");  
  160.         return;  
  161.     }else{  
  162.         GetHead(*Q,&e);  
  163.         printf("The first element of the Queue is:%c\n",e);  
  164.     }  
  165. }  
  166.   
  167. void ShowLength(LinkQueue *Q){  
  168.     //显示队列元素个数  
  169.     if(QueueEmpty(*Q)==TRUE){  
  170.         printf("The count of element is 0.\n");  
  171.     }else{  
  172.         printf("The count of element is:%d\n",QueueLength(*Q));  
  173.     }  
  174. }  
  175.   
  176. //-------------队操作实现----------------//  
  177. Status InitQueue(LinkQueue *Q){  
  178.     //构造一个空队列Q  
  179.     Q->front=Q->rear=(QueuePtr)malloc(sizeof(QNode));  
  180.     if(Q->front==NULL){  
  181.         printf("Apply for memory failed.\n");  
  182.         exit(0);  
  183.     }  
  184.     Q->rear->next=NULL;  
  185.     return OK;  
  186. }  
  187.   
  188. Status DestroyQueue(LinkQueue *Q){  
  189.     //销毁队列Q,Q不再存在  
  190.     while(Q->front){  
  191.         Q->rear=Q->front->next;  
  192.         free(Q->front);  
  193.         Q->front=Q->rear;  
  194.     }  
  195.     Q->front=Q->rear=NULL;  
  196.     return OK;  
  197. }  
  198.   
  199. Status ClearQueue(LinkQueue *Q){  
  200.     //将Q清为空队列  
  201.     QueuePtr p;  //临时指针  
  202.     Q->rear=Q->front->next;  
  203.     while(Q->rear){  
  204.         p=Q->rear->next;//指向下一个待释放的元素  
  205.         free(Q->rear);  
  206.         Q->rear=p;  
  207.     }  
  208.     Q->rear=Q->front;//修改队尾指针  
  209.     return OK;  
  210. }  
  211.   
  212. Status QueueEmpty(LinkQueue Q){  
  213.     //若队列Q为空队列,则返回TRUE,否则返回FALSE  
  214.     if(Q.front==Q.rear&&Q.front!=NULL)  
  215.         return TRUE;  
  216.     else  
  217.         return FALSE;  
  218.   
  219. }  
  220.   
  221. int QueueLength(LinkQueue Q){  
  222.     //返回队列Q元素个数,即队列长度  
  223.     QueuePtr p;//临时指针  
  224.     int count=0;//计算器  
  225.     p=Q.front->next;  
  226.     while(p!=NULL){  
  227.         p=p->next;  
  228.         count++;  
  229.     }  
  230.     return count;  
  231. }  
  232.   
  233. Status GetHead(LinkQueue Q,QElemType *e){  
  234.     //若队列不为空,则用e返回Q的队首元素,并返回OK;否则返回ERROR  
  235.     if(QueueEmpty(Q)==TRUE)//队列为空  
  236.         return ERROR;  
  237.     *e=Q.front->next->data;  
  238.     return OK;  
  239. }  
  240.   
  241. Status EnQueue(LinkQueue *Q,QElemType e){  
  242.     //插入元素e为Q的新的队尾元素  
  243.     QueuePtr p;  
  244.     if(Q->front==NULL||Q->rear==NULL)  
  245.         return ERROR;  
  246.     p=(QueuePtr)malloc(sizeof(QNode));  
  247.     if(!p){  
  248.         printf("Apply for memory error, element enqueue failed.\n");  
  249.         exit(0);  
  250.     }  
  251.     p->data=e;    p->next=NULL;  
  252.     Q->rear->next=p;  
  253.     Q->rear=p;  
  254.     return OK;  
  255. }  
  256.   
  257. Status DeQueue(LinkQueue *Q,QElemType *e){  
  258.     //若队列不为空,则删除Q的队首元素,用e返回,并返回OK;否则返回ERROR  
  259.     QueuePtr p;  
  260.     if(Q->front==Q->rear)//对为空  
  261.         return ERROR;  
  262.     p=Q->front->next;  
  263.     *e=p->data;  
  264.     Q->front->next=p->next;  
  265.     if(Q->rear==p)//队中只有一个元素  
  266.         Q->rear=Q->front;  
  267.     free(p);  
  268.     return OK;  
  269.   
  270. }  
  271.   
  272. Status QueueTraverse(LinkQueue Q){  
  273.     //从队首至队尾遍历队列Q中的元素  
  274.     QueuePtr p;  
  275.     p=Q.front->next;  
  276.     if(p==NULL){  
  277.         return ERROR;  
  278.     }  
  279.     printf("Elements of the Queue:");  
  280.     while(p!=NULL){//遍历队  
  281.         printf("%c ",p->data);  
  282.         p=p->next;  
  283.     }  
  284.     printf("\n");  
  285.     return OK;  
  286. }  
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值