Logo 
Search:

C++ Programming Articles

Submit Article
Home » Articles » C++ Programming » Data File StructureRSS Feeds

Program to create a queue using dynamic memory allocation

Posted By: Easy Tutor     Category: C++ Programming     Views: 9159

Write a program to create a queue using dynamic memory allocation

Code for Program to create a queue using dynamic memory allocation in C++ Programming

# 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);
  }
 }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to create a queue using dynamic memory allocation is from United States. Easy Tutor says

Hello Friends,

I am Free Lance Tutor, who helped student in completing their homework.

I have 4 Years of hands on experience on helping student in completing their homework. I also guide them in doing their final year projects.

I have share many programs on this website for everyone to use freely, if you need further assistance, than please contact me on easytutor.2ya [at the rate] gmail [dot] com

I have special discount scheme for providing tutor services. I am providing tutor service to students from various contries, currently most of my students are from United States, India, Australia, Pakistan, Germany, UK and Canada.

I am also here to expand my technical network to receive more opportunity in my career, make friends to help them in resolving their technical problem, learn and share my knowledge, If you like to be my friend, Please send me friend request.

Thanks,
Happy Programming :)

 
View All Articles

 

Other Interesting Articles in C++ Programming:


 
Please enter your Comment

  • Comment should be atleast 30 Characters.
  • Please put code inside [Code] your code [/Code].

 
No Comment Found, Be the First to post comment!