[C] 链队基本操作(创建 入队 出队 判断空) →→→→→进入此内容的聊天室

来自 , 2021-03-12, 写在 C, 查看 107 次.
URL http://www.code666.cn/view/978d7667
  1. //创建一个带头结点的空队:
  2. LQueue *Init_LQueue()
  3. {
  4.         LQueue *q,*p;
  5.         q=malloc ( sizeof ( LQueue ) ); /*申请头尾指针结点*/
  6.         p=malloc ( sizeof ( QNode ) ); /*申请链队头结点*/
  7.         p->next=NULL;
  8.         q->front=q->rear=p;
  9.         return q;
  10. }
  11.  
  12. // 入队
  13. void In_LQueue ( LQueue *q , datatype x )
  14. {
  15.         QNode *p;
  16.         p=malloc ( sizeof ( QNnode ) ); /*申请新结点*/
  17.         p->data=x;
  18.         p->next=NULL;
  19.         q->rear->next=p;
  20.         q->rear=p;
  21. }
  22.  
  23. //判队空
  24. int Empty_LQueue ( LQueue *q )
  25. {
  26.         if ( q->front==q->rear ) return 0;
  27.         else return 1;
  28. }
  29.  
  30. //出队
  31. int Out_LQueue ( LQueue *q , datatype *x )
  32. {
  33.         QNnode *p;
  34.         if ( Empty_LQueue ( q ) )
  35.         {
  36.                 printf ( "队空" )return 0;
  37.         } /*队空,出队失败*/
  38.         else
  39.         {
  40.                 p=q->front->neat;
  41.                 q->front->next=p->next;
  42.                 *x=p->data;/*队头元素放x 中*/
  43.                 free ( p );
  44.                 if ( q->front->next==NULL )
  45.                         q->rear=q->front;
  46.                 /*只有一个元素时,出队后队空,此时还要要修改队尾指针参考图3.16(c)*/
  47.                 return 1;
  48.         }
  49. }

回复 "链队基本操作(创建 入队 出队 判断空)"

这儿你可以回复上面这条便签

captcha