主题:循环链队问题
#include<iostream.h>
typedef struct queuenode
{
char data;
struct queuenode *next;
}queuenode;
typedef struct
{
queuenode *rear;
}linkqueue;
int initqueue(linkqueue *q)
{
q->rear=new(queuenode);
q->rear->next=q->rear;
return 1;
}
int emptyqueue(linkqueue *q)
{
return q->rear->next->next==q->rear->next ;
}
void enqueue(linkqueue *q,char x)
{
queuenode *p=new(queuenode);
p->data=x;
p->next=q->rear->next;
q->rear->next=p;
q->rear=p;
}
int dequeue(linkqueue *q,char x)
{
queuenode *p;
if(emptyqueue(q))
{
cout<<"errort"<<endl;
return 0;
}
else
{
p=q->rear->next->next;
x=p->data;
if(p==q->rear)
{q->rear=q->rear->next;q->rear ->next =p->next;}
else
{q->rear->next->next=p->next;}
return 1;
}
}
void main()
{
linkqueue p;
char x;
int j=0,i;
initqueue(&p);
cout<<"input:";
while(1)
{
cin>>x;
if(x!='$')
{
enqueue(&p,x);
j++;
}
else
break;
}
cout<<j<<endl;
for(i=0;i<j;i++)
{
dequeue(&p,x);
cout<<x;
}
}
typedef struct queuenode
{
char data;
struct queuenode *next;
}queuenode;
typedef struct
{
queuenode *rear;
}linkqueue;
int initqueue(linkqueue *q)
{
q->rear=new(queuenode);
q->rear->next=q->rear;
return 1;
}
int emptyqueue(linkqueue *q)
{
return q->rear->next->next==q->rear->next ;
}
void enqueue(linkqueue *q,char x)
{
queuenode *p=new(queuenode);
p->data=x;
p->next=q->rear->next;
q->rear->next=p;
q->rear=p;
}
int dequeue(linkqueue *q,char x)
{
queuenode *p;
if(emptyqueue(q))
{
cout<<"errort"<<endl;
return 0;
}
else
{
p=q->rear->next->next;
x=p->data;
if(p==q->rear)
{q->rear=q->rear->next;q->rear ->next =p->next;}
else
{q->rear->next->next=p->next;}
return 1;
}
}
void main()
{
linkqueue p;
char x;
int j=0,i;
initqueue(&p);
cout<<"input:";
while(1)
{
cin>>x;
if(x!='$')
{
enqueue(&p,x);
j++;
}
else
break;
}
cout<<j<<endl;
for(i=0;i<j;i++)
{
dequeue(&p,x);
cout<<x;
}
}