Data Structure Queue Array and Linked List.

This Program is for Queue using Array and Linked List. This is a part of Mumbai University MCA Colleges Data Structure C program MCA Sem 2

# include<stdio.h>
# include<conio.h>
# define MAX 5

void Qlink();
void del();
void display();
void insert();

void Qarray();
void insert1();
void del1();
void disp();

int queue_arr[MAX];
int rear1 = -1;
int front1 = -1;

struct node
{
int info;
struct node *link;
}*front=NULL,*rear=NULL;

main()
{
          int n;
          clrscr();
printf("--------------------------- MENU FOR QUEUE -------------------------------------\n\n");
          while(1)
          {
printf("1.Queue using Array.\n\n2.Queue using Linked list.\n\n");
                   printf("3.Quit.\n\n");
                   printf("Enter your choice : ");
                   scanf("%d",&n);
                   switch(n)
                   {
                             case 1:
                                      Qarray();
                                      break;
                             case 2:
                                      Qlink();
                                      break;
                             case 3:
                                       exit(1);
                             default:
                                      printf("INVALID CHOICE TRY AGAIN!!!!!");
                   }
          }
}

void Qlink()
{
          int choice;
printf("\n\n--------------------------- QUEUE USING LINKED LIST------------\n\n");
          while(1)
          {
printf("\n--------------------- MAIN MENU -----------------------\n");
                   printf("\n1.Insert\n\n");
                   printf("2.Delete\n\n");
                   printf("3.Display\n\n");
                   printf("4.Quit\n\n");
                   printf("Enter your choice : ");
                   scanf("%d", &choice);
                   switch(choice)
                   {
                             case 1:
                                      insert();
                                      break;
                             case 2:
                                      del();
                                      break;
                             case 3:
                                      display();
                                      break;
                             case 4:
                                      exit(1);
                             default :
printf("INVALID CHOICE TRY AGAIN!!!!!!!!!1\n");
                   }
          }
}

void insert()
{
          struct node *tmp;
          int added_item;
          tmp = (struct node *)malloc(sizeof(struct node));
          printf("\n\nInput the element for adding in queue : ");
          scanf("%d",&added_item);
          tmp->info = added_item;
          tmp->link=NULL;
          if(front==NULL)
                   front=tmp;
          else
                   rear->link=tmp;
                   rear=tmp;
}

void del()
{
          struct node *tmp;
          if(front == NULL)
          {
printf("\n\n----------------------------------------------------------\n\n");
                   printf("Queue Underflow........don't delete any more\n");
printf("\n\n----------------------------------------------------------\n\n");
          }
          else
          {
                   tmp=front;
printf("\n\n----------------------------------------------------------\n\n");
                   printf("Deleted element is %d\n",tmp->info);
printf("\n\n----------------------------------------------------------\n\n");
                   front=front->link;
                   free(tmp);
          }
}

void display()
{
          struct node *ptr;
          ptr = front;
          if(front == NULL)
          {
printf("\n\n----------------------------------------------------------\n\n");
                   printf("Queue is empty\n");
printf("\n\n----------------------------------------------------------\n\n");
          }
          else
          {
printf("\n\n--------------------- QUEUE ELEMENTS -----------------------\n\n");
                   while(ptr != NULL)
                   {
                             printf("%d ",ptr->info);
                             ptr = ptr->link;
                   }
                   printf("\n");
          }
}

void Qarray()
{
          int choice;
printf("\n--------------------------- QUEUE USING ARRAY -------------\n\n");
          while(1)
          {
printf("\n--------------------- MAIN MENU -----------------------\n");
                   printf("\n1.Insert\n\n");
                   printf("2.Delete\n\n");
                   printf("3.Display\n\n");
                   printf("4.Quit\n\n");
                   printf("Enter your choice : ");
                   scanf("%d",&choice);
                   switch(choice)
                   {
                             case 1 :
                                      insert1();
                                      break;
                             case 2 :
                                      del1();
                                      break;
                             case 3:
                                      disp();
                                      break;
                             case 4:
                                      exit(1);
                             default:
printf("INVALID CHOICE TRY AGAIN!!!!!!!\n\n");
                   }
          }
}

void insert1()
{
          int added_item;
          if (rear1==MAX-1)
          {
printf("\n\n----------------------------------------------------------\n\n");
                   printf("Queue Overflow.........don't insert any more\n");
printf("\n\n-------------------------------------------------------------\n\n");
          }
          else
          {
                   if (front1==-1)
                   front1=0;
                   printf("\nInput the element for adding in queue : ");
                   scanf("%d", &added_item);
                   rear1=rear1+1;
                   queue_arr[rear1] = added_item ;
          }
}

void del1()
{
          if (front1 == -1 || front1 > rear1)
          {
                   printf("\n\n---------------------------------------------------------------\n\n");
                   printf("Queue Underflow.........don't delete any more\n");
printf("\n\n--------------------------------------------------------------\n\n");
                   front1=-1;
                   return ;
          }
          else
          {
printf("\n\n----------------------------------------------------------------\n\n");
printf("Element %d is deleted from Queue \n", queue_arr[front1]);
printf("\n\n---------------------------------------------------------------\n\n");
                   front1=front1+1;
          }
}

void disp()
{
          int i;
          if (front1 == -1)
          {
printf("\n\n----------------------------------------------------------------\n\n");
                   printf("Queue is empty\n");
printf("\n\n----------------------------------------------------------------\n\n");
          }
          else
          {
printf("\n---------------------------------- Queue elements ---------------------\n\n");
                   for(i=front1;i<= rear1;i++)
                   printf("%d ",queue_arr[i]);
                   printf("\n");
          }
}


Hope this Program is useful to you in some sense or other. Keep on following this blog for more Mumbai University MCA College Programs. Happy Programming and Studying.

No comments:

Post a comment