3. Implement an ADT List (refer to the code below)-Based ADT Stack (refer to pp. 396 - 397 (attached image)) and verify "isEmpty()", "push()", "pop()", "popAll()", and "peak()" operations. class Node { int data; Node next; public Node(int data) { this.data = data; this.next = null; } } class List { private Node head; private int size; public List() { head = null; size = 0; } public void add(int data) { Node newNode = new Node(data); if (head == null) { head = newNode; } else { Node current = head; while (current.next != null) { current = current.next; } current.next = newNode; } size++; } public int get(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of range"); } Node current = head; for (int i = 0; i < index; i++) { current = current.next; } return current.data; } public void remove(int index) { if (index < 0 || index >= size) { throw new IndexOutOfBoundsException("Index out of range"); } if (index == 0) { head = head.next; } else { Node prev = null; Node current = head; for (int i = 0; i < index; i++) { prev = current; current = current.next; } prev.next = current.next; } size--; } public boolean isEmpty() { return size == 0; } public int size() { return size; } public void removeAll() { head = null; size = 0; } public void printList() { Node current = head; while (current != null) { System.out.print(current.data + " "); current = current.next; } System.out.println(); }
3. Implement an ADT List (refer to the code below)-Based ADT Stack (refer to pp. 396 - 397 (attached image)) and verify "isEmpty()", "push()", "pop()", "popAll()", and "peak()" operations.
class Node { int data;
Node next;
public Node(int data) { this.data = data;
this.next = null; }
}
class List {
private Node head; private int size;
public List() { head = null;
size = 0; }
public void add(int data) {
Node newNode = new Node(data); if (head == null) {
head = newNode; } else {
Node current = head; while (current.next != null) {
current = current.next; }
current.next = newNode; }
size++; }
public int get(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of range"); }
Node current = head;
for (int i = 0; i < index; i++) {
current = current.next; }
return current.data; }
public void remove(int index) { if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index out of range"); }
if (index == 0) {
head = head.next;
} else {
Node prev = null;
Node current = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current.next; }
prev.next = current.next; }
size--; }
public boolean isEmpty() {
return size == 0; }
public int size() {
return size; }
public void removeAll() { head = null;
size = 0; }
public void printList() { Node current = head; while (current != null) {
System.out.print(current.data + " ");
current = current.next; }
System.out.println(); }
}
public class Main {
public static void main(String[] args) {
List list = new List();
list.add(10);
list.add(20);
list.add(30);
list.add(40);
list.add(50);
System.out.println("Elements after adding: "); list.printList();
System.out.println("Element at index 2: " + list.get(2));
list.remove(1);
System.out.println("Elements after removing: "); list.printList();
System.out.println("Is the list empty? " + list.isEmpty()); System.out.println("Size of the list: " + list.size());
list.removeAll();
System.out.println("Is the list empty after removeAll? " + list.isEmpty()); }
}
![The Stack class is derived from the class Vector-a growable array of
objects. It extends the vector class with five methods that allow for a LIFO
stack of objects. Most of these methods are quite similar to the ones presented
in this chapter: push, pop, empty, and peek. An additional method, called
search, allows you to determine how far an item is from the top of the stack.
Here is the specification for the JCF Stack collection as it is derived from
vector, only the method headings are shown:
public class Stack<E> extends Vector<E> {
public Stack ( )
// Creates an empty Stack
public boolean empty()
// Tests if this stack is empty.
public E peek () throws EmptyStackException
// Looks at the object at the top of this stack without
// removing it from the stack.
public E pop () throws EmptyStackException
// Removes the object at the top of this stack and
// returns that object as the value of this function.
public E push(E item)
// Pushes an item onto the top of this stack.
public int search (Object o)
// Returns the 1-based position where an object is on this
//stack. The topmost item on the stack is considered to be
// at distance 1.
} // end Stack
Note that the Stack has one data-type parameter for the items contained in
the stack. Here is an example of how the JCF Stack is used:
import java.util.Stack;
public class TestStack {
static public void main(String[] args) {
Stack<Integer > aStack = new Stack<Integer>();
if (aStack.empty()) {
System.out.println("The stack is empty");
} // end if
for (int i = 0; i < 5; i++) {
aStack.push(i); // with autoboxing, this is the same
// as aStack.push(new Integer (i))
} // end for
while (!aStack.empty()) {
System.out.print(aStack.pop()+ " ");
} // end while.
System.out.println();
} // end main
} // end TestStack
Application: Algebraic Expressions
The output of this program is
The stack is empty
4 3 2 1 0
397](/v2/_next/image?url=https%3A%2F%2Fcontent.bartleby.com%2Fqna-images%2Fquestion%2Ff57c2464-7a9e-4e3b-9853-4b15978e2cee%2F62c5cde0-456d-4a15-8df5-4046e83208b9%2Fmflgpc_processed.png&w=3840&q=75)

Step by step
Solved in 3 steps with 2 images









