Hi all, this is my first post on Cprogramming forums, i love this place. I'm reading a book and i've came accross a few examples that i'm struggling with. Both problems are stack & queue, but i seem to have got the hang of the stack problem. Underneath is my problem which is the Queue problem, It seems to be sort of working, but printing out garbarge...Would somebody be kind enough to fix my problem Or give me an example of the Queue.
Code:
#include <stdio.h>
void Enqueue();
void Renqueue();
void ShowQueue();
void Continue();
struct StuRec
{ int StuRef;
char StuName[25];
struct StuRec *next;
} *QueueHead, *QueueTail;
void main()
{ char Opt;
do {
clrscr();
gotoxy(10,5); printf("(E)nqueue");
gotoxy(10,6); printf("(D)e-enqueue");
gotoxy(10,8); printf("(S)how Queue");
gotoxy(10,7); printf("(Q)uit\n\n");
printf("Enter Your Choice: ");
fflush(stdin); scanf("%c", &Opt);
if(toupper(Opt)=='E') Enqueue();
else if (toupper(Opt)=='D') Renqueue();
else if (toupper(Opt)=='S') ShowQueue();
} while (toupper(Opt)!='Q');
}
void Enqueue()
{ struct StuRec *NewNode;
NewNode = (struct StuRec *)malloc(sizeof(struct StuRec));
printf("\nPush node to stack.\n");
printf("Enter student ref: ");
fflush(stdin); scanf("%d", &NewNode->StuRef);
printf("Enter student name: ");
fflush(stdin); scanf("%s", NewNode->StuName);
if(QueueTail!=NULL)
{
QueueTail->next=NewNode;
QueueTail=NewNode;
}
else
{
QueueHead=NewNode;
QueueTail=NewNode;
}
ShowQueue();
}
void Renqueue()
{ struct StuRec *Temp;
if(QueueTail==NULL)
{printf("\nCannot pop emty stack");
gotoxy(12,24); printf("Press any key to continue . ."); getch();
}
else
{printf("\nPopped: Student Ref:%d; Student Name %s\n",
QueueHead->StuRef, QueueHead->StuName);
Temp=QueueHead;
QueueHead=QueueHead->next;
free(Temp);
ShowQueue();
}
}
void ShowQueue()
{ struct StuRec *ShowPtr;
ShowPtr = QueueHead;
while(ShowPtr!=NULL)
{ printf("\nStudent ref %d, student name %s",
ShowPtr->StuRef, ShowPtr->StuName);
ShowPtr = ShowPtr->next;
}
Continue();
}
void Continue()
{
gotoxy(24,25); printf("Press any key to continue");
getch();
}
Thank You all.