队列

队列的性质

1.队列是一种特殊的线性表
2.队列仅在线性表的两端进行操作
3.队头(Front):取出数据元素的一端
 4.队尾(Rear):插入数据元素的一端

 

 

 

队列也有两种存在的形式,顺序和链式。

链式队列

 1 #include <malloc.h>
 2 #include "LinkList.h"
 3 #include "LinkQueue.h"
 4 
 5 typedef struct _tag_LinkQueueNode
 6 {
 7     LinkListNode header;
 8     void* item;
 9 } TLinkQueueNode;
10 
11 LinkQueue* LinkQueue_Create() // O(1)
12 {
13     return LinkList_Create();
14 }
15 
16 void LinkQueue_Destroy(LinkQueue* queue) // O(n)
17 {
18     LinkQueue_Clear(queue);
19     LinkList_Destroy(queue);
20 }
21 
22 void LinkQueue_Clear(LinkQueue* queue) // O(n)
23 {
24     while( LinkQueue_Length(queue) > 0 )
25     {
26         LinkQueue_Retrieve(queue);
27     }
28 }
29 
30 int LinkQueue_Append(LinkQueue* queue, void* item) // O(n)
31 {
32     TLinkQueueNode* node = (TLinkQueueNode*)malloc(sizeof(TLinkQueueNode));
33     int ret = (item != NULL) && (node != NULL);
34     
35     if( ret )
36     {
37         node->item = item;
38         
39         ret = LinkList_Insert(queue, (LinkListNode*)node, LinkList_Length(queue));
40     }
41     
42     if( !ret )
43     {
44         free(node);
45     }
46     
47     return ret;
48 }
49 
50 void* LinkQueue_Retrieve(LinkQueue* queue) // O(1)
51 {
52     TLinkQueueNode* node = (TLinkQueueNode*)LinkList_Delete(queue, 0);
53     void* ret = NULL;
54     
55     if( node != NULL )
56     {
57         ret = node->item;
58         
59         free(node);
60     }
61     
62     return ret;
63 }
64 
65 void* LinkQueue_Header(LinkQueue* queue) // O(1)
66 {
67     TLinkQueueNode* node = (TLinkQueueNode*)LinkList_Get(queue, 0);
68     void* ret = NULL;
69     
70     if( node != NULL )
71     {
72         ret = node->item;
73     }
74     
75     return ret;
76 }
77 
78 int LinkQueue_Length(LinkQueue* queue) // O(1)
79 {
80     return LinkList_Length(queue);
81 }
  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include "LinkList.h"
  4 
  5 typedef struct _tag_LinkList
  6 {
  7     LinkListNode header;
  8     int length;
  9 } TLinkList;
 10 
 11 LinkList* LinkList_Create() // O(1)
 12 {
 13     TLinkList* ret = (TLinkList*)malloc(sizeof(TLinkList));
 14     
 15     if( ret != NULL )
 16     {
 17         ret->length = 0;
 18         ret->header.next = NULL;
 19     }
 20     
 21     return ret;
 22 }
 23 
 24 void LinkList_Destroy(LinkList* list) // O(1)
 25 {
 26     free(list);
 27 }
 28 
 29 void LinkList_Clear(LinkList* list) // O(1)
 30 {
 31     TLinkList* sList = (TLinkList*)list;
 32     
 33     if( sList != NULL )
 34     {
 35         sList->length = 0;
 36         sList->header.next = NULL;
 37     }
 38 }
 39 
 40 int LinkList_Length(LinkList* list) // O(1)
 41 {
 42     TLinkList* sList = (TLinkList*)list;
 43     int ret = -1;
 44     
 45     if( sList != NULL )
 46     {
 47         ret = sList->length;
 48     }
 49     
 50     return ret;
 51 }
 52 
 53 int LinkList_Insert(LinkList* list, LinkListNode* node, int pos) // O(n)
 54 { 
 55     TLinkList* sList = (TLinkList*)list;
 56     int ret = (sList != NULL) && (pos >= 0) && (node != NULL);
 57     int i = 0;
 58     
 59     if( ret )
 60     {
 61         LinkListNode* current = (LinkListNode*)sList;
 62         
 63         for(i=0; (i<pos) && (current->next != NULL); i++)
 64         {
 65             current = current->next;
 66         }
 67         
 68         node->next = current->next;
 69         current->next = node;
 70         
 71         sList->length++;
 72     }
 73     
 74     return ret;
 75 }
 76 
 77 LinkListNode* LinkList_Get(LinkList* list, int pos) // O(n)
 78 {
 79     TLinkList* sList = (TLinkList*)list;
 80     LinkListNode* ret = NULL;
 81     int i = 0;
 82     
 83     if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
 84     {
 85         LinkListNode* current = (LinkListNode*)sList;
 86         
 87         for(i=0; i<pos; i++)
 88         {
 89             current = current->next;
 90         }
 91         
 92         ret = current->next;
 93     }
 94     
 95     return ret;
 96 }
 97 
 98 LinkListNode* LinkList_Delete(LinkList* list, int pos) // O(n)
 99 {
100     TLinkList* sList = (TLinkList*)list;
101     LinkListNode* ret = NULL;
102     int i = 0;
103     
104     if( (sList != NULL) && (0 <= pos) && (pos < sList->length) )
105     {
106         LinkListNode* current = (LinkListNode*)sList;
107         
108         for(i=0; i<pos; i++)
109         {
110             current = current->next;
111         }
112         
113         ret = current->next;
114         current->next = ret->next;
115         
116         sList->length--;
117     }
118     
119     return ret;
120 }
 1 #ifndef _LINKLIST_H_
 2 #define _LINKLIST_H_
 3 
 4 typedef void LinkList;
 5 typedef struct _tag_LinkListNode LinkListNode;
 6 struct _tag_LinkListNode
 7 {
 8     LinkListNode* next;
 9 };
