Programmer

Thursday, 21 June 2018

Singly Linked List in Stack. (Data Structure)

//Example of stack
#include<iostream.h>
#include<conio.h>

struct Node
{
int data;
Node *next;
}*f,*ptr,*start=NULL;

void AddNode()
{
ptr=new Node;
int d;
cout<<"Enter Data : ";
cin>>d;
ptr->data=d;
if(start==NULL)
{
start=ptr;
}
else
{
ptr->next=start;
start=ptr;
}
}

void Display()
{
f=start;
if(f==NULL)
{
cout<<"Stack is Empty...\n";
}
else
{
while(f!=NULL)
{
cout<<f->data<<"-->";
f=f->next;
}
cout<<"<--Done\n";
cout<<"List Displayed....\n";
}
}

void Delete()
{
f=start;
if(f==NULL)
{
cout<<"Stack is Empty...\n";
}
else
{
start=start->next;
cout<<"The Data : --> "<<f->data<<" is deleted...\n";
delete f;
}
}

void main()
{
int ch;
do
{
clrscr();
cout<<"0. Exit\n";
cout<<"1. Add Node to Top\n";
cout<<"2. Display All Nodes from Top\n";
cout<<"3. Delete Node from Top\n";
cout<<"Enter your choice: ";
cin>>ch;
switch(ch)
{
case 1: AddNode(); break;
case 2: Display(); break;
case 3: Delete(); break;
}
getch();
}while(ch);
}

No comments:

Post a Comment

Program To Print Particular Line From File.

#include<fstream.h> #include<conio.h> #include<stdlib.h> void main() { ifstream fin; int count=0,num,l=1; char ...