1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124
|
#include<stdio.h>
#include<iostream>
#define MAX 30
using namespace std;
typedef struct pqueue
{
char str[MAX];
int priority;
pqueue* next;
} pqueue;
int enqueuepriority(pqueue *&pq, char str[MAX], int priority)
{
if(priority>0 && pq != NULL)
{
pqueue *p=(pqueue*)malloc(sizeof(pqueue));
strcpy( p->str, str );
p->priority=priority;
p->next=pq->next;
pq->next=p;
return 1;
}
else
{
pqueue *p=(pqueue*)malloc(sizeof(pqueue));
strcpy( p->str, str );
p->priority=priority;
pq = p ;
return 1;
}
return 0;
}
char* dequeue(pqueue *&pq)
{
pqueue *p;
char str[MAX];
memset( str , 0 , sizeof ( str) / sizeof (char) );
cout<<"\n Enter the name ";
cin>>str;
if(pq !=NULL)
{
pqueue *p=(pqueue*)malloc(sizeof(pqueue));
strcpy( p->str, str );
p->priority=priority;
p->next=pq->next;
pq->next=pq->next->next;
}
return (char*)&str ;
}
int main()
{
int re=0,t=1;
static char* str=new char[MAX];
int priority=0;
cout<<"(1) Enqueue (single)"<<endl;
cout<<"(2) Enqueue (multiple)"<<endl;
cout<<"(3) Dequeue (single)"<<endl;
cout<<"(4) Dequeue (all)"<<endl;
cout<<"(5) Quit"<<endl;
pqueue *pq=(pqueue*)malloc(sizeof(pqueue));
pq->next=NULL;
while(re!=5)
{
cout<<"Choose an action:";
cin>>re;
switch(re)
{
case 1:
cout<<"Enter a name to save and its priority"<<endl;
scanf("%s%d",str,&priority);
if(enqueuepriority(pq,str,priority))
else
cout<<"Input Error";
break;
case 2:
cout<<"Enter names to save and their priority. Enter “done” to quit"<<endl;
do
{
scanf("%s",str);
if(strcmp(str,"done")!=0)
{
scanf("%d",&priority);
enqueuepriority(pq,str,priority);
}
else t=0;
}while(t==1);
break;
case 3:
str=dequeue(pq);
if(str=="")
cout<<"NULL"<<endl;
else
printf("%s\n",str);
break;
case 4:
while(pq->next!=NULL)
{
printf("%s\n",dequeue(pq));
}
break;
case 5: break;
default:
cout<<"INPUT ERROR!!"<<endl;
break;
}
}
delete [] str;
while( pq!= NULL )
{
pqueue * temp = pq;
pq = pq->next;
delete temp;
}
return 0;
}
|