0% found this document useful (0 votes)
18 views

Data Struction

The document discusses different data structures including stack and queue implemented using arrays and linked lists. It provides code implementations for push, pop, insert, delete and display operations on stack and queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

Data Struction

The document discusses different data structures including stack and queue implemented using arrays and linked lists. It provides code implementations for push, pop, insert, delete and display operations on stack and queue.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

Data Structure

/*Stack using Array*/


#include<iostream>
using namespace std;
#define max 15
int ch,n,top,x,i,stack[max];
void push();
void pop();
void disp();
int main()
{
top=-1;

cout<<"\nenter size of stack:";


cin>>n;
cout<<"\n----STACK OPERATIONS using array:----";
do{
cout<<"\n1.push\t2.pop\t3.display\t4.exit";
cout<<"\nenter your choice:";
cin>>ch;
switch(ch)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
disp();
break;
}
case 4:
{
cout<<"\nexit point!";
break;
}
default:
{
cout<<"\nplease enter a valid choice(1/2/3/4)!";
break;
}
}
}while(ch<4);
return 0;
}
void push()
{
if(top>=n-1)
{
cout<<"\nSTACK is full!";
}
else
{
cout<<"\nenter a value to be pushed:";
cin>>x;
top++;
stack[top]=x;
}
}
void pop()
{
if(top<=-1)
{
cout<<"\nSTACK is underflow!";
}
else
{
cout<<"\nthe popped element is:"<<stack[top];
top--;
}
}
void disp()
{
if(top>=0)
{
cout<<"\nthe elements in STACK are:\n";
for(i=top;i>=0;i--)
{
cout<<"\t"<<stack[i];
}
cout<<"\npress next choice!";
}
else
{
cout<<"\nthe STACK is empty!";
}
}
/*Stack using linked list*/

#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;
}
}

You might also like