C++ Program to implement a stack using linked list.
/**************************************************************
Author: Arun Vishnu M V
Web: www.arunmvishnu.com
Description: C++ Program to implement a stack using linked list.
***************************************************************/#include<conio.h> #include<iostream.h> #include<process.h> #include<malloc.h> // Creating a NODE Structurestruct node
{int data;struct node *next;};// Creating a class STACKclass stack
{struct node *top;public:
stack()// constructure{
top=NULL;}void push();// to insert an elementvoid pop();// to delete an elementvoid show();// to show the stack};// PUSH Operationvoid stack::push(){int value;struct node *ptr;cout<<"\nPUSH Operation\n";cout<<"Enter a number to insert: ";cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;if(top!=NULL)
ptr->next=top;
top=ptr;cout<<"\nNew item is inserted to the stack!!!";
getch();}// POP Operationvoid stack::pop(){struct node *temp;if(top==NULL){cout<<"\nThe stack is empty!!!";
getch();return;}
temp=top;
top=top->next;cout<<"\nPOP Operation........\nPoped value is "<<temp->data;delete temp;
getch();}// Show stackvoid stack::show(){struct node *ptr1=top;cout<<"\nThe stack is\n";while(ptr1!=NULL){cout<<ptr1->data<<" ->";
ptr1=ptr1->next;}cout<<"NULL\n";
getch();}// Main functionint main(){
clrscr();
stack s;int choice;while(1){cout<<"\n-----------------------------------------------------------";cout<<"\n\t\tSTACK USING LINKED LIST\n\n";cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";cout<<"\nEnter your choice(1-4): ";cin>>choice;switch(choice){case1:
s.push();break;case2:
s.pop();break;case3:
s.show();break;case4:exit(0);break;default:cout<<"Please enter correct choice(1-4)!!";
getch();break;}}return0;}//---------------------- END--------------------
/**************************************************************
Author: Arun Vishnu M V
Web: www.arunmvishnu.com
Description: C++ Program to implement a stack using linked list.
***************************************************************/
#include<conio.h>
#include<iostream.h>
#include<process.h>
#include<malloc.h>
// Creating a NODE Structure
struct node
{
int data;
struct node *next;
};
// Creating a class STACK
class stack
{
struct node *top;
public:
stack() // constructure
{
top=NULL;
}
void push(); // to insert an element
void pop(); // to delete an element
void show(); // to show the stack
};
// PUSH Operation
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation\n";
cout<<"Enter a number to insert: ";
cin>>value;
ptr=new node;
ptr->data=value;
ptr->next=NULL;
if(top!=NULL)
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
getch();
}
// POP Operation
void stack::pop()
{
struct node *temp;
if(top==NULL)
{
cout<<"\nThe stack is empty!!!";
getch();
return;
}
temp=top;
top=top->next;
cout<<"\nPOP Operation........\nPoped value is "<<temp->data;
delete temp;
getch();
}
// Show stack
void stack::show()
{
struct node *ptr1=top;
cout<<"\nThe stack is\n";
while(ptr1!=NULL)
{
cout<<ptr1->data<<" ->";
ptr1=ptr1->next;
}
cout<<"NULL\n";
getch();
}
// Main function
int main()
{
clrscr();
stack s;
int choice;
while(1)
{
cout<<"\n-----------------------------------------------------------";
cout<<"\n\t\tSTACK USING LINKED LIST\n\n";
cout<<"1:PUSH\n2:POP\n3:DISPLAY STACK\n4:EXIT";
cout<<"\nEnter your choice(1-4): ";
cin>>choice;
switch(choice)
{
case 1:
s.push();
break;
case 2:
s.pop();
break;
case 3:
s.show();
break;
case 4:
exit(0);
break;
default:
cout<<"Please enter correct choice(1-4)!!";
getch();
break;
}
}
return 0;
}
//---------------------- END--------------------
you are requested to send me any other linledlist example that do inesrtion first node ,middle, last,delete in first,in middle ,display member function!!1
thank you for your effort!!
oh oh oh oh oh. realy helpfull source code. i love your codes . i am very much thankfull to you. thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks
i am the studnet of computer systems engineering in the university of engineering and technology pehsawar. your source codes are very easy.
in push operation how top is not equal to null only there.becoz u initialised top=null in constructor. so no need of that if condition(top!=null).. this one is correct..
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation\n";
cout<>value;
ptr=new node;
ptr->data=value;
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
getch();
}
you are requested to send me any other linledlist example that do inesrtion first node ,middle, last,delete in first,in middle ,display member function!!1
thank you for your effort!!
THANK YOU FOR YOUR ASSISTANCE
thanks a lot!! i needed these codes!!
“#include<conio.h>
#include<iostream.h>
#include<process.h>
#include<malloc.h> ”
I dont understand whay r u taking” <iostream.h> ”
Plzzz spaciafy……..i m waiting for Good answer…..
c i dont know dis might help……………
but……..according to me………
is used when we r including
“cout” & “cin” operation
perfect program…… thanks ….
Yes. iostream.h is used for using << and >> operators. ie for here for cin and cout
oh oh oh oh oh. realy helpfull source code. i love your codes . i am very much thankfull to you. thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks thanks
i am the studnet of computer systems engineering in the university of engineering and technology pehsawar. your source codes are very easy.
thanks………………..
I think u r a good programmer
cool…………………………….
looks helpful let me try it out……..I’ll be back soon
thnx a lot yar….. it s very helpfull during lab exams!!!!!
Send to an
Some programs……….
implement insert and delete operations on linked list………………
Thanks its to easy to understand..:)
Awesome …
in push operation how top is not equal to null only there.becoz u initialised top=null in constructor. so no need of that if condition(top!=null).. this one is correct..
void stack::push()
{
int value;
struct node *ptr;
cout<<"\nPUSH Operation\n";
cout<>value;
ptr=new node;
ptr->data=value;
ptr->next=top;
top=ptr;
cout<<"\nNew item is inserted to the stack!!!";
getch();
}
good
i dont understand anything
Thanx a lot sir .Your code is perfect. Thank you for your help….
Very short program and simple
congratulations for your effort and best wishes for a great future
please send me the code for create a singly linked list and search for a given node and count the nodes of a linked list.