0% found this document useful (0 votes)
12 views

CSE 2001 Lab Session 2 - Stack Operations

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

CSE 2001 Lab Session 2 - Stack Operations

Uploaded by

zakwan1292003
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Lab Session 02

Date
Course Code CSE 2001
Course Name Data Structures and Algorithms
Semester III
Course Instructor Name of the instructor
Learning LO1: Identify the key operations of a stack, including push, pop, peek, and
Outcomes checking the stack's status.
LO2: Implement a stack using arrays and execute fundamental stack
operations effectively in a program.

Definition: A stack is an ordered list in which insertion and deletion are done at one end,
called
top. The last element inserted is the first one to be deleted. Hence, it is called the Last in First
Out (LIFO) or First in Last Out (FILO) list.

ADT

The following operations make a stack an ADT. For simplicity, assume the data is an integer
type.

Primary stack operations


1. void push(int data): Inserts data onto the stack.
2. int pop(): Removes and returns the last inserted element from the stack

Stack Top Operations Return Value


-1 -
10 Push (10) void
10 20 Push (20) void
- 10 20 30 Push (30) void
10 20 Pop() 30
10 Pop() 20
-1 Pop() 10

Auxiliary stack operations


1. int Top(): Returns the last inserted element without removing it.
2. int Size(): Returns the number of elements stored in the stack.
3. int IsEmptyStack(): Indicates whether any elements are stored in the stack or not.
4. int IsFullStack(): Indicates whether the stack is full or not.

Top Operations Return


Stack
Value
- 0 23 45 45 34
1-
-
Top() 34
Size() 06
IsEmptyStack() False
IsFullStack() True
-1 0 23 45 45 Pop() 34
IsFullStack() False

Applications
1. Balancing of symbols
2. Infix-to-postfix conversion
3. Evaluation of postfix expression
4. Implementing function calls (including recursion)
5. Finding of spans (finding spans in stock markets, refer to Problems section)
6. Page-visited history in a Web browser [Back Buttons]
7. Undo sequence in a text editor
8. Matching Tags in HTML and XML

Implementation of STACK Data structure using Arrays

File Name: Stack.java

package myStack;

/**
* @author pavan
*
*/
public class Stack {
int size;
int stack[];
int top;
public Stack(int size) {

this.size = size;
this.stack = new int[size];
this.top = -1;
}
public int Top()
{
return stack[top];
}
public int Size()
{
return top+1;
}
public boolean IsEmptyStack()
{
return top==-1?true:false;
}
public boolean IsFullStack()
{
return top>=size-1?true:false;
}
public void push(int element)
{
if(IsFullStack())
{
System.out.println("Stack is OverFlow");
}
else
{
top++;
stack[top]=element;
}
}
public int pop()
{
int deletedElemnt;
if(IsEmptyStack())
{
System.out.println("Under Flow");
return -1;
}
else
{
deletedElemnt=stack[top];
top--;
return deletedElemnt;
}
}
public void display()
{
if(IsEmptyStack())
{
System.out.println("No ELment to display");
}
else
{
for(int i=0;i<=top;i++)
{
System.out.print("| "+stack[i]);
}
}
}

File Name: TestStack.java

package myStack;

import java.util.Scanner;

/**
* @author pavan
*
*/
public class TestStack {

public static void main(String[] args) {


int choice=0;
int size;
int newElemnt;
int deletedElement;
Scanner scanner=new Scanner(System.in);
System.out.println("Enter the Size of Stack");
size=scanner.nextInt();
Stack stack=new Stack(size);
while(choice!=8)
{
System.out.println("1.PUSH 2.POP 3.Display 4.Find Top
Element 5.Find the Size 6.Stack is Empty 7.Stack is Full 8.Exit");
System.out.println("Enter Your Choice:");
choice=scanner.nextInt();
switch(choice)
{
case 1:System.out.println("New Element");
newElemnt=scanner.nextInt();
stack.push(newElemnt);
break;
case 2: deletedElement=stack.pop();
if(!stack.IsEmptyStack())
{
System.out.println("The deleted
Element is :"+deletedElement);
}
break;
case 3: stack.display();
break;
case 4: System.out.println("Top
Element:"+stack.Top());break;
case 5:
System.out.println("Size:"+stack.Size());break;
case 6: System.out.println("IS Stack is
Empty :"+stack.IsEmptyStack());break;
case 7: System.out.println("Is Stack is
Full:"+stack.IsFullStack());break;
}
System.out.println("");
}
scanner.close();

You might also like