📄 dqueue.cpp
字号:
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct queuenode
{
char data;
struct queuenode* nextptr;
}Queuenode;
typedef Queuenode*qptr;
void printqueue (qptr);
int isempty (qptr);
char dequeue (qptr*,qptr*);
void enqueue (qptr*,qptr*,char);
void instructions(void);
int main()
{clrscr();
qptr headptr=NULL,tailptr=NULL;
int choice;
char item;
instructions();
printf ("?");
scanf ("%d",&choice);
while (choice!=3)
{
switch (choice)
{
case 1:
{
printf ("Enter a character:");
scanf ("\n%c",&item);
enqueue (&headptr,&tailptr,item);
printqueue (headptr);
break;
}
case 2:
{
if (!isempty(headptr))
{
item=dequeue(&headptr,&tailptr);
printf ("%c has been dequeued.\n",item);
}
printqueue(headptr);
break;
}
default:
{
printf ("Invalid choice.\n\n");
instructions();
break;
}
}
printf ("?");
scanf ("%d",&choice);
}
printf ("End of the Program");
getch();
return 0;
}
void instructions(void)
{
printf ("Enter your choice:\n");
printf (" 1 to add an item to the queue\n");
printf (" 2 to remove an item from the queue\n");
printf (" 3 to end\n");
}
void enqueue (qptr *headptr,qptr*tailptr,char value)
{
qptr newptr;
newptr= (Queuenode*)malloc(sizeof (Queuenode));
if (newptr!=NULL)
{
newptr->data=value;
newptr->nextptr=NULL;
if (isempty(*headptr))
*headptr=newptr;
else
(*tailptr)->nextptr=newptr;
*tailptr=newptr;
}
else
printf ("%c not inserted. No memory availale\n");
}
char dequeue (qptr *headptr,qptr*tailptr)
{
char value;
qptr tempPtr;
value =(*headptr)->data;
tempPtr=*headptr;
*headptr=(*headptr)->nextptr;
if (*headptr==NULL)
*tailptr=NULL;
free (tempPtr);
return value;
}
int isempty (qptr headptr)
{
return headptr==NULL;
}
void printqueue (qptr currentptr)
{
if (currentptr==NULL)
{
printf ("Queue is empty");
}
else
{
printf ("The Queue is:\n");
while (currentptr!=NULL)
{
printf ("%c-->",currentptr->data);
currentptr=currentptr->nextptr;
}
printf ("NULL\n\n");
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -