zoukankan      html  css  js  c++  java
  • c语言实现队列的基本操作

    话不多说,直接代码

      1 #include"stdio.h"
      2 #include"stdlib.h"
      3 typedef struct QNode{
      4     int date;
      5     struct QNode *next;
      6 }QNode,*QueuePtr;
      7 typedef struct{
      8     QueuePtr front;
      9     QueuePtr rear;
     10 }LinkQueue;
     11 //初始化
     12 int InitStack(LinkQueue &S){
     13     S.front=(QueuePtr)malloc(sizeof(QNode));
     14     //S.front=NULL;
     15     S.rear=S.front;
     16     if(!S.front)
     17         return 0;
     18     S.front->next=NULL;
     19     return 1;
     20 }
     21 //
     22 int EnQueue(LinkQueue &S,int e){
     23     QueuePtr p=NULL;
     24     p=(QueuePtr)malloc(sizeof(QNode));
     25     if(!p)exit(1);
     26     p->date=e;
     27     p->next=0;
     28     S.rear->next=p;
     29     S.rear=p;
     30     return 1;
     31 }
     32 //置空
     33 void ClearQueue(LinkQueue &S){
     34     QueuePtr p=NULL;
     35     p=S.front;
     36     while(S.front!=S.rear){
     37         p=S.front->next;
     38         free(S.front);
     39         S.front=p;
     40     }    
     41 }
     42 //判空
     43 void QueueEmpty(LinkQueue &S){
     44     if(S.front==S.rear)
     45         printf("判空:是
    ");
     46     else 
     47         printf("判空:否
    ");
     48     
     49 }
     50 //长度
     51 int   QueueLen(LinkQueue &S){
     52     QueuePtr p=NULL;
     53     int len=0;
     54     p=S.front;
     55     if(S.front==S.rear)
     56         return len;
     57     else{
     58         while(p!=S.rear){
     59             len++;
     60             p=p->next;
     61         }
     62         return len;
     63     }
     64 }
     65 void len(LinkQueue &S){
     66 printf("%d
    ",S.front);
     67 printf("%d
    ",S.front->next);
     68 printf("%d
    ",S.rear);
     69 printf("大小%d
    ",sizeof(QNode));
     70 printf("%d
    ",S.rear-S.front->next);
     71 printf("%d
    ",S.rear-S.front);
     72 printf("%d
    ",(S.rear-S.front->next)/sizeof(QNode));
     73 
     74 }
     75 //
     76 int pop(LinkQueue &S){
     77     int tem=0;
     78     QueuePtr p=NULL;
     79     if(S.front->next==NULL)
     80         return 0;
     81     else{
     82         tem=S.front->next->date;
     83         p=S.front->next;
     84         free(S.front);
     85         S.front=p;
     86         return tem;
     87     }
     88 }
     89 //输出
     90 void QueueTraverse(LinkQueue &S){
     91     printf("输出:");
     92     QueuePtr p=S.front;
     93     while(p!=S.rear){
     94         printf("%d   ",p->next->date);
     95         p=p->next;
     96     }
     97     
     98 }
     99 void main(){
    100     LinkQueue S;
    101     printf("初始化:");
    102     printf("%d
    ",InitStack(S));
    103     printf("%d
    ",EnQueue(S,1));
    104     printf("%d
    ",EnQueue(S,2));
    105     printf("%d
    ",EnQueue(S,3));
    106     printf("%d
    ",EnQueue(S,4));
    107     printf("%d
    ",EnQueue(S,5));
    108     printf("长度:%d
    ",QueueLen(S));
    109     len(S);
    110     QueueTraverse(S);
    111     printf("输出:%d
    ",pop(S));
    112     QueueTraverse(S);
    113     EnQueue(S,9);
    114     QueueTraverse(S);
    115     QueueEmpty(S);
    116     ClearQueue(S);
    117     QueueEmpty(S);
    118 }

    在使用构造体的时候注意应该有两个构造体,分别对应整个链表和链表的一个结点。

    在获取队列长度的时候不能像栈一样队首队尾直接相减,具体我也搞不懂为什么,代码中的len函数就是对该方法的测试。

    另外,一定在。要注意S.front并不是第一个元素的位置,S.front->next才是,见图

  • 相关阅读:
    Python之并发编程(三)生产者与消费者模型
    Python之并发编程(四)多线程(相关理论)
    cookiesession okenkey四大参数解析
    常见http返回的状态码
    for遍历用例数据时,报错:TypeError: list indices must be integers, not dict,'int' object is not iterable解决方法
    python中调用函数时,参数顺序与参数赋值问题
    自动化测试用例中的raise
    python --------简单的socket通话实现例子
    python---------------logging
    Monkey
  • 原文地址:https://www.cnblogs.com/tao7/p/9489299.html
Copyright © 2011-2022 走看看