0% found this document useful (0 votes)
46 views8 pages

Lab Session # 5 Stacks: Answer To Question # 1 and 2

This document discusses stacks and provides code examples for push and pop operations on a stack. It defines a stack as an array with a TOP variable to track the last element. PUSH increments TOP and inserts a new element. POP decrements TOP and sets the top element to 0, removing it. Code examples demonstrate pushing and popping values, and checking for overflow and underflow conditions. The output shows the stack contents and TOP value before and after each operation.

Uploaded by

TAHREEM FATIMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views8 pages

Lab Session # 5 Stacks: Answer To Question # 1 and 2

This document discusses stacks and provides code examples for push and pop operations on a stack. It defines a stack as an array with a TOP variable to track the last element. PUSH increments TOP and inserts a new element. POP decrements TOP and sets the top element to 0, removing it. Code examples demonstrate pushing and popping values, and checking for overflow and underflow conditions. The output shows the stack contents and TOP value before and after each operation.

Uploaded by

TAHREEM FATIMA
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Lab Session # 5

Stacks
Answer to Question # 1 and 2:
PUSH:
Code:
package lab5;

import java.util.*;

public class Lab5 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in);

//getting the MAXSTK(length of array)

System.out.println("enter length of the array you want to create: ");

int MAXSTK=input.nextInt(); //settting the max length of Stack

int STACK[]=new int[MAXSTK]; //creating a new stack of type int and length MAXSTK

int TOP=-1; //setting the TOP (last in element index) of the STACK

//putting values in stack till TOP

System.out.println("do you want to enter element? If yes press Y else N: ");

String in=input.next();

int k=0; //setting default value to a looping vaiable

while(in.equals("Y")){

System.out.println("Enter element: ");

int element=input.nextInt();

TOP=k;

STACK[k]=element;

k++;

System.out.println("if you want to add another item please write Y else N: ");

in=input.next();}
System.out.println("TOP before PUSH operation: "+TOP);

int ITEM=9; //setting ITEM we want ot push in the stack

// applying condition to check if stack have space

if(MAXSTK==TOP+1){

System.out.println("Stack Overflow!!");}

else{

TOP+=1; //incrementing TOP

STACK[TOP]=ITEM; //inserting the ITEM in stack

//printing STACK

for(int i=0;i<STACK.length;i++){

System.out.println(STACK[i]);}

System.out.println("TOP after PUSH operation: "+TOP);}}}

Output:
Output by the above code:
Output by setting MAXSTK=3 and ITEM=77 and getting TOP=1

Output by setting MAXSTK=4 and ITEM=80 and getting TOP=3

POP:
Code:
package lab5;
import java.util.*;

public class Lab5 {

public static void main(String[] args) {

Scanner input=new Scanner(System.in); //getting length of the array

System.out.println("Enter length of the array you want to create: ");

int a=input.nextInt();

int STACK[]=new int[a]; //creating a new stack of type int

int TOP=-1; //setting the TOP (last in element index) to be default

//putting values in stack

System.out.println("Press Y if you want to add element to array else N: ");

String in=input.next();

int k=0; //setting default value to a looping vaiable

while(in.equals("Y")){

System.out.println("Enter element: ");

int element=input.nextInt();

TOP=k;

STACK[k]=element;

k++;

System.out.println("if you want ot add another item please write Y else N: ");

in=input.next();}

System.out.println("TOP before POP operation: "+TOP);

// applying condition to check if stack have space

if(TOP==-1){

System.out.println("Stack Underflow!!");}

else{

STACK[TOP]=0; //setting element at TOP to be 0 (deleted/POPed)

TOP-=1; //decrementing TOP

//printing STACK

for(int i=0;i<STACK.length;i++){
System.out.println(STACK[i]);}

System.out.println("TOP after POP operation: "+TOP);}}}

Output:
Output by the above code:

Output by setting array to be of length 3 and getting TOP=-1


Output by setting array to be of length 6 and getting TOP=2

Answer to Question # 3:
Code:
package lab5;

import java.util.*;

public class Lab5 {

public static void main(String[] args) {

int MAXSTK=5; //setting length of the STACK

int STACK[]=new int[MAXSTK]; //creating a new stack of type int

int TOP=3; //setting the TOP (last in element index) to be default

//putting values in stack

STACK[0]=1;

STACK[1]=2;

STACK[2]=3;
STACK[3]=4;

Scanner input=new Scanner(System.in);

System.out.println("Enter the operation you want ot perform (PUSH/POP): ");

String op=input.next();

if(op.equals("POP")){

System.out.println("TOP before POP operation: "+TOP);

// applying condition to check if stack have space

if(TOP==-1){

System.out.println("Stack Underflow!!");}

else{

STACK[TOP]=0; //setting element at TOP to be 0 (deleted/POPed)

TOP-=1; //decrementing TOP

//printing STACK

for(int i=0;i<STACK.length;i++){

System.out.println(STACK[i]);}

System.out.println("TOP after POP operation: "+TOP);}}

else if(op.equals("PUSH")){

System.out.println("TOP before PUSH operation: "+TOP);

int ITEM=9; //setting ITEM we want ot push in the stack

// applying condition to check if stack have space

if(MAXSTK==TOP+1){

System.out.println("Stack Overflow!!");}

else{

TOP+=1; //incrementing TOP

STACK[TOP]=ITEM; //inserting the ITEM in stack

//printing STACK

for(int i=0;i<STACK.length;i++){

System.out.println(STACK[i]);}

System.out.println("TOP after PUSH operation: "+TOP);}}}}


Output:
Output by the above code:

Output by giving POP as the operation:

You might also like