Professional Documents
Culture Documents
Lab-5
Statement Purpose:
This lab will introduce you the concept of Stack data structure
Activity Outcomes:
This lab teaches you the following topics:
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:
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
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");
}
**********************