체인 대기열 M

14342 단어 대열
  1 #include<stdio.h>

  2 #include<stdlib.h>

  3 #define ok 1

  4 #define error 0

  5 #define overflow -1

  6 #define TRUE 1

  7 #define FALSE 0

  8 #define status int

  9 typedef struct QNode

 10 {  int data;

 11    struct QNode *next;

 12 }QNode,*QueuePtr;

 13 typedef struct

 14 {  QueuePtr front;

 15    QueuePtr rear;

 16 }LinkQueue;

 17 status creat(LinkQueue &Q,int n)// n , . 

 18 {

 19     QueuePtr p,s;

 20     Q.front=(QueuePtr)malloc(sizeof(QNode));

 21     p=Q.front;

 22     Q.front->data=n;

 23     printf("
"); 24 while(n--) 25 { 26 if(!(s=(QueuePtr)malloc(sizeof(QNode)))) 27 exit(error); 28 p->next=s; 29 s->next=NULL; 30 scanf("%d",&s->data); 31 p=s; 32 } 33 Q.rear=p; 34 return ok; 35 } 36 status DestroyQueue(LinkQueue &Q) 37 { 38 while(Q.front) 39 { 40 Q.rear=Q.front->next; 41 free(Q.front); 42 Q.front=Q.rear; 43 } 44 return ok; 45 } 46 status QueueEmpty(LinkQueue Q) 47 { 48 if(Q.front->next==NULL) 49 return TRUE; 50 else 51 return FALSE; 52 } 53 status GetHead(LinkQueue Q,int &e)// , e 54 { 55 QueuePtr p; 56 if(Q.front==Q.rear) 57 return error; 58 p=Q.front->next; 59 e=p->data; 60 return ok; 61 } 62 status EnQueue(LinkQueue &Q,int e)// e 63 { 64 QueuePtr p; 65 p=(QueuePtr)malloc(sizeof(QNode)); 66 if(!p) exit(overflow); 67 p->data=e; p->next=NULL; 68 Q.rear->next=p; 69 Q.rear=p; 70 Q.front->data++; 71 return ok; 72 } 73 status DeQueue(LinkQueue &Q,int &e)// Q , e 74 { 75 QueuePtr p; 76 if(Q.front==Q.rear) return error; 77 p=Q.front->next; 78 e=p->data; 79 Q.front->next=p->next; 80 if(Q.rear==p) Q.rear=Q.front; 81 free(p); 82 Q.front->data--; 83 return ok; 84 } 85 status printQueue(LinkQueue Q)// 86 { 87 QueuePtr p=Q.front; 88 printf(""); 89 printf("
********************
"); 90 while(p->next) 91 { 92 p=p->next; 93 printf("%d ",p->data); 94 } 95 printf("
********************
"); 96 return ok; 97 } 98 int main() 99 { 100 int m,n; 101 LinkQueue Q; 102 printf("
"); 103 scanf("%d",&n); 104 creat(Q,n); 105 printQueue(Q); 106 printf("
"); 107 scanf("%d",&m); 108 EnQueue(Q,m); 109 printQueue(Q); 110 printf("
"); 111 DeQueue(Q,m); 112 printf(" :%d
",m); 113 printQueue(Q); 114 DestroyQueue(Q); 115 system("pause"); 116 return ok; 117 }

좋은 웹페이지 즐겨찾기