Vous êtes sur la page 1sur 9

//PUSH and POP operations on a stack-array implementation

#include<iostream.h>
#include<conio.h>
#include<process.h>
# define SIZE 20
void PUSH(int stack[],int item, int &top)
{if(top==size-1)
{cout<<”\n OVERFLOW”;
exit(1);
}
else
{ top++;
stack[top]=item;
}
}
int POP(int stack[], int &top)
{ if(top==-1)
{ cout<<”\n UNDERFLOW”;
Exit(1);
}
else
{ int ret=stack[top];
top--;
}
return (ret);
}
void DISPLAY(int stack[], int top)
{ if (top==-1)
cout<,”Stack empty”;
else
{cout<,stack[top]<<”<--“ <<endl;
for(int i=top-1;i>=0;i--)
cout<,stack[i]<,endl;
}
}
void main()
{int stack[size],item, top=-1, res;
char ch=‟y‟;
clrscr();
while(ch==‟y‟ || ch==‟Y‟)
{cout<<”\n Enter the ITEM for insertion:”;
cin>>item;
PUSH(stack,item,top);
cout<<”\n The stack is :\n”;
DISPLAY(stack,top);
cout<<”\n Want to insert more elements? (y/n)”;
cin>>ch;
}
cout<<”\n Deletion of elements:\n”;
ch=‟y‟;
while(ch==‟y‟ || ch==‟Y‟)
{
res=POP(stack,top);
cout<<”\n Deleted element is:”<< res;
cout<<”\n The stack is :\n”;
DISPLAY(stack,top);
cout<<”\n Want to delete more elements? (y/n)”;
cin>>ch;
}
getch();
}

/Program illustrating basic operation of add stack, delete stack


//and shows stack using linked list.
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node
{
int data;
node *link;
};
node *push(node *top, int val); // Add stack
node *pop(node *top, int &val); // Delete stack
void show_Stack(node *top); // Show stack
void main()
{node *top;
int val;
int choice;
char opt = 'Y'; // To continue the do loop in case
top = NULL; // Initialization of Stack
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Stack";
cout << "\n\t2. Deletion from Stack";
cout << "\n\t3. Traverse of Stack";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{cout << "Enter the value to be added in the stack ";
cin >> val;
top = push(top, val);
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{top = pop(top,val);
if (val != -1)
cout << "Value deleted from Stack is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Stack(top);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
node *push(node *top, int val)
{
node *temp;
temp = new node;
temp->data = val;
temp->link = NULL;
if(top ==NULL)
top = temp;
else
{
temp->link = top;
top = temp;
}
return(top);
}
node *pop(node *top,int &val)
{
node *temp;
clrscr();
if (top == NULL )
{
cout<<"Stack Empty ";
val = -1;
}
else
{
temp = top;
top = top->link;
val = temp->data;
temp->link = NULL;
delete temp;
}
return (top);
}
void show_Stack(node *top)
{ node *temp;
temp = top;
clrscr();
cout<<"The values are \n";
while (temp != NULL)
{
cout <<"\n"<< temp->data;
temp = temp->link;
}
}

// Program illustrating basic operations in an array queue


#include<iostream.h>
#include<conio.h>
#include <stdlib.h>
class queue
{ int data[10];
int front, rear;
public:
queue()
{
front = -1;
rear = -1;
}
void add(); // To add an element into the queue
void remove(); // To remove an element from the wueue
void Delete(int ITEM); //To delete all elements which are equal to ITEM;
};
void queue::add()
{ if (rear == front)
{
if (rear == -1)
front = rear = 0;
else
rear = (rear + 1) % 10;
cout << "Enter data : ";
cin >> data[rear];
}
else
cout << "Queue full :: Overflow error!!\n";
}
void queue::remove()
{
if (front != -1)
{
cout << data[front] << " deleted ";
if (front == rear)
front = rear - 1;
else
front = (front - 1) % 10;
}
else
cout << "Queue empty ! Underflow error!!\n";
}
void main()
{ clrscr();
queue Q;
Q.add();
Q.remove();
}
//Program illustrating basic operations in a linked queue
#include <iostream.h>
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <ctype.h>
struct node
{ char data;
node *link;
};
node *add_Q(node *rear, char val); // Add queue
node *del_Q(node *front, char &val);// Delete queue
void show_Q(node *front); // Show queue
void main()
{
node *front, *rear;
char val;
int choice;
char opt = 'Y'; // To continue the do loop in case
front = rear = NULL; // Initialization of Queue
clrscr();
do
{
cout << "\n\t\t Main Menu";
cout << "\n\t1. Addition of Queue";
cout << "\n\t2. Deletion from Queue";
cout << "\n\t3. Traverse of Queue";
cout << "\n\t4. Exit from Menu";
cout << "\n\nEnter Your choice from above ";
cin >> choice;
switch (choice)
{
case 1:
do
{
cout << "Enter the value to be added in the queue ";
cin >> val;
rear = add_Q(rear, val);
if (front == NULL)
front = rear;
cout << "\nDo you want to add more element <Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 2:
opt = 'Y'; // Initialize for the second loop
do
{
front = del_Q(front, val);
if (front == NULL)
rear = front;
if (val != -1)
cout << "Value deleted from Queue is " << val;
cout << "\nDo you want to delete more element <Y/N>? ";
cin >> opt;
} while (toupper(opt) == 'Y');
break;
case 3:
show_Q(front);
break;
case 4:
exit(0);
}
}
while (choice != 4);
}
node *add_Q(node *rear, char val)
{ node *temp;
temp = new node;
temp->data = val;
temp->link = NULL;
rear->link = temp;
rear = temp;
return (rear);
}
node *del_Q(node *front, char &val)
{ node *temp;
clrscr();
if (front == NULL)
{
cout << "Queue Empty ";
val = -1;
}
else
{
temp = front;
front = front->link;
val = temp->data;
temp->link = NULL;
delete temp;
}
return (front);
}
void show_Q(node *front)
{
node *temp;
temp = front;
clrscr();
cout << "The Queue values are";
while (temp != NULL)
{
cout <<"\n"<< temp->data;
temp = temp->link;
}
}

Vous aimerez peut-être aussi