博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
算法(4)—— 队列的链表实现
阅读量:5788 次
发布时间:2019-06-18

本文共 3507 字,大约阅读时间需要 11 分钟。

1. 队列的链表实现EnQueue 在尾节点,DeQueue 在首节点。

1 #include  "fatal.h"  2   3 typedef int ElementType ;  4 typedef struct QueueNode * Queue;  5 typedef struct QueueNode * Position;  6   7 #define MAXSIZE (6)  8   9  10 struct QueueNode  { 11   ElementType data; 12   Position Next; 13   Position Rear; 14   Position Front; 15   int size; 16 }; 17  18 void DeleteQueue  (Queue Q); 19 Queue MakeEmpty(Queue Q); 20 int  IsEmpty  (Queue Q); 21 int  IsFull (Queue Q); 22 void EnQueue  (ElementType X, Queue Q); 23 void DeQueue  (Queue Q); 24 ElementType FrontAndDequeue (Queue); 25  26  27  28 int main(int argc, char *argv[])  { 29   Queue Q; 30   Q = MakeEmpty (NULL); 31  32   EnQueue (1,Q); 33   EnQueue (2,Q); 34    35   ElementType X = 12 ; 36    37   while ( !IsEmpty(Q))  { 38     printf ("%d ", Q->Front->data); 39     DeQueue (Q); 40   } 41   printf("\n");   42   printf("Empty = %c\n",IsEmpty(Q)?'Y':'N' ); 43  44   Q = MakeEmpty (NULL); 45   EnQueue (X,Q); 46   EnQueue (21,Q); 47   EnQueue (X,Q); 48   printf("Queue is: "); 49    50   Position P = Q -> Front; 51   while (P)  { 52     printf ("%d ",P->data); 53     P = P->Next; 54   } 55   printf("\n"); 56    57   printf ("Front and Rear:%d %d  ",Q->Front->data,Q->Rear->data); 58   printf("\n"); 59    60   ElementType Y = FrontAndDequeue (Q); 61   printf ("Del and Front:%d %d ", Y,Q->Front->data); 62    63   printf("\n"); 64   Y = FrontAndDequeue (Q); 65   printf ("Del and Front:%d %d ", Y,Q->Front->data); 66   printf("\n"); 67    68   Y = FrontAndDequeue (Q); 69   printf ("Del and Front:%d %d\n", Y,(Q->Front == Q?0:Q->Front->data)); 70  71   DeQueue (Q);    //do not Del any node 72   DeQueue (Q); 73  74   printf("size = %d\n",Q->size); 75  76   return 0; 77  78 } 79  80  81 void Delete (Queue Q)  { 82   if ( Q = NULL) 83     Error ("Out of space."); 84   Position P, tmp; 85   P = Q->Next; 86   Q -> Next = NULL; 87  88   while ( P != NULL)  { 89     tmp = P -> Next; 90     free (P); 91     P = tmp; 92   } 93 } 94  95 Queue MakeEmpty (Queue Q)  { 96   if ( Q != NULL) 97     Delete (Q); 98  99   Q = malloc (sizeof (struct QueueNode));100   if ( Q == NULL)  Error ("Out of memory.");101   Q -> Next = NULL;102   Q -> Rear = Q;103   Q -> Front = Q->Next;104   Q-> size = 0;105   return Q;106 }107 108 int IsFull (Queue Q)  {109   return Q->size == MAXSIZE; 110 }111 112 int IsEmpty (Queue Q)  {113   return Q->size == 0;114 }115 116 void EnQueue  (ElementType X, Queue Q)  {117   if ( Q == NULL ) Error ("Out of space.");118 119   Position tmp;120   tmp = malloc (sizeof (struct QueueNode));121   if (tmp == NULL)  Error ("Out of space,");122 123   tmp -> data = X;124   Q -> Rear -> Next = tmp;125   Q -> Rear = tmp;126   Q -> Rear -> Next = NULL;127   Q -> Front = Q -> Next;128   Q->size++;129 130   if (Q -> size > MAXSIZE)  Error ("Too large.");131   132 }133   134 void DeQueue  (Queue Q)  {135   if (IsEmpty(Q))  Error ("Empty Queue.");136   137   Position tmp = Q -> Front;138   Q -> Next = tmp -> Next;139    free(tmp);140   if (Q -> Front == Q->Rear)  { Q->Front = Q;}141   else {   Q -> Front = Q->Next;  }142   Q->size--;143 }144   145 ElementType FrontAndDequeue ( Queue Q)  {146   ElementType X;147   if (IsEmpty (Q))  Error ("Empty Queue.");148   149   X = Q ->Front->data;150   Position t = Q->Front;151   Q -> Next = t-> Next;152   if (Q -> Front == Q->Rear)  { Q->Front = Q;}153   else {   Q -> Front = Q->Next;  }154   free (t);155   Q -> size--;156     157   return X;158 }

 

转载于:https://www.cnblogs.com/hanxinle/p/7426370.html

你可能感兴趣的文章
C++中指针和引用的区别
查看>>
簡單分稀 iptables 記錄 udp 微軟 138 端口
查看>>
Java重写equals方法和hashCode方法
查看>>
Spark API编程动手实战-07-join操作深入实战
查看>>
H3C-路由策略
查看>>
centos 修改字符界面分辨率
查看>>
LNMP之Mysql主从复制(四)
查看>>
阅读Spring源代码(1)
查看>>
nagios一键安装脚本,nagios监控被监控主机上的应用服务mysql数据库
查看>>
grep 命令
查看>>
JS二维数组的声明和使用
查看>>
v$archive_gap dg dataguard 断档处理 scn恢复
查看>>
问责IT风险管理:CIO需关注两个重点
查看>>
Winform打包发布图解
查看>>
PDF文件怎么编辑,超简单的方法
查看>>
EasyUI基础入门之Easyloader(载入器)
查看>>
Uva 839 Not so Mobile
查看>>
30款超酷的HTTP 404页面未找到错误设计
查看>>
程序猿必备 MyEclipse2013-2014系列
查看>>
java中ArrayList 、LinkList区别
查看>>