10 
11 LinkList* LinkList_Create();
12 
13 void LinkList_Destroy(LinkList* list);
14 
15 void LinkList_Clear(LinkList* list);
16 
17 int LinkList_Length(LinkList* list);
18 
19 int LinkList_Insert(LinkList* list, LinkListNode* node, int pos);
20 
21 LinkListNode* LinkList_Get(LinkList* list, int pos);
22 
23 LinkListNode* LinkList_Delete(LinkList* list, int pos);
24 
25 #endif
 1 #ifndef _LINKQUEUE_H_
 2 #define _LINKQUEUE_H_
 3 
 4 typedef void LinkQueue;
 5 
 6 LinkQueue* LinkQueue_Create();
 7 
 8 void LinkQueue_Destroy(LinkQueue* queue);
 9 
10 void LinkQueue_Clear(LinkQueue* queue);
11 
12 int LinkQueue_Append(LinkQueue* queue, void* item);
13 
14 void* LinkQueue_Retrieve(LinkQueue* queue);
15 
16 void* LinkQueue_Header(LinkQueue* queue);
17 
18 int LinkQueue_Length(LinkQueue* queue);
19 
20 #endif
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "LinkQueue.h"
 4 
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 
 7 int main(int argc, char *argv[]) 
 8 {
 9     LinkQueue* queue = LinkQueue_Create();
10     int a[10] = {0};
11     int i = 0;
12     
13     for(i=0; i<10; i++)
14     {
15         a[i] = i + 1;
16         
17         LinkQueue_Append(queue, a + i);
18     }
19     
20     printf("Header: %d\n", *(int*)LinkQueue_Header(queue));
21     printf("Length: %d\n", LinkQueue_Length(queue));
22     
23     while( LinkQueue_Length(queue) > 0 )
24     {
25         printf("Retrieve: %d\n", *(int*)LinkQueue_Retrieve(queue));
26     }
27     
28     LinkQueue_Destroy(queue);
29     
30     return 0;
31 }

 

