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(); }

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question

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
Transcribed Image Text: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
Expert Solution
steps

Step by step

Solved in 3 steps with 2 images

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY