0% found this document useful (0 votes)
5 views14 pages

DSLab Excersices

Uploaded by

adhithya0306
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)
5 views14 pages

DSLab Excersices

Uploaded by

adhithya0306
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/ 14

EX.

NO:1 IMPLEMENTATION OF STACK


DATE:
AIM:
To implement stack operation (push, pop, display) in C++.
ALGORITHM:
1. Start the program
2. Get the choice from the user
3. To push an item into the stack
i. Get the item from the user
ii. Allocate memory for the node
iii. Store the item into the node
iv. Pointer is moved to top item of list.
4. To pop an item
i. Delete the top item of the list
ii. Move the top pointer.
5. Display the items of the stack
6. Stop the program.
PROGRAM:
#include<iostream.h>
#include<conio.h>
int stack[100],option,n,top,x,i;
void push(),pop(),display();
void main()
{
clrscr();
top=-1;
cout<<"\nEnter The Size Of STACK [MAX=100] : ";
cin>>n;
do
{
clrscr();
cout<<"\n\t\tSTACK OPERATION";
cout<<"\n\t\t===== =========";
cout<<"\n\t1.PUSH\n\t2.POP\n\t3.DISPLAY\n\t4.EXIT";
cout<<"\n\nEnter Your Option : ";
cin>>option;
switch(option)
{
case 1:
{
push();
break;
}
case 2:
{
pop();
break;
}
case 3:
{
display();
break;
}
case 4:
{
cout<<"\n\n\t\tYour On Exit...";
break;
}
}
getch();
}while(option!=4);
}
void push()
{
if(top>=n-1)
{
cout<<"\n\t\tSTACK is Over Flow...";
getch();
}
else
{
cout<<"\nEnter a Value to be Push : ";
cin>>x;
top++;
stack[top]=x;
cout<<"\n\t\t"<<x<<" is Inserted.\n\n\n";
display();
}
}
void pop()
{
if(top<=-1)
{
cout<<"\n\t\tStack is Under Flow";
}
else
{
cout<<"\nRemoving a Value...";
cout<<"\n\t\tThe Deleted Item is "<<stack[top];
top--;
display();
}
}
void display()
{
if(top>=0)
{
cout<<"\nThe Elements in STACK.";
cout<<"\n=== ======== == =====";
for(i=0;i<=top;i++)
cout<<"\n\nDATA "<< i+1<<"="<<stack[i];
cout<<"\n\n\n\n Press Any Key to Continue...";
}
else
{
cout<<"\nThe STACK is Empty.";
}
}

OUTPUT:
Enter The Size Of STACK [MAX=100] : 2
STACK OPEATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 1
Enter a Value to be Push : 89
89 is Inserted.
The Elements in STACK.
=== ======== == =====
DATA 1=89
Press Any Key to Continue...

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 1
Enter a Value to be Push : 37
37 is Inserted.
The Elements in STACK.
=== ======== == =====
DATA 1=89
DATA 2=37
Press Any Key to Continue...

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 1
STACK is Over Flow...

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 2
Removing a Value...
The Deleted Item is 37
The Elements in STACK.
=== ======== == =====
DATA 1=89
Press Any Key to Continue...

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 3
The Elements in STACK.
=== ======== == =====
DATA 1=89
Press Any Key to Continue...

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 2
Removing a Value...
The Deleted Item is 89
The STACK is Empty.

STACK OPERATION
===== =========
1.PUSH
2.POP
3.DISPLAY
4.EXIT
Enter Your Option : 4
Your On Exit...

RESULT:
Thus the stack operation was successfully implemented and executed using C++.
EX.NO:2 EVALUATION OF EXPRESSION

DATE:
AIM:

To evaluate the value for given postfix expression using C++.

ALGORITHM:

1. Start the program


2. Declare the variables and functions
3. Get the postfix expression from the user
4. Get value for the expression
5. Perform arithmetic operation and display the result
6. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
#include<math.h>
#include<ctype.h>
#include<string.h>

int n,j,top;
float v1,v2,v,vstack[100],val[100];
char ch[100],chh,postfix[100];
float pop(float*),compute(float,float,char),eval(char*);
float return_val(char);
void push(float*,float),alpha(char*),getvalue();

void main()
{
clrscr();
cout<<"\n Enter An Postfix Exp : ";
cin>>postfix;
alpha(postfix);
getvalue();
j=0;
chh=postfix[j];
while(chh!='\0')
{
if(isalpha(chh))
{
v=return_val(chh);
push(vstack,v);
}
else
{
if(top!=-1)
v2=pop(vstack);
if(top!=-1)
v1=pop(vstack);
v=compute(v1,v2,chh);
push(vstack,v);
}
j++;
chh=postfix[j];
}
v=pop(vstack);
cout<<"\n The Result is "<<v;
getch();
}

