You are on page 1of 5

LAB # 05

Lab-5

March 14th 2019

Statement Purpose:
This lab will introduce you the concept of Stack data structure

Activity Outcomes:
This lab teaches you the following topics:

• How to access the top of the stack


• How to push data onto the stack
• How to pop data from the stack

1) Stage J (Journey)
Introduction
A stack is a special case of a singly-linked list which works according to LIFO algorithm.
Access is restricted to one end, called the top of the stack. A stack may be depicted as given in
the figure below . Its operations are:

push − push an element onto the top of the stack;

pop − pop an element from the top of the stack;

top − retrieve the element at the top of the stack;

delete − delete the whole stack. This can be done as explained in the previuos paragraph.
2) Stage a1 (apply) Lab
Activities:
Activity 1
Take a stack of size 5. Push the elements 5, 10, 15, and 20. Now do the following
operations and
show the contents of stack at each stage.
I. Pop
II. push 2
III. push 4
IV. push 6
V. pop

/* Program of stack using array*/


#include<stdio.h>
#define MAX 5
int top = -1;
int stack_arr[MAX];
main(){
int choice;
while(1){
printf("1.Push\n");
printf("2.Pop\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);
switch(choice){
case 1 :
push();
break;
case 2:
pop();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while*/
}/*End of main()*/
push(){
// write your code here
}
}/*End of push()*/
pop(){
// write your code here
}
}/*End of pop()*/
display(){
int i;
if(top == -1)
printf("Stack is empty\n");
else {
printf("Stack elements :\n");
for(i = top; i >=0; i--)
printf("%d\n", stack_arr[i] );
}
}/*End of display()*/

Activity 1.1
Write a function that checks whether parentheses in a mathematical expression are balanced or not.

Activity 2
Write a function that converts a mathematical expression with no parentheses from infix form to postfix
form

Activity 3
Write a function that converts an expression from infix form to prefix form.

3) Stage v (verify)

Home Activities:
Write a function that converts a mathematical expression containing parentheses from infix form to
postfix form

4) Stage a2 (assess)
Stack using Linked List:

#include<iostream>
#include<conio.h>
using namespace std;
struct mstack
{
int data;
mstack *next;
};
mstack* push(mstack* top, int data)
{
mstack* node = new mstack;
node->data = data;
node->next = top;
top = node;

return top;
}
void push(mstack** top, int data)
{
mstack* node = new mstack;
node->data = data;
node->next = *top;
*top = node;

//return top;
}
mstack* pop(mstack* top)
{
if (top == NULL)
cout<<"stack empty";
else
{
cout<<"data"<<top->data;
top = top->next;
}
return top;
}
void display(mstack* top)
{
if (top == NULL)
cout<<"stack empty";
else
while(top != NULL)
{
cout<<"Data:\t"<<top->data<<endl;
top = top->next;
}
}
//-----------------------------------------------------------------------------
int main () {
mstack *top;
int data,
choice;

top = NULL;
do {
printf ("\nEnter\t1 to add item,\n\t2 to remove item\n\t3 to print queue\n\t4 to quit\n");
cin>>choice;
switch (choice) {
case 1:
printf ("Enter data item value to add ");
scanf ("%d", &data);
//top = push(top, data);
push(&top, data);
break;
case 2:
if (top == NULL)
printf ("Stack empty!\n");
else
top = pop (top);
break;
case 3:
display(top);
break;

case 4:
break;

default:
printf ("Invalid menu choice - try again\n");
break;
}
} while (choice != 4);

system("pause");
}

**********************

You might also like