Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

Lab#09 Stack Implementation

SSUET/QR/114

LAB # 09

Stack Implementation

Object
To implement Stack as Arrays and linked list.

Theory

– Stack is an array used as stack.


– TOS gives them TOP of stack position
– push () operation inserts an element ITEM at TOS,
– pop () operation deletes an element from TOS.
– ISFULL () & ISEMPTY () operations: one used in push () & pop () to check the overflow
& underflow condition in stack.

Algorithm to insert an item:

Push (ITEM)

precond: Stack should not be full or overflow

Set TOS := TOS+1 [Increasing TOS by 1]

Set Stack[TOS]:= ITEM [Insert ITEM in New TOS position ]

N:=N+1

Return

Algorithm to delete an item:

Pop (ITEM)

precond: Stack should be empty or under flow

Set ITEM := Stack[TOS] [Assigns Top elements to ITEM]

Set TOS= TOS-1 , N:=N-1

Return item

SWE-203L Data Structure and Algorithms


Lab#09 Stack Implementation
SSUET/QR/114

Algorithm to check stack is Full:

IsEmpty ()

If TOS = -1, then Return TRUE

Else return False

Algorithm to check stack is Empty:

IsFull ()

If TOS = stack.length, then Return TRUE

Else return False

Sample Program

SWE-203L Data Structure and Algorithms


Lab#09 Stack Implementation
SSUET/QR/114

public class MyStack { private int


maxSize; private long[] stackArray;
private int top; public MyStack(int s) {
maxSize = s; stackArray = new
long[maxSize]; top = -1;
}
public void push(long j) { stackArray[++top] =
j;
}
public long pop() { return
stackArray[top--];
}

SWE-203L Data Structure and Algorithms


Lab#09 Stack Implementation
SSUET/QR/114

public long peek()


{ return stackArray[top];
}
public boolean isEmpty() { return
(top == -1);
}
public boolean isFull() { return
(top == maxSize - 1);
}
public static void main(String[] args)
{ MyStack theStack = new MyStack(10);
theStack.push(10); theStack.push(20);
theStack.push(30); theStack.push(40);
theStack.push(50); while (!
theStack.isEmpty()) { long value =
theStack.pop();
System.out.print(value);
System.out.print(" ");
}
System.out.println("");
}
}

SWE-203L Data Structure and Algorithms


Lab#09 Stack Implementation
SSUET/QR/114

LAB TASK

1. Write a program to create Stack that can take input random candies color (i.e. yellow, green,
orange, red etc) and stored in order. The user likes any one of the colored candy to eat so he
takes out all the candies, one by one, eats only the chosen color and keeps other in order so
that he can return them to stack in exactly the same order as before minus the chosen candies.
Print both the input and resultant stack. For example:

Original stack: Yellow->green->orange->yellow->red


Updated stack: Green->orange->red i.e. yellow is removed

2. Create a Stack Class which can store Strings in the stackArr. Let the stack be empty. Input 5
Strings change their case & then push them onto the stack. Pop the Strings & display them.

HOME TASK

1. Write a program to convert Infix expression into postfix expression by stack using linked list.

A + ( B * C - ( D / E | F ) * G ) * H

SWE-203L Data Structure and Algorithms

You might also like