carysu@126.com

carysu@126.com
posts - 11, comments - 0, trackbacks - 0, articles - 0

队列的链式存储

Posted on 2011-10-30 13:16 susu 阅读(316) 评论(0)  编辑 收藏 引用
  1
  2
  3
  4#include <stdio.h>
  5#include <malloc.h>
  6#define MAX 100
  7char exp[MAX];
  8
  9
 10struct qnode
 11{
 12    char data;
 13    struct qnode *next;
 14}
;
 15
 16typedef struct queue
 17{
 18    struct qnode *front;
 19    struct qnode *rear;
 20}
linkqueue;
 21
 22
 23void initqueue(linkqueue **q)
 24{
 25    struct queue *s;
 26    s = (struct queue *)malloc(sizeof(struct queue));
 27    s->front = s->rear = NULL;
 28    *= s;
 29}

 30/*  */
 31void enter(linkqueue **q, char x)
 32{
 33    struct qnode *s;
 34    linkqueue *q_tmp;
 35    q_tmp = *q;
 36    s = (struct qnode *)malloc(sizeof(struct qnode));
 37    s->data = x;
 38    s->next = NULL;
 39    if (q_tmp->front == NULL)
 40    {
 41        q_tmp->front = q_tmp->rear = s; 
 42    }

 43    else
 44    {
 45        q_tmp->rear->next = s;
 46        q_tmp->rear =s;
 47    }

 48    //*q = q_tmp;
 49}

 50
 51
 52void deleted(linkqueue **q)
 53{
 54    linkqueue *t;
 55    struct qnode *tmp;
 56    t = *q;
 57    if (t->front == NULL)  //队为空的时候
 58    {
 59        printf("队列为空!\n");
 60    }

 61    else if (t->front == t->rear)  //队只有节点的时候
 62    {
 63        //t = t->front;
 64        //t = t->front->next;
 65        tmp = t->front;
 66        t->front = t->rear;
 67    }

 68    else 
 69    {
 70        tmp = t->front;
 71        t->front = t->front->next;
 72    }

 73    free(tmp);
 74}

 75
 76
 77char gethead(linkqueue **q)
 78{
 79    linkqueue *s;
 80    s = *q;
 81    if (s->front == s->rear)
 82    {
 83        printf("队列为空!\n");
 84        return 0;
 85    }

 86    else 
 87    {
 88        return (s->front->data);
 89    }

 90}

 91
 92int empty(linkqueue **q)
 93{
 94    linkqueue *s;
 95    s = *q;  
 96
 97    if (s->front == s->rear)  return 1;
 98    else return 0;
 99}

100
101void display(linkqueue **q)
102{
103    linkqueue *s;
104    struct qnode *p;
105    s = *q;  
106    p = s->front;
107    printf("队列元素:\n");
108    while (p != NULL)
109    {
110        printf("%c :", p->data);
111        p = p->next;
112    }

113    printf("\n");
114}

115
116void main()
117{
118    char c;
119    linkqueue *qu;
120    printf("初始化队列qu \n");
121    initqueue(&qu);
122    printf("队列空:%d\n", empty(&qu));
123    printf("依次入队a, b, c, d 元素\n");
124    enter(&qu, 'a');
125    //printf("%c\n", qu->rear->data);
126    enter(&qu, 'b');
127    //printf("%c\n", qu->rear->data);
128    enter(&qu, 'c');
129    //printf("%c\n", qu->rear->data);
130    enter(&qu, 'd');
131    enter(&qu, 'e');
132    enter(&qu, 'f');
133    enter(&qu, 'g');
134    enter(&qu, 'h');
135    enter(&qu, 'i');
136    enter(&qu, 'j');
137    enter(&qu, 'k');
138    //printf("%c\n", qu->rear->data);
139    display(&qu);
140    c = gethead(&qu);
141    printf("%c\n", c);
142    deleted(&qu);
143    display(&qu);
144    c = gethead(&qu);
145    printf("%c\n", c);
146}

147

只有注册用户登录后才能发表评论。
网站导航: 博客园   IT新闻   BlogJava   知识库   博问   管理