Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

STACK: It is a data structure in which insertion and deletion of element takes

place at one end only know as TOP.


PROGRAM
#include<iostream>
using namespace std;
struct element
{
int data;
struct element *next;
};
typedef struct element node;
class stack
{
node *top;
void display();
void push();
void pop();
public: stack();
};
stack::stack()
{
top=NULL;
int c;
do
{ cout<<"\n1: Insertion 2: Deletion 0: Exit";
cin>>c;
switch(c)
{
case 1: push(); display(); break;
case 2: pop(); display(); break;
}
}while(c);
}
void stack::display()
{
node *ptr=top;
if(ptr==NULL) cout<<"\nStack Empty...";
else
while(ptr!=NULL)
{
cout<<ptr->data<<"\n";
ptr=ptr->next;
}

}
void stack::push()
{
node *temp;
cout<<"Enter the value in new node :";
temp=new node;
cin>>temp->data;
temp->next=NULL;
if(top==NULL) top=temp; else { temp->next=top; top=temp;}
}

void stack::pop()
{
node *temp;
if( top==NULL) cout<<"\nStack Empty...";
else
{
temp=top;
top=top->next;
delete temp;
}
}

int main()
{
stack z;
return 0;
}
QUEUE: It is a data structure in which all insertion of elements takes place at
one end known as REAR and all deletion of elements are done at
another end known as FRONT.
PROGRAM
#include<iostream>
using namespace std;
struct element
{
int data;
struct element *next;
};
typedef struct element node;
class queue
{
node *front,*rear;
void display();
void insertion();
void deletion();
public: queue();
};
queue::queue()
{
front=rear=NULL;
int c;
do
{ cout<<"\n1: Insertion 2: Deletion 0: Exit";
cin>>c;
switch(c)
{
case 1: insertion(); display(); break;
case 2: deletion(); display(); break;
}
}while(c);
}

void queue::display()
{
node *ptr=front;
if(front==NULL&& rear==NULL) cout<<"\nQueue Empty...";
else
while(ptr!=NULL)
{
cout<<ptr->data<<"\t";
ptr=ptr->next;
}
}
void queue::insertion()
{
node *temp;
cout<<"Enter the value in new node :";
temp=new node;
cin>>temp->data;
temp->next=NULL;

if(front==NULL && rear==NULL) front=rear=temp;


else
{ rear->next=temp; rear=temp;}
}

void queue::deletion()
{
node *temp;
if( front==NULL) rear=NULL;
else
{
temp=front;
front=front->next;
delete temp;
}
}

int main()
{
queue z;
return 0;
}
SINGLY LINKED LIST: It is a data structure which consists of interconnected nodes,
where in each node consists of multiple data fields and one
address field which points to the next node.
PROGRAM
#include<iostream>
using namespace std;
struct element
{
int data;
struct element *next;
};
typedef struct element node;

class list
{
node *start;
void creation();
void display();
void insertion();
void deletion();
public: list();
};
list::list()
{
start=NULL;
int c;
do
{
cout<<"\n1: creation 2: insertion 3:deletion 0:exit";
cin>>c;
switch(c)
{
case 1: creation(); display(); break;
case 2: insertion(); display(); break;
case 3: deletion(); display(); break;
}
}while(c);
}

void list::creation()
{
int i,n;
node *ptr,*temp;
cout<<"\n Enter the total no. of nodes :";
cin>>n;
for(i=1;i<=n;i++)
{
temp=new node;
cout<<"\n Enter the value in node : ";
cin>>temp->data;
temp->next=NULL;

if(i==1) start=ptr=temp;
else
{
ptr->next=temp;
ptr=temp;
}
}

void list::display()
{
node *ptr=start;
while(ptr!=NULL)
{
cout<<ptr->data<<" ";
ptr=ptr->next;
}
}

void list::insertion()
{
int i,pos;
node *ptr=start,*temp;
cout<<"\nEnter the position: ";
cin>>pos;
cout<<"Enter the value in new node :";
temp= new node;
cin>>temp->data;
temp->next=NULL;

for(i=1;i<=pos-2;i++)
ptr=ptr->next;

if (pos==1)
{ temp->next=start; start=temp;}
else
{temp->next=ptr->next; ptr->next=temp;}
}
void list::deletion()
{
int i,pos;
node *ptr=start,*temp;
cout<<"\nEnter the position: ";
cin>>pos;
for(i=1;i<=pos-2;i++)
ptr=ptr->next;

if (pos==1)
{ temp=start; start=start->next;}
else
{temp=ptr->next; ptr->next=temp->next;}
free(temp);
}

int main()
{
list z;
return 0;
}

DOUBLY LINKED LIST: It is a data structure which consists of interconnected


nodes, where in each node consists of multiple data fields
and two address field which points to the previous node
and the next node.
CIRCULAR LINKED LIST: It is a data structure which consists of interconnected
nodes, where in each node consists of multiple data fields
and one address field which points to the next node. The
last node points to the first node.

You might also like