程序代码:
#include <stdio.h>
#include<stdlib.h>
#include<iostream.h>
typedef struct QNode{
int data;
struct QNode * next;
}QNode, * QueuePtr;
typedef struct
{
QueuePtr front;
QueuePtr rear;
}LinkQueue;
LinkQueue Q;
int InitQueue( )
{
QueuePtr p;
Q.front=Q.rear=(QueuePtr)malloc(sizeof(QNode));
if(!Q.front)
{
printf("OVERFLOW");
return 0;
}
p->next=NULL;
return 1 ;
}
void DestroyQueue( )
{
while(Q.front)
{
Q.rear=Q.front->next;
free(Q.front);
Q.front=Q.rear;
}
}
int EnQueue ( )
{
QueuePtr p;
int e;
p=(QueuePtr)malloc(sizeof(QNode));
if(!p)
{
printf("OVERFLOW");
return 0;
}
printf("Input the data:\n");
scanf("%d",&e);
p->data=e;
p->next=NULL;
Q.rear->next=p;
Q.rear=p;
}
void DeQueue( )
{
int e;
QueuePtr p;
if(Q.front==Q.rear)
{
printf("The queue is empty!\n\n");
return ;
}
p=Q.front->next;
e=p->data;
printf("The top data is: %d\n\n",e);
Q.front->next=p->next;
if(Q.rear==p) Q.rear=Q.front;
free(p);
}
void PrintQueue( )
{
QueuePtr p;
int i=1;
if(Q.front==Q.rear)
{
printf("ERROR!\nThe Queue is empty!\n");
return ;
}
printf("The Queue is:\n");
p=Q.front->next;
while(p!=Q.rear)
{
printf("%d %d\n",i++,p->data);
p=p->next;
}
printf("%d %d\n\n",i,p->data);
}
int main()
{
int i=1;
while (i)
{
printf("1. Create a Queue\n");
printf("2. Destroy the Queue\n");
printf("3. Insert a data\n");
printf("4. Delete a data\n");
printf("5. Print the Queue\n");
printf("0. Exit\n\n");
printf("What do you want to do?\n");
scanf("%d",&i);
switch(i)
{
case 1: InitQueue();break;
case 2: DestroyQueue();break;
case 3: EnQueue();break;
case 4: DeQueue();break;
case 5: PrintQueue();break;
case 0: return 1;
default: printf("ERROR! Try again!\n");
}
}
return 1;
}
以上就是源程序
谢了
对了,我还试了一下,似乎QNode还是类型名