Logo 
Search:

C++ Programming Articles

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

Program to create a stack using dynamic memory allocation

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

Write a program to create a stack using dynamic memory allocation

Code for Program to create a stack 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 stack
{
 link_list *list,*head;
 public:
    stack()
    {
     head=NULL;
     free(list);
    }
    void push();
    void peep();
    void pop();
};

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

void stack :: peep()
{
 cout<<endl;
  if(head==NULL)
 {
  cout<<"Empty Stack !!!";
  return;
 }
 list=head;








 while(list!=NULL)
 {
  cout<<list->no<<"\t";
  list=list->next;
 }
}

void stack :: pop()
{
 cout<<endl;
 if(head==NULL)
 {
  cout<<"Empty Stack !!!";
  return;
 }
 else
 {
  cout<<"Top : "<<head->no<<endl;
  head=head->next;
 }
}

int display_menu()
{
 int ch;
 clrscr();
 cout<<endl;
 cout<<"[ 1 ] Push"<<endl;
 cout<<"[ 2 ] Pop"<<endl;
 cout<<"[ 3 ] Peep"<<endl;
 cout<<"[ 4 ] Exit"<<endl;
 cout<<"Enter your choice :";
 cin>>ch;
 return ch;
}
void main()
{
 stack s1;
 while(1)
 {
  switch(display_menu())
  {
   case 1:s1.push();
      break;
   case 2:s1.pop();
      getch();
      break;
   case 3:s1.peep();
      getch();
      break;
   case 4: exit(1);
  }
 }
}
  
Share: 



Easy Tutor
Easy Tutor author of Program to create a stack 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

 
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!