Wednesday, 9 April 2014

write a program to implement queue using linked list.
#include<stdio.h>
#include<stdlib.h>
void insertion();
void display();
void create_node();
void deletion();
struct link_list
{
  int data;
  struct link_list *next;
}*start=NULL;
typedef struct link_list node;
node *current,*nnew,*temp;
int main()
 {
  int a;
  char ch;
   do
   {
    printf("\n enter your choice\n\n1->insertion\n2->display\n3->deletion\n");
    scanf("%d",&a);
    switch(a)
      {
      case 2:
      display();
       break;
      case 1:
       {
        insertion();
        }
         break;
      case 3:
       {
        deletion();
       }
        break;
      default:
        printf("wrong choice");
     }
    printf("\n \n press any key to continue and press n to quit->");
    scanf("%s",&ch);
   }while(ch!='n');
 return 0;
 }

void insertion()
{
 int i=0;
 printf("\nhow many nodes you want to create->");
 scanf("%d",&i);
 while(i!=0)
  {
  if(start==NULL)
   {
    create_node();
    start=nnew;
    current=start;
    printf("\tnode inserted successfully\n");
   }
  else
    {
       current=start;
       while(current->next!=NULL)
       {
       current=current->next;
       }
       create_node();
       current->next=nnew;
       current=nnew;
       printf("\tinserted successfully\n");
     }
   i--;
  }
}
void create_node()
{
       int k;
       nnew=malloc(sizeof(node));
       nnew->next=NULL;
       printf("enter the data for this node->");
       scanf("%d",&k);
       nnew->data=k;
}
void display()
{
       int c=0,i;
       current=start;
       while(current!=NULL)
{c++;
       current=current->next;
}
   for(i=0;i<c;i++)
   printf("=====");
   printf("\n");
   current=start;
while(current!=NULL)
{
   printf("%d | " ,current->data);
   current=current->next;
}
   printf("\n");
 for(i=0;i<c;i++)
  printf("=====");
}

void deletion()
{
       int d,i;
       printf("enter how many element you want to delete->");
       scanf("%d",&d);
       for(i=0;i<d;i++)
       {
       if(start==NULL)
       {
              start=NULL;
     printf("Queue is empty\n");
       }
       Else
     {
      temp=start;
      start=start->next;
      free(temp);
      printf("data is deleted");
     }
  }
}



No comments:

Post a Comment