单链队列[原创]

  1/*===========================单链队列=============================*/
  2#include <stdio.h>
  3#include <malloc.h>
  4
  5typedef struct qnode{/*队列中结点类型的定义*/
  6    char data;
  7    struct qnode *next;
  8}
qnode,*queptr;
  9
 10
 11typedef struct/*队列类型的定义*/
 12    queptr front;
 13    queptr rear;
 14}
linkque;
 15
 16
 17/*=====================常用的被调用函数定义====================*/
 18void print(linkque l){     /*-在屏幕上输出单链队列的所有元素--*/
 19    queptr t;
 20    t=l.front;
 21    if(t==l.rear){
 22        printf("\nThe queue is empty..\n");
 23        return 0;
 24    }

 25
 26    printf("The queue :");
 27    t=t->next;
 28    while(t){
 29        printf("%c-",t->data);
 30        t=t->next;
 31    }

 32    printf("\n");
 33}

 34
 35
 36/*================对队列进行操作的函数定义======================*/
 37int initque(linkque *q){/*构造一个空队列*/
 38    q->front=q->rear=(queptr)malloc(sizeof(struct qnode));
 39    if(!q->front) return 0;
 40    q->front->next=NULL;/**/
 41    q->rear->next=NULL;
 42}

 43
 44
 45int destroyque(linkque *q){/*销毁队列*/
 46    free(q->front);
 47    free(q->rear);
 48    free(q);
 49
 50}

 51
 52
 53int clearque(linkque *q){/*清空队列*/
 54    q->front=q->rear;
 55}

 56
 57
 58int queempty(linkque q){/*若队列为空,返回1*/
 59    if(q.front==q.rear){
 60        printf("the queue is empty");
 61        return 1;
 62    }

 63    else return 0;
 64}

 65
 66
 67int quelength(linkque q){/*返回队列中元素的个数*/
 68    int i=0;
 69    queptr p;
 70    p=q.front;
 71
 72    while(p->next!=NULL){
 73        p=p->next;
 74        i++;
 75    }

 76    return i;
 77}

 78
 79
 80char gethead(linkque q){/*返回对头元素*/
 81    if(queempty(q)) return 0;
 82    return q.front->next->data;
 83}

 84
 85
 86int entque(linkque *q,char e)/*_入队函数(1)_*/
 87    queptr p;
 88    p=(queptr)malloc(sizeof(struct qnode));
 89    if(!p) exit(0);
 90    p->data=e;
 91    p->next=NULL;
 92    q->rear->next=p;
 93    q->rear=p;
 94    return 1;
 95}

 96
 97
 98int enque(linkque *q,char e){/*_入队函数(2)_*/
 99    q->rear->next=(queptr)malloc(sizeof(struct qnode));
100    q->rear->next->data=e;
101    q->rear->next->next=NULL;
102    q->rear=q->rear->next;
103    return 1;
104}

105
106
107char delque(linkque *q){/*删除对头元素*/
108    char e;
109    if(!q->front->next){
110        printf("\nThe queue is empty,can not delete..");
111        return 0;
112    }

113    e=q->front->next->data;
114    q->front=q->front->next;
115    return e;
116}

117
118/*==================主函数部分================*/
119main(){
120    int i;
121    char x,tem='A';
122    linkque *que,queue;
123    que=&queue;
124
125    initque(que);/*调用初始化函数构造空队列*/
126    print(queue);
127
128    for(i=1;i<=8;i++)  enque(que,tem++);/*循环调用入队函数,向队尾插入元素*/
129    print(queue);
130    printf("There are %d elements in this queue\n",quelength(queue));
131
132    for(i=1;i<=7;i++)  entque(que,tem++);
133    print(queue);
134    printf("There are %d elements in this queue\n",quelength(queue));
135
136    printf("The head element:%c\n",gethead(queue));
137
138    x=delque(que);
139    printf("After delque,");    print(queue);
140    printf("The deleted element: %c\n",x);
141    print(queue);
142    printf("The head element:%c\n",gethead(queue));
143    printf("There are %d elements in this queue\n",quelength(queue));
144
145getch();
146}

147
148
149
150
151
152
153
154
155
156
posted @ 2009-07-25 10:14  林田惠  阅读(281)  评论(0编辑  收藏  举报


.
    ;  `┣━┒ ; `.┣─┒`   . .   ;   `.        
   .┟━┃┍╄┓┟━│ ╃━  `     、.    
` ┝─┃┣╈┤┣━┃;/ ╈ ╰⌒ˋのˊ  
 . ┗━┘┗┸┛└━┛/┃┻ `.   ′ ˋ `