Implementation of Stack using link list
Do you remember that we learn about the Link list in our last tutorial as well as Stack using Array.today we will discussed “Implementation of Stack using Link list“. I always tell you about the major difference between Array and Link list in our Last tutorial But i will do this again.
The major difference between Array and Link list is that Array is not assign the memory dynamically and on the other hand Link list is use to assign the dynamically. Dynamically means that “between the Run time”.
The other application of the Stack is same just like that Stack is the LIFO(Last In First Out),Non primitive Data Structure ,ADT(Abstract Data Type), Push And Pop Function etc…
The Stack using Link List just Like as : –
The Program of the Stack Using Link list is as follows : —
#include<iostream>
using namespace std;
struct Node
{ int data;
struct Node *next;
};
class Stack
{ private:
struct Node *p,*top;
public:
Stack()
{ top=NULL;
}
int tra()
{ p=top;
while(p!=NULL)
{ cout<<p->data<<endl;
p=p->next;
}
}
int push()
{ p=new Node;
cout<<“Enter the value”;
cin>>p->data;
if(top==NULL)
{ p->next=NULL;
top=p;
}
else
{ p->next=top;
top=p;
}
}
int pop()
{ if(top==NULL)
{ cout<<“nntt Stack is Emptyt”;
}
else
{ top=top->next;
}
}
};
int main()
{ char t;
Stack a;
do
{ cout<<“Want to insert the data and delete the Data in Stack [‘i’,’d’]t”;
cin>>t;
switch(t)
{ case’i’:
{ a.push();
a.tra();
break;
}
case’d’:
{ a.pop();
a.tra();
break;
}
}
cout<<“t Want to perform more operations [y/N]t”;
cin>>t;
}while(t==’y’);
}
I will upload a new tutorial of Queue using Link list Insha_Allah
i hope this is helpful to you.
you may like to read 5th and 8th Class Result Declared – Check Your Result Now.