顺序队列和链队列的各种操作

一. 先说顺序队列

  1. /*********************************************************** 
  2. 顺序队列的各种操作13-4-1.cpp 
  3. 1.初始化队列 
  4. 2.判断队空与否 
  5. 3.取队头元素 
  6. 4.入队 
  7. 5.出队 
  8. 6.主函数测试以上功能 
  9. ***********************************************************/  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12.   
  13. #define MAX 1000  
  14. typedef struct  
  15. {  
  16.     int data[MAX];  
  17.     int front;  
  18.     int rear;  
  19. }SeQueue;  
  20.   
  21. void InitQueue(SeQueue *SQ)  
  22. {  
  23.     SQ->front=SQ->rear=0;  
  24. }  
  25.   
  26. int EmptyQueue(SeQueue *SQ)  
  27. {  
  28.     if(SQ->front==SQ->rear)  
  29.     {  
  30.         printf("\n队空!\n");  
  31.         return 1;  
  32.     }  
  33.     return 0;  
  34. }  
  35.   
  36. int GetFront(SeQueue *SQ, int *x)  
  37. {  
  38.     if(EmptyQueue(SQ))  
  39.     {  
  40.         printf("队空!\n");  
  41.         return 0;  
  42.     }  
  43.     *x=SQ->data[(SQ->front+1)%MAX];  
  44.     return 1;  
  45. }  
  46.   
  47. int EnQueue(SeQueue *SQ, int x)  
  48. {  
  49.     if(SQ->front==(SQ->rear+1)%MAX)  
  50.     {  
  51.         printf("队满!\n");  
  52.         return 0;  
  53.     }  
  54.     SQ->rear=(SQ->rear+1)%MAX;  
  55.     SQ->data[SQ->rear]=x;  
  56.     return 1;  
  57. }  
  58.   
  59. int OutQueue(SeQueue *SQ, int *x)  
  60. {  
  61.     if(EmptyQueue(SQ))  
  62.     {  
  63.         printf("队空!\n");  
  64.         return 0;  
  65.     }  
  66.     SQ->front=(SQ->front+1)%MAX;  
  67.     *x=SQ->data[SQ->front];  
  68.     return 1;  
  69. }  
  70.   
  71. int main()  
  72. {  
  73.     SeQueue *Q;  
  74.     Q=(SeQueue *)malloc(sizeof(SeQueue));  
  75.     int x, fx;  
  76.     int n;  
  77.     int j;  
  78.     InitQueue(Q);  
  79.     printf("输入入队元素个数n:\n");  
  80.     scanf("%d", &n);  
  81.     for(int i=1; i<=n; i++)  
  82.     {  
  83.         EnQueue(Q, i);  
  84.     }  
  85.     GetFront(Q, &fx);  
  86.     printf("队头元素:%d\n", fx);  
  87.     printf("输出队列元素:\n");  
  88.     for(j=Q->front+1;j<=Q->rear;j=(j+1)%MAX)  
  89.     {  
  90.         printf("%3d",Q->data[j]);      
  91.     }  
  92.   
  93.     for(int i=1; i<=n; i++)  
  94.     {  
  95.         OutQueue(Q, &x);  
  96.         printf("\n出队元素:%d\n", x);  
  97.     }  
  98.     system("pause");  
  99.     return 0;  
  100. }  

