Thread: Queue (First in, First Out)

  1. #1
    Registered User
    Join Date
    Jan 2003
    Posts
    5

    Queue (First in, First Out)

    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.

  2. #2
    Registered User Sargnagel's Avatar
    Join Date
    Aug 2002
    Posts
    166
    It must be:
    Code:
    int main()
    {
             ...
              return 0;
    }
    Refering to the ANSI C standard fflush(stdin) is undefined. fflush() is only defined for output streams. Use something like this instead:
    Code:
    void tidy (FILE *in)
    {
      	int c;
      	
      	while ( (c = fgetc(in)) != '\n' && c != EOF);
    }
    NewNode = (struct StuRec *)malloc(sizeof(struct StuRec));
    Typecasting malloc/calloc/realloc is not neccessary if you include <stdlib.h>. Always check if malloc was successful. Use this instead:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    if ( (NewNode = malloc(sizeof(*NewNode))) == NULL )
    {
            printf("\nmemory allocation error\n");
             /*... free queue ...*/
            exit(1); // end program
    }
    Maintaining the code is now much easier. Now you only need to change the type of NewNode in its declaration.
    Last edited by Sargnagel; 01-24-2003 at 01:56 PM.

  3. #3
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    Try this.
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    #include <conio.h>
    #include <ctype.h>
    
    void Enqueue();
    void Renqueue();
    void ShowQueue();
    void Continue();
    
    struct StuRec
    {      int StuRef;
    		 char StuName[25];
    		 struct StuRec *next;
    }   *QueueHead = NULL, *QueueTail = NULL;
    
    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 = malloc(sizeof(struct StuRec));
              NewNode->next = NULL;
    	  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;
                      if (QueueHead == NULL)
                         QueueTail = NULL;
    		  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();
     }

  4. #4
    Registered User
    Join Date
    Nov 2002
    Posts
    491
    Just a note on style. Usually when you write an ADT, or any type of type, you want to make it and its functions as generic as possible. You might want to consider moving the printf's in your functions to another one and then caling Enqueue from that. That way if you do not want to prompt the uesr for input, when you call Enqueue they are not prompted. This would be handy, for example, when writing a program that requires a queue but is working as a server where it has no interaction with the user through stdin, stdout.

    Just some food for thought.

  5. #5
    Registered User
    Join Date
    Jan 2003
    Posts
    5
    First of all i would like to thank all of you for helping with my problem(s) and your comments/tips on style, It's Very much appreciated. ThankYou.


    Thanks,
    Regards,
    Kevin

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with FIFO QUEUE
    By jackfraust in forum C++ Programming
    Replies: 23
    Last Post: 04-03-2009, 08:17 AM
  2. Fixing my program
    By Mcwaffle in forum C Programming
    Replies: 5
    Last Post: 11-05-2008, 03:55 AM
  3. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 09:09 PM
  4. help with queues
    By Unregistered in forum C Programming
    Replies: 3
    Last Post: 05-21-2002, 11:39 AM
  5. queue help
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-29-2001, 09:38 AM