顺序队列

  1 #include <stdio.h>
  2 #include <malloc.h>
  3 #include "SeqList.h"
  4 
  5 typedef unsigned int TSeqListNode;
  6 
  7 typedef struct _tag_SeqList
  8 {
  9     int capacity;
 10     int length;
 11     TSeqListNode* node;
 12 } TSeqList;
 13 
 14 SeqList* SeqList_Create(int capacity) // O(1)
 15 {
 16     TSeqList* ret = NULL;
 17     
 18     if( capacity >= 0 )
 19     {
 20         ret = (TSeqList*)malloc(sizeof(TSeqList) + sizeof(TSeqListNode) * capacity);
 21     }
 22     
 23     if( ret != NULL )
 24     {
 25         ret->capacity = capacity;
 26         ret->length = 0;
 27         ret->node = (TSeqListNode*)(ret + 1);
 28     }
 29     
 30     return ret;
 31 }
 32 
 33 void SeqList_Destroy(SeqList* list) // O(1)
 34 {
 35     free(list);
 36 }
 37 
 38 void SeqList_Clear(SeqList* list) // O(1)
 39 {
 40     TSeqList* sList = (TSeqList*)list;
 41     
 42     if( sList != NULL )
 43     {
 44         sList->length = 0;
 45     }
 46 }
 47 
 48 int SeqList_Length(SeqList* list) // O(1)
 49 {
 50     TSeqList* sList = (TSeqList*)list;
 51     int ret = -1;
 52     
 53     if( sList != NULL )
 54     {
 55         ret = sList->length;
 56     }
 57     
 58     return ret;
 59 }
 60 
 61 int SeqList_Capacity(SeqList* list) // O(1)
 62 {
 63     TSeqList* sList = (TSeqList*)list;
 64     int ret = -1;
 65     
 66     if( sList != NULL )
 67     {
 68         ret = sList->capacity;
 69     }
 70     
 71     return ret;
 72 }
 73 
 74 int SeqList_Insert(SeqList* list, SeqListNode* node, int pos) // O(n) 
 75 {
 76     TSeqList* sList = (TSeqList*)list;
 77     int ret = (sList != NULL);
 78     int i = 0;
 79     
 80     ret = ret && (sList->length + 1 <= sList->capacity);
 81     ret = ret && (0 <= pos);
 82     
 83     if( ret )
 84     {
 85         if( pos >= sList->length )
 86         {
 87             pos = sList->length;
 88         }
 89         
 90         for(i=sList->length; i>pos; i--)
 91         {
 92             sList->node[i] = sList->node[i-1];
 93         }
 94         
 95         sList->node[i] = (TSeqListNode)node;
 96         
 97         sList->length++;
 98     }
 99     
100     return ret;
101 }
102 
103 SeqListNode* SeqList_Get(SeqList* list, int pos) // O(1) 
104 {
105     TSeqList* sList = (TSeqList*)list;
106     SeqListNode* ret = NULL;
107     
108     if( (sList != NULL) && (0 <= pos) && (pos <= sList->length) )
109     {
110         ret = (SeqListNode*)(sList->node[pos]);
111     }
112     
113     return ret;
114 }
115 
116 SeqListNode* SeqList_Delete(SeqList* list, int pos) // O(n)
117 {
118     TSeqList* sList = (TSeqList*)list;
119     SeqListNode* ret = SeqList_Get(list, pos);
120     int i = 0;
121     
122     if( ret != NULL )
123     {
124         for(i=pos+1; i<sList->length; i++)
125         {
126             sList->node[i-1] = sList->node[i];
127         }
128         
129         sList->length--;
130     }
131     
132     return ret;
133 }
 1 #ifndef _SEQLIST_H_
 2 #define _SEQLIST_H_
 3 
 4 typedef void SeqList;
 5 typedef void SeqListNode;
 6 
 7 SeqList* SeqList_Create(int capacity);
 8 
 9 void SeqList_Destroy(SeqList* list);
