您的位置:首页 > 其它

队列_链表实现

2011-06-17 09:54 225 查看
1:  // RLC分割.cpp : 定义控制台应用程序的入口点。


2:  //


3:   


4:  #include "stdafx.h"


5:  #include


6:   


7:  typedef char ElemType;


8:   


9:  typedef struct QNode{


10:      ElemType data;


11:      QNode *next;


12:  } QNode , *QueuePtr;


13:   


14:  typedef struct{


15:      QueuePtr front;   //队头指针


16:      QueuePtr rear;    //队尾指针


17:  }LinkQueue;


18:   


19:  void initQueue(LinkQueue *q){


20:      /*初始化一个空队列*/


21:      q->front = q->rear = (QueuePtr)malloc(sizeof(QNode)); /*创建一个头结点,队头队尾指针                                                                指向该结点*/


22:      if( !q->front) exit(0);     /*创建头结点失败*/


23:      q->front->next = NULL;     /*头结点指针域置NULL*/


24:  }


25:   


26:  void EnQueue(LinkQueue *q, ElemType e){


27:      QueuePtr p;


28:      p = (QueuePtr)malloc(sizeof(QNode));  /*创建一个队列元素结点*/


29:      if( !p) exit(0);    /*创建结点失败*/


30:      p->data = e;


31:      p->next = NULL;


32:      q->rear ->next = p;


33:      q->rear = p;


34:  }


35:   


36:  void DeQueue(LinkQueue *q, ElemType *e){


37:      /*如果队列q不为空,删除q的队头元素,用e返回其值*/


38:      QueuePtr p;


39:      if(q->front == q->rear) return;  /*队列为空,返回*/


40:      p = q->front->next;


41:      *e = p->data;


42:      q->front->next = p->next;


43:      if(q->rear == p) q->rear = q->front;  /*如果队头就是队尾,则修改队尾指针*/


44:      free(p);


45:  }


46:   


47:  int _tmain(int argc, _TCHAR* argv[])


48:  {


49:      ElemType e;


50:      LinkQueue  q;


51:      initQueue(&q);           /*初始化一个队列q*/


52:      printf("Please input a char into a queue/n");


53:      scanf("%c",&e);


54:      while(e!='@'){


55:          EnQueue(&q,e);   /*向队列中输入字符串,以@表示结束*/


56:          scanf("%c",&e);


57:      }


58:      printf("The string into the queue is/n");


59:      while(q.front != q.rear){   /*将队列中的元素出队列,并打印在屏幕上*/


60:          DeQueue(&q,&e);


61:          printf("%c",e);


62:      }


63:      printf("/n");


64:      system("pause");


65:      return 0;


66:  }


67:   


.csharpcode, .csharpcode pre
{
font-size: small;
color: black;
font-family: consolas, "Courier New", courier, monospace;
background-color: #ffffff;
/*white-space: pre;*/
}
.csharpcode pre { margin: 0em; }
.csharpcode .rem { color: #008000; }
.csharpcode .kwrd { color: #0000ff; }
.csharpcode .str { color: #006080; }
.csharpcode .op { color: #0000c0; }
.csharpcode .preproc { color: #cc6633; }
.csharpcode .asp { background-color: #ffff00; }
.csharpcode .html { color: #800000; }
.csharpcode .attr { color: #ff0000; }
.csharpcode .alt
{
background-color: #f4f4f4;
width: 100%;
margin: 0em;
}
.csharpcode .lnum { color: #606060; }
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: