码迷,mamicode.com
首页 > 其他好文 > 详细

队列的定义与操作——顺序存储和链式存储

时间:2018-11-17 16:42:56      阅读:167      评论:0      收藏:0      [点我收藏+]

标签:mil   指针   元素   struct   tty   microsoft   span   position   else   

  队列的 存储结构 有 顺序存储 和 链式存储

  1. 队列的顺序存储与操作 (循环队列)

 1 typedef int Position;
 2 struct QNode {
 3     ElementType *Data;     // 存储元素的数组 
 4     Position Front, Rear;  // 队列的 头、尾指针 
 5     int Cap;               // 队列最大大容量 
 6 };
 7 typedef struct QNode *Queue; 
 8 
 9 // 操作集
10 Queue CreateQueue(int MaxSize);
11 int IsFull(Queue Q);
12 int IsEmpty(Queue Q);
13 void AddQ(Queue Q, ElementType X);
14 ElementType DeleteQ(Queue Q);
15 
16 Queue CreateQueue()
17 {
18     Queue Q = (Queue)malloc(sizeof(struct QNode));
19     Q->Data = (ElementType*)malloc(MaxSize * sizeof(ElementType));
20     Q->Front = Q->Rear = 0;
21     Q->Cap= MaxSize;
22     
23     return Q;
24 }
25 
26 int IsFull(Queue Q)
27 {
28     return (Q->Rear+1)%Q->MaxSize == Q->Front;
29 }
30 
31 void AddQ(Queue Q, ElementType X)
32 {
33     if ( IsFull(Q) )
34         return;            // queue is fulled
35     else  {
36         Q->Rear = (Q->Rear+1)%Q->MaxSize;
37         Q->Data[Q->Rear] = X;
38     }
39 }
40 
41 int IsEmpty(Queue Q)
42 {
43     return (Q->Front == Q->Rear);
44 }
45 
46 ElementType DeleteQ(Queue Q)
47 {
48     if ( IsEmpty(Q) ) 
49         return NULL; // 这里的返回值应该视具体情况而定(可能返回 NULL,或者返回 ElementType 类型)
50     else {
51         Q->Front = (Q->Front+1)%Q->MaxSize;
52         return Q->Data[Q->Front];
53     }
54 }            

  2.队列的链式存储与操作

 1 typedef struct Node *PtrToNode;
 2 struct Node {    // 队列中的节点 
 3     ElementType Data;
 4     PtrToNode Next;
 5 };
 6 typedef PtrToNode Position;
 7 
 8 struct QNode {
 9     Position Front, Rear;    // 队列的头、尾指针 
10 };
11 typedef struct QNode *Queue;
12 
13 // 操作集
14 Queue CreateQueue();
15 int IsEmpty(Queue Q);
16 void AddQ(Queue Q, ElementType X);
17 ElementType DeleteQ(Queue Q);
18 
19 Queue CreateQueue()
20 {
21     Queue Q = (Queue)malloc(sizeof(struct QNode));
22     Q->Front = Q->Rear = NULL;
23     
24     return Q;
25 }
26 
27 int IsEmpty(Queue Q)
28 {
29     return ( Q->Front == NULL );
30 }
31 
32 void AddQ(Queue Q, ElementType X)
33 {
34     PtrToNode TmpCell = (struct Node*)malloc(sizeof(struct Node)); 
35     TmpCell->Next = NULL;
36     TmpCell->Data= X;
37     
38     if ( IsEmpty(Q) )    //  队列为空 
39         Q->Front = Q->Rear = TmpCell;
40     else {
41         Q->Rear->Next = TmpCell;
42         Q->Rear = TmpCell;
43     }
44 }
45 
46 ElementType DeleteQ(Queue Q)
47 {
48     if ( IsEmpty(Q) )    return NULL;  // 这里的返回值应该视具体情况而定(可能返回 NULL,或者返回 ElementType 类型)
49     
50     Position FrontCell;
51     ElementType FrontElem;
52     FrontCell = Q->Front;
53     
54     if ( Q->Front == Q->Rear )        //  若队列中只有一个元素 
55         Q->Front = Q->Rear = NULL;    //  删除后置空 
56     else                         
57         Q->Front = Q->Front->Next;
58     
59     FrontElem = FrontCell->Data;
60     free(FrontCell);
61     
62     return FrontElem;
63 }

 

队列的定义与操作——顺序存储和链式存储

标签:mil   指针   元素   struct   tty   microsoft   span   position   else   

原文地址:https://www.cnblogs.com/wgxi/p/9974513.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!