Written Assignment 3, Stack Implementation
University of the People
CS 3303 Data Structures
Written Assignment 3
April 27, 2022
Stack Implementation
Written Assignment 3, Stack Implementation
2
Stack Implementation
A stack is a list-like data structure, that has only one end for both adding and removing
elements. Adding an element to a stack is called push operation and removing an element
from a stack is called pop operation (Shaffer, 2011)
This assignment has asked to develop an algorithm for Stack and deploy it to push 3
vehicles into the stack and pop them one by one while showing the popped element in the
console.
import jeliot.io.*:
public class Stacks {
public static void main(String[] args) {
Car car1 = new Car("Toyota", 18143);
Car car2 = new Car("Tesla", 16230);
Car car3 = new Car("BMW", 8978);
Stack stack = new StackO;
stack.push(car1);
stack.push(car2);
stack.push(car3);
Car a = stack.popO;
System.out.println(a + "is popped out from the stack.");
2
Written Assignment 3, Stack Implementation
3
Car b = stack.popO;
System.out.println(b + "is popped out from the stack.")
Car c = stack.popO;
System.out.println(c + "is popped out from the stack.");
System.out.println("First popped car: " + a);
System.out.println("Last popped car: " + c);
}
class Car{
String name;
int miles;
Car(String name, int miles){
this.miles = miles;
this.name = name;
};
/l@Override
public String toStringO{
return this.name + " - " + this.miles;