Operation on queue [LinkedList Version] - C/C++

A queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as enqueue, and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also implemented, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection.

Implementation of Queue [ Using LinkedList] in 'C'.
Following operations are performed on Queue :
1. Insertion in a queue
2. Deletion in a queue
3. Traversing in a queue



/* queue using linked list*/
#include<stdio.h>
#include<conio.h>
int i;
struct que
{
 int data;
 struct que *ptr;
}*start = NULL, *end = NULL;
 
void disp();
void ins();
void del();
void menu();
int main()
{
 clrscr();
 ins();
 disp();
 getch();
 menu();
 return 0;
}
 
void menu()
{
 int ch;
 clrscr();
 do {
  printf("\n1. Insert\n2. Delete\n3. Display\n\nPress 0 to exit.\n\n"); printf("\nYour Choice :: "); fflush(stdin); scanf("%d", &ch);
  switch (ch) { case 1:ins(); breakcase 2:del(); breakcase 3:disp(); breakdefault:exit(); }
 } while (ch != 0);
}
 
void disp()
{
 struct que *temp;
 printf("\nElements of Queue :: ");
 temp = start;
 while (temp != NULL)
 {
  printf("\n %d , ", temp->data);
  temp = temp->ptr;
 }
}
 
void ins()
{
 int data;
 char choice = 'y';
 struct que *new;
 do
 {
  new = (struct que *)malloc(sizeof(struct que));
  printf("\nEnter Element of Queue :: ");
  scanf("%d", &data); new->data = data; new->ptr = NULL;
  if (start == NULL)
  {
   start = new;
   end = new;
  }
  else
  {
   end->ptr = new;
   end = new;
  }
  fflush(stdin);
  printf("\nDo you want to insert more ? (Y/N) :: ");
  scanf("%c", &choice);
 } while (choice == 'Y' || choice == 'y');
}
 
void del()
{
 char choice = 'y';
 do
 {
  if (start != NULL)
  {
   printf("\nElement Deleted :: %d ", start->data);
   start = start->ptr;
   fflush(stdin);
   printf("\nDo you want to delete more ? (Y/N) :: ");
   scanf("%c", &choice);
  }
  else
  {
   printf("\nError : Queue is empty.");
   fflush(stdin); choice = 'n';
  }
 } while (choice == 'Y' || choice == 'y'); getch();
}


Check out other programs of queue here :
1. Priority Queue in C
2. Circular Queue in C
3. Operation on Queue [ Linked List ]
4. Operation on Queue [ Array ]

2 comments

Hello Jayesh, I have a question about your infix conversion (there's no comment area). First, I really appreciate it...helped make sense of the algorithm, secondly, can you offer pointers on getting the stack list to show the items pushed and popped in and out...it currently just shows the ( and operands. Also, what would be the next process to perform a calculation on the postfix?

Thanks

Reply

Hello Dear.
Thanks a lot for your appreciation.
I have already added comment section there. :)
I have already posted a program for evaluation of PostFix Conversion. Click Here

and Last think. Actually I didn't understand what you really want so you can ask question on that post. I will try to help you my best. :)

Reply

Post a Comment