0% found this document useful (0 votes)
6 views4 pages

Labsheet 2 - Stack

The document contains two implementations of a stack data structure using arrays in Java. The first implementation has a fixed size of 5, while the second allows the user to specify the stack size using the Scanner class. Both implementations include methods for pushing, popping, peeking, checking if the stack is empty or full, and displaying the stack contents.

Uploaded by

20ammar02
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)
6 views4 pages

Labsheet 2 - Stack

The document contains two implementations of a stack data structure using arrays in Java. The first implementation has a fixed size of 5, while the second allows the user to specify the stack size using the Scanner class. Both implementations include methods for pushing, popping, peeking, checking if the stack is empty or full, and displaying the stack contents.

Uploaded by

20ammar02
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/ 4

Lab sheet 2: Stack Implementation using Arrays

package stack1;

public class Stack {


private int maxSize;
private int top;
private int[] stackArray;

public Stack() {
maxSize = 5;
stackArray = new int[maxSize];
top = -1; // Empty stack
}

public void push(int value) {


if (top < maxSize - 1) {
top=top+1;
stackArray[top] = value;
System.out.println(value + " pushed onto the stack.");
} else {
System.out.println("Stack is full. Cannot push " + value);
}
}

public int pop() {


if (top >= 0) {
int poppedValue = stackArray[top];
top=top-1;
System.out.println(poppedValue + " popped from the stack.");
return poppedValue;
} else {
System.out.println("Stack is empty. Cannot pop.");
return -1; // Or throw an exception
}
}

public int peek() {


if (top >= 0) {
return stackArray[top];
} else {
System.out.println("Stack is empty. Nothing to peek.");
return -1; // Or throw an exception
}
}

public boolean isEmpty() {


return top == -1;
}
public boolean isFull() {
return top == maxSize - 1;
}

public void display() {


for(int i=0;i<=top;i++)
{
System.out.println("The elements in array are:");
System.out.println(stackArray[i]);
}
}

public static void main(String[] args) {


Stack stack = new Stack();

stack.push(10);
stack.push(20);
stack.push(30);
stack.display();

System.out.println("Peek: " + stack.peek());

stack.pop();
stack.pop();
stack.push(50);
stack.display();

System.out.println("Is empty? " + stack.isEmpty());


System.out.println("Is full? " + stack.isFull());

}
}

[OR]

Using SCANNER CLASS

package stack1;
import java.util.Scanner;
public class Stacknew {
private int maxSize;
private int top;
private int[] stackArray;

public Stacknew(int size) {


maxSize = size;
stackArray = new int[maxSize];
top = -1;
}
public boolean isEmpty() {
return top == -1;
}

public boolean isFull() {


return top == maxSize - 1;
}

public void push(int item) {


if (isFull()) {
System.out.println("Stack is full. Cannot push.");
return;
}
stackArray[++top] = item;
System.out.println("Pushed: " + item);
}

public int pop() {


if (isEmpty()) {
System.out.println("Stack is empty. Cannot pop.");
return -1; // You can choose a different value to represent an error.
}
int item = stackArray[top--];
System.out.println("Popped: " + item);
return item;
}

public int peek() {


if (isEmpty()) {
System.out.println("Stack is empty. Cannot peek.");
return -1; // You can choose a different value to represent an error.
}
return stackArray[top];
}

//public class Main {


public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.print("Enter the size of the stack: ");
int size = scanner.nextInt();

Stacknew stack = new Stacknew(size);

while (true) {
System.out.println("\nStack Operations:");
System.out.println("1. Push");
System.out.println("2. Pop");
System.out.println("3. Peek");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();

switch (choice) {
case 1:
System.out.print("Enter element to push: ");
int pushItem = scanner.nextInt();
stack.push(pushItem);
break;
case 2:
int popItem = stack.pop();
if (popItem != -1) {
System.out.println("Popped item: " + popItem);
}
break;
case 3:
int peekItem = stack.peek();
if (peekItem != -1) {
System.out.println("Peeked item: " + peekItem);
}
break;
case 4:
scanner.close();
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy