Data Struction
Data Struction
#include<iostream.h>
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
void push();
void pop();
void disp();
struct node
{
int val;
struct node *next;
}*head;
void main()
{
int ch;
clrscr();
do
{
cout<<"\nStack operations are:\n1.push 2.pop 3.display 4.exit";
cout<<"\nenter your option:";
cin>>ch;
switch(ch)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
disp();
break;
}
case 4:
{
cout<<"\nexit point!";
break;
}
default:
{
cout<<"\nenter valid choice b/w 1 to 4!";
break;
}
}
}while(ch!=4);
getch();
}
void push()
{
int x;
struct node *pt=(struct node*)malloc(sizeof(struct node));
if(pt==NULL)
{
cout<<"\nnot able to push element!";
}
else
{
cout<<"\nenter a value to be pushed:";
cin>>x;
if(head==NULL)
{
pt->val=x;
pt->next=NULL;
head=pt;
}
else
{
pt->val=x;
pt->next=head;
head=pt;
}
cout<<"\nitem pushed!";
}
}
void pop()
{
struct node *ptr=(struct node*)malloc(sizeof(struct node));
if(head==NULL)
{
cout<<"\nunderflow!";
}
else
{
ptr=head;
head=head->next;
free(ptr);
cout<<"\nitem popped!";
}
}
void disp()
{
int i;
struct node *pt1=(struct node*)malloc(sizeof(struct node));
pt1=head;
if(pt1==NULL)
{
cout<<"\nunderflow!";
}
else
{
cout<<"\nprinting stack elements:";
while(pt1!=NULL)
{
cout<<"\n"<<pt1->val;
pt1=pt1->next;
}
}
}
/*Queue using array*/
#include< iostream.h>
#include<conio.h>
#define max 3
void insert();
void delete();
void disp();
int queue[max],front=-1,rear=-1;
int cou=max;
int in=0;
void main()
{
int ch;
clrscr();
do
{
cout<<"\n1.insert\t2.delete\t3.display\t4.exit";
cout<<"\nenter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
if(cou!=0)
{
insert();}
else{
cout<<"memory not avail!";
}
break;
}
case 2:
{
delete();
break;
}
case 3:
{
disp();
break;
}
case 4:
{
cout<<"\nexit point!";
break;
}
default:
{
cout<<"\nenter the correct choice!";
}
}
}while(ch!=4);
}
void insert()
{
int x;
if(rear==max-1)
{
cout<<"nqueue is overflow!";
}
else{
if(front==-1)
{front=0;}
cout<<"\nenter the value to insert in queue:";
cin>>x n;
rear+=1;
queue[rear]=x;
cout<<"\ninsertion successful!";}
}
void delete()
{
if(front==-1||front>rear){
cout<<"\nqueue is underflow";
else{
cout<<"\nelement deleted is:%d",queue[front];
front-=1;
}
}
void disp()
{
int i;
if(front==-1)
{
cout<<"\nqueue is empty!";
}
else{
cout<<"\nthe elements in queue are:\n";
for(i=front;i<=rear;i++)
{
cout<<queue[i];
cout<<"\t";
}
}
}
/*Queue using linked list*/
#include< iostream.h>
#include<conio.h>
struct node
{
int data;
struct node *next;
}*front=NULL,*rear=NULL;
void enque(int);
void deque();
void disp();
void main()
{
int ch,val;
clrscr();
do{
cout<<"\n1.insert\t2.delete\t3.display\t4.exit";
cout<<"\nenter your choice:";
cin>>ch;
switch(ch)
{
case 1:{
cout<<"\nenter the value to insert:";
cin>>val;
enque(val);
break;}
case 2:{
deque();
break;}
case 3:{
disp();
break;}
case 4:{
cout<<"\nexit point!";
break;}
default:{
cout<<"\nplease enter a valid choice!";
}
}
}while(ch!=4);
getch();
}
void enque(int val)
{
struct node *newn=(struct node*)malloc(sizeof(struct node));
newn->data=val;
newn->next=NULL;
if(front==NULL)
{front=rear=newn;}
else{
rear->next=newn;
rear=newn;
}
cout<<"\ninsertion successful!";
}
void deque()
{
struct node *pt;
if(front==NULL)
{
cout<<"\nthe queue elements are underflow!";
}
else{
pt=front;
front=front->next;
cout<<"\ndeleted element is:%d",pt->data;
free(pt);
}
}
void disp()
{
struct node *ptr;
if(front==NULL)
{
cout<<"\nqueue is empty!";
}
else{
ptr=front;
while(ptr->next!=NULL)
{
cout<<"%d\t"<<ptr->data;
ptr=ptr->next;
}
cout<<"%d->NULL"<<ptr->data;
}
}