Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Implement Stack using Linked List Stacks works in last-in-first-out fashion. You will implement the following methods using Linked List methods made available to you: public

Implement Stack using Linked List Stacks works in last-in-first-out fashion. You will implement the following methods using Linked List methods made available to you:

  • public void push( ) which pushes an element to the stack.
  • public int pop( ) which removes the top element from the stack and returns it.
  • public int peek( ) which returns the top element without removing from the stack.

StackList Class_____________________(edit this)

import java.util.Scanner;

public class StackList { LinkedListBasic stackList = new LinkedListBasic(); // Adds new elements to the top of the stack public void push(int data){ // Complete this method } // Removes the top element from the stack public void pop(){ // Complete this method }

// returns the top element of the stack but does not remove it. public int peek(){ // Complete this method } public void print(){ stackList.print(); } public static void main(String[] args){ StackList newStackList = new StackList(); Scanner input = new Scanner(System.in); while(true){ int data = input.nextInt(); if(data == -1) break; newStackList.push(data); } // printing stack elements System.out.println("Stack elements:"); newStackList.print();

// printing stack elements after peek newStackList.peek(); System.out.println(" After peeking:"); newStackList.print(); //printing stack elements after pop newStackList.pop(); newStackList.pop(); System.out.println(" After popping twice:"); newStackList.print(); }

}

LinkedListBasic Class______________________________

import java.util.Scanner;

public class LinkedListBasic { Node head; Node tail; // inserts data to the end of the list only using the head pointer public void append(int data){ if(head == null){ Node newNode = new Node(data); head = newNode;

} else { Node newNode = new Node(data); Node currentNode = head; while(currentNode.next != null){ currentNode = currentNode.next; } currentNode.next = newNode; } } // inserts data to the beginning of the list public void prepend(int data){ if(head == null){ Node newNode = new Node(data); head = newNode; return; } Node newNode = new Node(data); newNode.next = head; head = newNode; } // insert a new data after the given one public void insertAfter(int givenData, int newData){ if(head == null){ Node newNode = new Node(newData); head = newNode; return; } else{ Node currentNode = head; while(currentNode.next != null){ if(currentNode.data == givenData){ Node newNode = new Node(newData); newNode.next = currentNode.next; currentNode.next = newNode; } } } } // check if the list is empty public boolean isEmpty(){ return (head == null); } // prints the list public void print(){ if(isEmpty()){ System.out.println("Empty."); } Node currentNode = head; while(currentNode != null){ System.out.printf("%d ", currentNode.data); currentNode = currentNode.next; } }

// Removes the first element public Node removeFirst(){ Node first = head; head = head.next; return first; } // Removes the last element public void removeLast(){ if(head == null){ return; } Node current = head; while(current.next.next != null){ current = current.next; } current.next = null; } }

NODE CLASS_____________________________________________

public class Node{ int data; Node next; public Node(int data){ this.data = data; next = null; } }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Concepts Of Database Management

Authors: Joy L. Starks, Philip J. Pratt, Mary Z. Last

9th Edition

1337093424, 978-1337093422

More Books

Students also viewed these Databases questions