Saturday, 31 December 2011

Implementation of Queues using Linked lists



#include<stdio.h>
#include<stdlib.h>

typedef struct node
{
 int data;
 struct node *link;
}NODE,*NODEPTR;

NODEPTR insert(NODEPTR start)
{
 NODEPTR temp;
 NODEPTR cur=start;
 temp=(NODEPTR)malloc(sizeof(NODE));
 if(temp==NULL)
 {
  printf("Memory allocation not possible\n");
  return start;
 }
 printf("Enter the data\n");
 scanf("%d",&temp->data);
 temp->link=NULL;
 if(start==NULL)
  return temp;
 while(cur->link!=NULL)
  cur=cur->link;
 cur->link=temp;
 return start;
}

NODEPTR del(NODEPTR start)
{
 NODEPTR cur=start;
 if(start==NULL)
 {
 printf("Queue is empty\n");
  return NULL;
 }
 start=start->link;
 printf("Element deleted is %d\n",cur->data);
 free(cur);
 return start;
}

NODEPTR dis(NODEPTR start)
{
 NODEPTR cur=start;
 if(start==NULL)
 {
  printf("Queue is empty\n");
  return;
 }
 for(cur=start;cur!=NULL;cur=cur->link)
  printf("%d\n",cur->data);
}

main()
{
 int ch;
 NODEPTR start=NULL;
 while(1)
 {
  printf("1.Insert 2.Delete 3.Display\n");
  scanf("%d",&ch);
  switch(ch)
 {
  case 1 : start=insert(start);
           break;
  case 2 : start=del(start);
            break;
  case 3 : dis(start);
           break;
  default : return;
 }
}
}


No comments:

Post a Comment