C++ Programming Blog

 
 
 

Image

# include <iostream.h>
# include <process.h>
# include <conio.h>
# include <malloc.h>

struct link_list
{
 int no;
 struct link_list *next;
};
class queue
{
 link_list *list,*head;
 public:
    queue()
    {
     head=NULL;
     free(list);
    }
    void insert();
    void display();
    void delete_element();
};

void queue :: insert()
{
  link_list *newnode;
  newnode=new link_list;
  cout<<\"Enter Number to Insert :\";
  cin>>newnode->no;
  list=head;
  if(head==NULL)
  {
    head=newnode;
    newnode->next=NULL;
  }
  else
  {
    newnode->next=list;
    list=newnode;
    head=newnode;
  }
}

void queue :: display()
{
 cout<<endl;
  if(head==NULL)
 {
  cout<<\"Empty Queue !!!\";
  return;
 }
 list=head;
 while(list!=NULL)
 {





  cout<<list->no<<\"\\t\";
  list=list->next;
 }
}

void queue :: delete_element()
{
 cout<<endl;
 if(head==NULL)
 {
  cout<<\"Empty Queue !!!\";
  return;
 }
 else
 {
  list=head;

  if(list->next==NULL)
  {
    cout<<\"Deleted Element : \"<<list->no<<endl;
    head=NULL;
    delete(list);
    list=NULL;
    return;
  }
  while(list->next->next!=NULL)
  {
   list=list->next;
  }
  cout<<\"Deleted Element : \"<<list->next->no<<endl;
  delete(list->next);
  list->next=NULL;
 }
}
int display_menu()
{
 int ch;
 clrscr();
 cout<<endl;
 cout<<\"[ 1 ] Insert\"<<endl;
 cout<<\"[ 2 ] Delete\"<<endl;
 cout<<\"[ 3 ] Display\"<<endl;
 cout<<\"[ 4 ] Exit\"<<endl;
 cout<<\"Enter your choice :\";
 cin>>ch;
 return ch;
}
void main()
{
queue q1;
 while(1)
 {
  switch(display_menu())
  {
   case 1:q1.insert();
      break;
   case 2:q1.delete_element();
      getch();
      break;
   case 3:q1.display();
      getch();
      break;
   case 4: exit(1);
  }
 }
}

 
 
Didn't find what you were looking for? Find more on Program to create a queue using dynamic memory allocation