The linked list is generally pictured as a list of linear nodes that are tethered together somehow. In C/++, you generally have a structure which contains data and a pointer to the next structure container which contains data and a pointer to the next structure container... and so on. The linked list's main advantage is that it doesn't contain data in a contiguous way and rather, a flexible way. This allows for fast insertion and better overall iteration. The linked list is often even used as the basis for other containers (such as queue or stack containers).
Program:
#include<iostream>
#include<windows.h>
using namespace std;
struct Stack{
int data;
Stack *next;
}*top,*temp;
void push(int);
int pop();
int main()
{
system("color ec");
int n;
top=NULL;
cout<<"\n\t\t\tPusing data...";
Sleep(2500);
push(10);
push(20);
push(30);
push(40);
push(50);
cout<<"\n\n\n\t\t\tPoping data...";
Sleep(2000);
cout<<"\n\n";
cout<<"\t\t\t\t"<<pop()<<endl;
cout<<"\t\t\t\t"<<pop()<<endl;
cout<<"\t\t\t\t"<<pop()<<endl;
cout<<"\t\t\t\t"<<pop()<<endl;
cout<<"\t\t\t\t"<<pop()<<endl;
cout<<"\n\n";
return 0;
}
void push(int a)
{
temp=new Stack;
if(temp==NULL)
cout<<"Memory full!!!!!!";
else
{
temp->data=a;
temp->next=NULL;
if(top==NULL)
top=temp;
else
{
temp->next=top;
top=temp;
}
}
}
int pop()
{
int b;
if(top==NULL)
return -1;
else
{
temp=top;
b=top->data;
top=top->next;
delete temp;
return b;
}
}
Output:
0 comments:
Post a Comment