10 
11 void SeqList_Clear(SeqList* list);
12 
13 int SeqList_Length(SeqList* list);
14 
15 int SeqList_Capacity(SeqList* list);
16 
17 int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
18 
19 SeqListNode* SeqList_Get(SeqList* list, int pos);
20 
21 SeqListNode* SeqList_Delete(SeqList* list, int pos);
22 
23 #endif
 1 #include "SeqList.h"
 2 #include "SeqQueue.h"
 3 
 4 SeqQueue* SeqQueue_Create(int capacity) // O(1)
 5 {
 6     return SeqList_Create(capacity);
 7 }
 8 
 9 void SeqQueue_Destroy(SeqQueue* queue) // O(1)
10 {
11     SeqList_Destroy(queue);
12 }
13 
14 void SeqQueue_Clear(SeqQueue* queue) // O(1)
15 {
16     SeqList_Clear(queue);
17 }
18 
19 int SeqQueue_Append(SeqQueue* queue, void* item) // O(1)
20 {
21     return SeqList_Insert(queue, item, SeqList_Length(queue));
22 }
23 
24 void* SeqQueue_Retrieve(SeqQueue* queue) // O(n)
25 {
26     return SeqList_Delete(queue, 0);
27 }
28 
29 void* SeqQueue_Header(SeqQueue* queue) // O(1) 
30 {
31     return SeqList_Get(queue, 0);
32 }
33 
34 int SeqQueue_Length(SeqQueue* queue) // O(1)
35 {
36     return SeqList_Length(queue);
37 }
38 
39 int SeqQueue_Capacity(SeqQueue* queue) // O(1)
40 {
41     return SeqList_Capacity(queue);
42 }
 1 #ifndef _SEQQUEUE_H_
 2 #define _SEQQUEUE_H_
 3 
 4 typedef void SeqQueue;
 5 
 6 SeqQueue* SeqQueue_Create(int capacity);
 7 
 8 void SeqQueue_Destroy(SeqQueue* queue);
 9 
10 void SeqQueue_Clear(SeqQueue* queue);
11 
12 int SeqQueue_Append(SeqQueue* queue, void* item);
13 
14 void* SeqQueue_Retrieve(SeqQueue* queue);
15 
16 void* SeqQueue_Header(SeqQueue* queue);
17 
18 int SeqQueue_Length(SeqQueue* queue);
19 
20 int SeqQueue_Capacity(SeqQueue* queue);
21 
22 #endif
 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "SeqQueue.h"
 4 
 5 /* run this program using the console pauser or add your own getch, system("pause") or input loop */
 6 
 7 int main(int argc, char *argv[]) 
 8 {
 9     SeqQueue* queue = SeqQueue_Create(20);
10     int a[10] = {0};
11     int i = 0;
12     
13     for(i=0; i<10; i++)
14     {
15         a[i] = i + 1;
16         
17         SeqQueue_Append(queue, a + i);
18     }
19     
20     printf("Header: %d\n", *(int*)SeqQueue_Header(queue));
21     printf("Length: %d\n", SeqQueue_Length(queue));
22     printf("Capacity: %d\n", SeqQueue_Capacity(queue));
23     
24     while( SeqQueue_Length(queue) > 0 )
25     {
26         printf("Retrieve: %d\n", *(int*)SeqQueue_Retrieve(queue));
27     }
28     
29     SeqQueue_Destroy(queue);
30     
31     return 0;
32 }

队列是一种特殊的线性表
队列只允许在线性表的头部和尾部进行操作

 

posted on 2019-04-30 21:09  年少小五郎  阅读(257)  评论(0编辑  收藏  举报

导航