二.再说链队列

  1. /*********************************************************** 
  2. 链队列的各种操作13-4-2.cpp 
  3. 1.初始化队列 
  4. 2.判断队空与否 
  5. 3.取队头元素 
  6. 4.入队 
  7. 5.出队 
  8. 6.主函数测试以上功能 
  9. ***********************************************************/  
  10. #include<stdio.h>  
  11. #include<stdlib.h>  
  12.   
  13. typedef struct Lnode  
  14. {  
  15.     int data;  
  16.     struct Lnode *next;  
  17. }LinkList;  
  18.   
  19. typedef struct  
  20. {  
  21.     LinkList *front;  
  22.     LinkList *rear;  
  23. }LinkQueue;  
  24.   
  25. int InQueue(LinkQueue *LQ)  
  26. {  
  27.     LinkList *p=(LinkList*)malloc(sizeof(LinkList));  
  28.     if(p==NULL)  
  29.     {  
  30.         printf("初始化失败!\n");  
  31.         return 0;  
  32.     }  
  33.     p->next=NULL;  
  34.     LQ->front=LQ->rear=p;  
  35.     return 1;  
  36. }  
  37.   
  38. int EmptyQueue(LinkQueue *LQ)  
  39. {  
  40.     if(LQ->front==LQ->rear)  
  41.     {  
  42.         printf("队列空!\n");  
  43.         return 1;  
  44.     }  
  45.     return 0;  
  46. }  
  47.   
  48. int EnQueue(LinkQueue *LQ, int x)  
  49. {  
  50.     LinkList *s=(LinkList *)malloc(sizeof(LinkList));  
  51.     if(s==NULL)  
  52.     {  
  53.         printf("分配空间失败!\n");  
  54.         return 0;  
  55.     }  
  56.     s->data=x;  
  57.     s->next=NULL;  
  58.     LQ->rear->next=s;  
  59.     LQ->rear=s;  
  60.     return 1;  
  61. }  
  62.   
  63. int GetFront(LinkQueue *LQ, int *x)  
  64. {  
  65.     if(EmptyQueue(LQ))  
  66.     {  
  67.         printf("队空!\n");  
  68.         return 0;  
  69.     }  
  70.     *x=LQ->front->next->data;  
  71.     return 1;  
  72. }  
  73.   
  74. int OutQueue(LinkQueue *LQ, int *x)  
  75. {  
  76.     LinkList *p;  
  77.     if(EmptyQueue(LQ))  
  78.     {  
  79.         printf("队空!\n");  
  80.         return 0;  
  81.     }  
  82.     p=LQ->front->next;  
  83.     *x=p->data;  
  84.     LQ->front->next=p->next;  
  85.     if(LQ->front->next==NULL)  
  86.     {  
  87.         LQ->rear=LQ->front;  
  88.     }  
  89.     free(p);  
  90.     return 1;  
  91. }  
  92.   
  93. int main()  
  94. {  
  95.     LinkQueue *Q;  
  96.     Q=(LinkQueue *)malloc(sizeof(LinkQueue));  
  97.     LinkList *p;  
  98.     int n, x, fx;  
  99.     InQueue(Q);  
  100.     printf("请输入入队元素个数n:\n");  
  101.     scanf("%d",&n);  
  102.     for(int i=1; i<=n; i++)  
  103.     {  
  104.         EnQueue(Q, i);  
  105.     }  
  106.   
  107.     GetFront(Q, &fx);  
  108.     printf("队首元素:%d\n", fx);  
  109.   
  110.     printf("输出队列元素:\n");  
  111.     for(p=Q->front->next; p!=NULL; p=p->next)  
  112.     {  
  113.         printf("%3d", p->data);  
  114.     }  
  115.     for(int i=1; i<=n; i++)  
  116.     {  
  117.         OutQueue(Q, &x);  
  118.         printf("\n出队元素:%d\n", x);  
  119.     }  
  120.   
  121.     system("pause");  
  122.     return 0;  
  123. }  
  124. 注意://1)链队列入队的死或坏,这个跟顺序队列入队就不一样了,顺序队列入队要先判断队满与否,因为数组有边界,但是链队列是用链表存储的,没有边界,所以不用判断队列满  
  125. //2)与顺序队列一样,出队要判断队空与否。)出队和GetFront非常像。  
  126. //3)出队的时候,front一直没有变,只不过是把front的Next给做掉了。  
  127.     //4)这个链队列出队要注意如果只剩下一个节点情况(删除完之后LQ->front->next==NULL),这是LQ->rear是游离的,一定要将LQ->front赋给rear  
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值