用带头节点的循环链表表示队列

#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;

typedef struct LNode{
  int data;
  struct LNode *next;
}LNode;
void enqueue(LNode *&rear,int x)
{
    LNode *s;
    s=(LNode *)malloc(sizeof(LNode));
    s->data=x;
    s->next=rear->next;
    rear->next=s;
    rear=rear->next;
}
int dequene(LNode *&rear,int &x)
{

   if(rear->next==rear) return 0;
   LNode *p;
   p=rear->next->next;
   x=p->data;
   rear->next=p->next;
   if(rear==p) rear=rear->next;
   free(p);
   return 1;

}
int main()
{
    LNode *A;
    A=(LNode *)malloc(sizeof(LNode));
    A->next=A;
    int x=0;
    enqueue(A,3);
    enqueue(A,4);
    dequene(A,x);
    printf("%d",x);
    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_39350434/article/details/81366186