Tuesday, November 22, 2011

4 Queue - Linked List


#include<iostream>
#include<stdlib.h>

using namespace std;

struct node
{
int data;
struct node *link;
};
struct node *cur,*first,*last;

void insert();
void delte();
void display();

void insert()
{
 if(first==NULL)
 {
 cout<<"\nENTER THE FIRST ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 first=cur;
 last=cur;
 }
 else
 {
 cout<<"\nENTER THE NEXT ELEMENT: ";
 cur=(struct node *)malloc(sizeof(struct node));
 cin>>cur->data;
 cur->link=NULL;
 last->link=cur;
 last=cur;
 }
}

void delte()
{
 if(first==NULL)
 {
 cout<<"\t\nQUEUE IS EMPTY\n";
 }
 else
 {
 cur=first;
 first=first->link;
 cur->link=NULL;
 cout<<"\n DELETED ELEMENT IS:";
 cout<<cur->data;
 free(cur);
 }
}

void display()
{
 if(first==NULL)
 cout<<"\nQueue Empty";
 else
 {
 cur=first;
 cout<<"\n";
  while(cur!=NULL)
  {
  cout<<"\t"<<cur->data;
  cur=cur->link;
  }
 }
}

int main()
{
 int ch;
 do
 {
 cout<<"\n 1.INSERT \n 2.DELETE  \n 3.Display \n 4.EXIT ";
 cout<<"\nENTER YOUR CHOICE : ";
 cin>>ch;

  switch(ch)
  {
  case 1: insert(); break;
  case 2: delte(); break;
  case 3: display(); break;
  case 4: exit(0);
  default:cout<<"\nInvalid Choice";
  }
 }while(ch!=4);
return 0;
}

No comments:

Post a Comment