void alpha(char* pf)


{
int i=0,t=0;
n=-1;
while(pf[i]!='\0')
{
if(isalpha(pf[i]))
{
for(j=0;j<=n;j++)
if(ch[j]==pf[i])
t=1;
if(t==0)
{
n++;
ch[n]=pf[i];
}
}
i++;
t=0;
}
}

void getvalue()
{
int i;
for(i=0;i<=n;i++)
{
cout<<"\nEnter the value for "<<ch[i]<<":";
cin>>val[i];
}
}

float return_val(char cc)


{
int i;
for(i=0;i<=n;i++)
{
if(cc==ch[i])
break;
}
return(val[i]);
}

float pop(float* x)
{
float cc;
if(top!=-1)
{
cc=x[top];
top--;
return (cc);
}
return(-1000);
}

void push(float* x,float cc)


{
top++;
x[top]=cc;
}

float compute(float x,float y,char ccc)


{
switch(ccc)
{
case '*':return(x*y);
case '/':return(x/y);
case '+':return(x+y);
case '-':return(x-y);
case '^':return(pow(x,y));
default :return(-1000);
}
}
OUTPUT:

Enter An Postfix Exp : abc^/de*-ac*+

Enter the value for a:1

Enter the value for b:2

Enter the value for c:3

Enter the value for d:4


Enter the value for e:5

The Result is -16.875

RESULT:
Thus the program to evaluate postfix expression was successfully implemented &
executed using C++.
EX.NO:3 IMPLEMENTATION OF QUEUE

DATE:
AIM:

To implement queue operation(enque,deque,view) in C++.

ALGORITHM:

1. Start the program


2. Get the choice from the user
3. To enque an item into the queue
i. Get the item from the user
ii. Identify the last node of the queue
iii. Allocate memory for the node
iv. Store the item into the node.
4. To deque an item
i. Note the first item of the queue
ii. Delete node from the queue.
5. View the items of the queue
6. Stop the program.

PROGRAM:

#include<iostream.h>
#include<conio.h>
int front,rear;
int queue[200];
int n,choice,x;
void enque(),deque(),view(void);
void main()
{
clrscr();
front=0;
rear=0;
cout<<"\nEnter The Size Of QUEUE [MAX=100] : ";
cin>>n;
do
{
clrscr();
cout<<"\n\t\tQUEUE OPERATION";
cout<<"\n\t\t===== =========";
cout<<"\n\n\t1.ENQUE\n\t2.DEQUE\n\t3.VIEW\n\t4.EXIT";
cout<<"\n\nEnter Your Choice : ";
cin>>choice;
switch(choice)
{
case 1:
{
enque();
break;
}
case 2:
{
deque();
break;
}
case 3:
{
view();
break;
}
case 4:
{
cout<<"\n\n\t\t You Are Exited...";
break;
}
}
getch();
}while(choice!=4);
}
void enque()
{
if(rear>=n)
{
cout<<"\n\t\tQUEUE is Over Flow...";
getch();
}
else
{
cout<<"\nEnter a Value : ";
cin>>x;
queue[rear]=x;
cout<<x<<" is Added.\n";
rear++;
view();
}
}
void deque()
{
if(front>=rear)
{
cout<<"\n\t\tQUEUE is Under Flow...";
}
else
{
cout<<queue[front]<<" is Deleted.\n\n";
front++;
view();
}
}
void view()
{
int i=front,j=rear;
if(i>=j)
{
cout<<"\n\t\tQUEUE is Empty...";
}
else
{
cout<<"\n\n\tData in Queue";
cout<<"\n\t===============";
for(;i<=j-1;i++)
cout<<"\nDATA "<<i+1<<"="<<queue[i];
cout<<"\nPress any key to continue..";
}
}

OUTPUT:
Enter The Size Of QUEUE [MAX=100] : 2
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 1
Enter a Value : 12
12 is Added.
Data in Queue
===============
DATA 1=12
Press any key to continue..
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 1
Enter a Value : 951
951 is Added.
Data in Queue
===============
DATA 1=12
DATA 2=951
Press any key to continue..
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 3
Data in Queue
===============
DATA 1=12
DATA 2=951
Press any key to continue..
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 1
QUEUE is Over Flow...
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 2
12 is Deleted.
Data in Queue
===============
DATA 2=951
Press any key to continue..
QUEUE OPERATION
===== =========
1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 2
951 is Deleted.
QUEUE is Empty...

QUEUE OPERATION
===== =========

1.ENQUE
2.DEQUE
3.VIEW
4.EXIT
Enter Your Choice : 4

You Are Exited...

RESULT:

Thus the program to perform queue operation was successfully implemented and
executed using C++.

You might also like