DATA STRUCTURE CODE-3
STACK USING ARRAY:
#include <iostream>
#define N 5
using namespace std;
int stack[N];
int top{-1};
void push(int d)
{
if(top > N-1)
{
cout<<"!!! OVERFLOW CONDITION !!!"<<endl;
}
else
{
top++;
stack[top] = d;
}
}
void pop()
{
int item{};
if(top == -1)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else
{
item = stack[top];
top--;
}
cout<<"The popped item is: "<<item<<" from position "<<top+1<<" ."<<endl;
}
void peek() //to get the top most element of the stack without removing the element
{
if(top == -1)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else
{
cout<<"The top most element is: "<<stack[top]<<" at position "<<top<<" ."<<endl;
}
}
void display()
{
for(int i{top};i>=0;i--)
{
cout<<"Elements is: "<<stack[i]<<endl;
}
}
int main()
{
char ch1{};
do
{
cout<<"============================"<<endl;
cout<<"1. Push operation"<<endl;
cout<<"2. Pop operation"<<endl;
cout<<"3. Peek operation"<<endl;
cout<<"4. Display elements"<<endl;
cout<<"============================"<<endl;
int op{};
cout<<"Which operation you want to perform (1/2/3/4): ";
cin>>op;
switch(op)
{
case 1:
{
int data;
cout<<"Enter the value to push: ";
cin>>data;
push(data);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
default:
{
cout<<"Invalid option choose correct option"<<endl;
}
}
cout<<"Do you want to continue: ";
cin>>ch1;
}while(ch1 == 'y');
return 0;
}
STACK USING LINKED LIST:
#include <iostream>
using namespace std;
int top{-1};
struct node
{
int data{};
node *next{NULL};
};
node *head{NULL},*temp{NULL};
void push(int d)
{
top++;
node *new_node;
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
}
else
{
new_node -> next = head;
head = new_node;
}
}
void pop()
{
if(top < 0)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else
{
cout<<"Element popped is: "<<head -> data<<" from position "<<top+1<<endl;
head = head -> next;
top--;
}
}
void peek()
{
cout<<"The top most elements is: "<<head -> data<<" on position "<<top+1<<endl;
}
void display()
{
node *temp{};
temp = head;
while(temp != 0)
{
cout<<"Element is :"<<temp -> data<<endl;
temp = temp -> next;
}
}
int main()
{
char ch1{};
do
{
cout<<"============================"<<endl;
cout<<"1. Push operation"<<endl;
cout<<"2. Pop operation"<<endl;
cout<<"3. Peek operation"<<endl;
cout<<"4. Display elements"<<endl;
cout<<"============================"<<endl;
int op{};
cout<<"Which operation you want to perform (1/2/3/4): ";
cin>>op;
switch(op)
{
case 1:
{
int data;
cout<<"Enter the value to push: ";
cin>>data;
push(data);
break;
}
case 2:
{
pop();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
default:
{
cout<<"Invalid option choose correct option"<<endl;
}
}
cout<<"Do you want to continue (y/n): ";
cin>>ch1;
}while(ch1 == 'y');
return 0;
}
QUEUE USING ARRAY:
#include <iostream>
#define N 5
using namespace std;
int queue[N];
int front{-1},rear{-1};
void enqueue(int d)
{
if(rear == N-1)
{
cout<<"!!! OVERFLOW CONDITION !!!"<<endl;
}
else if(front == -1 && rear == -1)
{
front++;
rear++;
queue[rear] = d;
}
else
{
rear++;
queue[rear] = d;
}
}
void dequeue()
{
if(front == -1 && rear == -1)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else if(front == rear)
{
cout<<"The element dequeued is: "<<queue[front]<<endl;
front = -1;
rear = -1;
}
else
{
cout<<"The element dequeued is: "<<queue[front]<<endl;
front++;
}
}
void peek() //to get the top most element of the stack without removing the element
{
if(front == -1)
{
cout<<"No elements in queue"<<endl;
}
else
{
cout<<"The front element is: "<<queue[front]<<endl;
}
}
void display()
{
if(front == -1 && rear == -1)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else
{
for(int i{front};i<=rear;i++)
{
cout<<"Element is: "<<queue[i]<<endl;
}
}
}
int main()
{
char ch1{};
do
{
cout<<"============================"<<endl;
cout<<"1. Equeue operation"<<endl;
cout<<"2. Dequeue operation"<<endl;
cout<<"3. Peek operation"<<endl;
cout<<"4. Display elements"<<endl;
cout<<"============================"<<endl;
int op{};
cout<<"Which operation you want to perform (1/2/3/4): ";
cin>>op;
switch(op)
{
case 1:
{
int data;
cout<<"Enter the value to push: ";
cin>>data;
enqueue(data);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
default:
{
cout<<"Invalid option choose correct option"<<endl;
}
}
cout<<"Do you want to continue (y/n): ";
cin>>ch1;
}while(ch1 == 'y');
return 0;
}
QUEUE USING LINKED LIST:
#include <iostream>
using namespace std;
int front{-1},rear{-1};
struct node
{
int data{};
node *next{};
};
node *head{NULL},*rear_node{NULL};
void enqueue(int d)
{
node *new_node{};
new_node = new node();
new_node -> data = d;
new_node -> next = NULL;
if(head == NULL)
{
head = new_node;
rear_node = new_node;
}
else
{
rear_node -> next = new_node;
rear_node = new_node;
}
}
void dequeue()
{
if(head == NULL)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else
{
cout<<"Element dequeued is: "<<head -> data<<endl;
head = head -> next;
}
}
void peek() //to get the top most element of the stack without removing the element
{
cout<<"Top most element is: "<<head -> data<<endl;
}
void display()
{
if(head == NULL0
{
cout<<"Queue is empty"<<endl;
}
else
{
node *temp{NULL};
temp = head;
while(temp != 0)
{
cout<<"Element is: "<<temp -> data<<endl;
temp = temp -> next;
}
}
}
int main()
{
char ch1{};
do
{
cout<<"============================"<<endl;
cout<<"1. Equeue operation"<<endl;
cout<<"2. Dequeue operation"<<endl;
cout<<"3. Peek operation"<<endl;
cout<<"4. Display elements"<<endl;
cout<<"============================"<<endl;
int op{};
cout<<"Which operation you want to perform (1/2/3/4): ";
cin>>op;
switch(op)
{
case 1:
{
int data;
cout<<"Enter the value to push: ";
cin>>data;
enqueue(data);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
peek();
break;
}
case 4:
{
display();
break;
}
default:
{
cout<<"Invalid option choose correct option"<<endl;
}
}
cout<<"Do you want to continue (y/n): ";
cin>>ch1;
}while(ch1 == 'y');
return 0;
}
CIRCULAR QUEUE USING ARRAY:
#include <iostream>
#define N 5
using namespace std;
int queue[N];
int front{-1},rear{-1};
void enqueue(int d)
{
if(front == -1 && rear == -1)
{
front++;
rear++;
queue[rear] = d;
}
else if((rear + 1)%N == front)
{
cout<<"Queue is full"<<endl;
}
else
{
rear = (rear + 1) % N;
queue[rear] = d;
}
}
void dequeue()
{
if(front == -1 && rear == -1)
{
cout<<"!!! UNDERFLOW CONDITION !!!"<<endl;
}
else if(front == rear)
{
front = -1;
rear = -1;
}
else
{
cout<<"Element dequeued is: "<<queue[front]<<endl;
front = (front + 1) % N;
}
}
void display()
{
int i{front};
if(front == -1 && rear == -1)
{
cout<<"Queue is empty"<<endl;
}
else
{
while(i != rear)
{
cout<<"Element is: "<<queue[i]<<endl;
i = (i + 1) % N;
}
}
}
int main()
{
char ch1{};
do
{
cout<<"============================"<<endl;
cout<<"1. Equeue operation"<<endl;
cout<<"2. Dequeue operation"<<endl;
cout<<"3. Display elements"<<endl;
cout<<"============================"<<endl;
int op{};
cout<<"Which operation you want to perform (1/2/3): ";
cin>>op;
switch(op)
{
case 1:
{
int data;
cout<<"Enter the value to push: ";
cin>>data;
enqueue(data);
break;
}
case 2:
{
dequeue();
break;
}
case 3:
{
display();
break;
}
default:
{
cout<<"Invalid option choose correct option"<<endl;
}
}
cout<<"Do you want to continue (y/n): ";
cin>>ch1;
}while(ch1 == 'y');
return 0;
}
==============================================================
Comments
Post a Comment