Question

In: Computer Science

In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may...

In Java or C++, implement a stack and a queue using a linkedlist data structure.  You may not use any standard Java or C++ libraries.

Assume your data structure only allows Strings. Implement the following operations for the data structure:

Queue: enqueue, dequeue, create, isEmpty (10 points)

Stack: push, pop, create, isEmpty (10 points)

Here is a link to get started on transferring from Java to C++ http://www.horstmann.com/ccj2/ccjapp3.html (Links to an external site.)

Upload a zip file with one implementation for your Stack and one for your Queue, along with any classes required to implement your linkedlist (i.e. nodes for linkedlist). In each file include a test method which shows how you've tested each operation.

Solutions

Expert Solution

Here is the completed code for this problem in Java. Two files are attached – LinkedQueue.java that represents linked list representation of a Queue and LinkedStack.java that represents linked list representation of a Stack, with main methods in both files for testing. Comments are included, go through it, learn how things work and let me know if you have any doubts or if you need anything to change. If you are satisfied with the solution, please rate the answer. Thanks

// LinkedQueue.java

public class LinkedQueue {

      //references to front and rear node

      private Node front, rear;

      // private inner class representing a Node in linked list representation of

      // the queue

      private class Node {

            private String item;

            private Node next;

            // constructor initializing item and next node

            public Node(String item, Node next) {

                  this.item = item;

                  this.next = next;

            }

      }

      // constructor, creates an empty queue

      public LinkedQueue() {

            create();

      }

      // method to create/reset an empty queue

      public void create() {

            front = null;

            rear = null;

      }

      // returns true if queue is empty

      public boolean isEmpty() {

            return (front == null);

      }

      // adds an element to the rear

      public void enqueue(String item) {

            // creating a new node

            Node newNode = new Node(item, null);

            // if queue is currently empty, adding as both front and rear

            if (isEmpty()) {

                  front = newNode;

                  rear = newNode;

            } else {

                  // appending to rear and updating rear

                  rear.next = newNode;

                  rear = newNode;

            }

      }

      // removes and returns the front value, will return null if queue is empty

      public String dequeue() {

            if (front != null) {

                  // getting item on front

                  String item = front.item;

                  // updating front

                  front = front.next;

                  // if front became null, setting rear to null

                  if (front == null) {

                        rear = null;

                  }

                  // returning removed value

                  return item;

            }

            return null;// empty

      }

     

      //main method for testing linked queue

      public static void main(String[] args) {

            // creating a queue, adding some strings

            LinkedQueue que = new LinkedQueue();

            que.enqueue("hello");

            que.enqueue("world");

            que.enqueue("how");

            que.enqueue("are");

            que.enqueue("you");

            // looping until que is empty

            while (!que.isEmpty()) {

                  // dequeing and displaying front element

                  System.out.println(que.dequeue());

            }

      }

}

// LinkedStack.java

public class LinkedStack {

      private Node top;

      // private inner class representing a Node in linked list representation of

      // the stack

      private class Node {

            private String item;

            private Node next;

            // constructor initializing item and next node

            public Node(String item, Node next) {

                  this.item = item;

                  this.next = next;

            }

      }

      // constructor, creates an empty stack

      public LinkedStack() {

            create();

      }

      // method to create/reset an empty stack

      public void create() {

            top = null;

      }

      // returns true if stack is empty

      public boolean isEmpty() {

            return (top == null);

      }

      // pushes an element to the top

      public void push(String item) {

            // adding a new node before top and updating top

            top = new Node(item, top);

      }

      // removes and returns the top value, will return null if stack is empty

      public String pop() {

            if (top != null) {

                  // getting item on top

                  String item = top.item;

                  // updating top

                  top = top.next;

                  // returning removed value

                  return item;

            }

            return null;// empty

      }

     

      //main method for testing linked stack

      public static void main(String[] args) {

            // creating a stack, pushing some strings

            LinkedStack stack = new LinkedStack();

            stack.push("hello");

            stack.push("world");

            stack.push("how");

            stack.push("are");

            stack.push("you");

            // looping until stack is empty

            while (!stack.isEmpty()) {

                  // popping and displaying top element

                  System.out.println(stack.pop());

            }

      }

}

/*OUTPUT of LinkedQueue program*/

hello

world

how

are

you

/*OUTPUT of LinkedStack program*/

you

are

how

world

hello


Related Solutions

C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you...
C++ Data Structures: Implement a Stack and a Queue using Linked list In this lab you will implement the functionality of a stack and a queue using a linked list. Your program must use of the declaration of the Stack and Queue class in Stack.h and Queue.h You have to implement the functionalities of queue (enq, deq, displayQueue) in a file called Queue.cpp. All the functions in Queue.cpp should follow the prototypes declared in Queue.h. Your code should make use...
Implement a stack using a single queue. In particular, you are given a queue Q that...
Implement a stack using a single queue. In particular, you are given a queue Q that provides the method Q.size() to return its size at any point and the standard methods of queues (i.e, Q.enqueue(x) and Q.dequeue()). The requirement is to use such methods of Q to implement two methods S.push(x) and S.pop() for a stack S. What are the running times of your methods? Kindly answer using python programming
In this lab, using C++, you will create two data structures: a stack and a queue....
In this lab, using C++, you will create two data structures: a stack and a queue. You will use STL containers to demonstrate basic ADTs. Queue For the queue, you will simulate a buffer. Remember it is first-in-first-out. The user will enter a number for the number of rounds to run your simulation. You need one function that randomly generates a number. You will also have a user specified percentage, and the function uses this percentage to randomly put the...
Requirements Both the stack and queue were implemented correctly according data structure and java standard practices....
Requirements Both the stack and queue were implemented correctly according data structure and java standard practices. The stack was used to implement a post-fix calculator using push, pop, peek and other stack concepts. The queue was used to implement the palindrome using add, remove, insert and other queue concepts. Both implementations produced the expected output. All submission requirements were followed. Test Harness for the first part: public class StackCalcTest { public static void main(String[] args) { StackCalc stackCalc = new...
Are you able to implement a stack using just one queue? If yes, please provide the...
Are you able to implement a stack using just one queue? If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If yes, please provide the pop(only) algorithm to simulate the stack operations with just one queue. If no, explain.
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class...
Define empty methods in Queue class using LinkedList class in Java ------------------------------------------------------------------------------- //Queue class public class Queue{ public Queue(){ // use the linked list } public void enqueue(int item){ // add item to end of queue } public int dequeue(){ // remove & return item from the front of the queue } public int peek(){ // return item from front of queue without removing it } public boolean isEmpty(){ // return true if the Queue is empty, otherwise false }...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only),...
(using single linkedlist c++)In this assignment, you will implement a Polynomial linked list(using single linkedlist only), the coefficients and exponents of the polynomial are defined as a node. The following 2 classes should be defined. p1=23x 9 + 18x 7+3 1. Class Node ● Private member variables: coefficient (double), exponents (integer), and next pointer. ● Setter and getter functions to set and get all member variables ● constructor 2. Class PolynomialLinkedList ● Private member variable to represent linked list (head)...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class...
Define empty methods in Stack class using LinkedList class in Java ------------------------------------------------------------------------------- //Stack class public class Stack{ public Stack(){ // use LinkedList class } public void push(int item){ // push item to stack } public int pop(){ // remove & return top item in Stack } public int peek(){ // return top item in Stack without removing it } public boolean isEmpty(){ // return true if the Stack is empty, otherwise false } public int getElementCount(){ // return current number...
0. Introduction. In this assignment you will implement a stack as a Java class, using a...
0. Introduction. In this assignment you will implement a stack as a Java class, using a linked list of nodes. Unlike the stack discussed in the lectures, however, your stack will be designed to efficiently handle repeated pushes of the same element. This shows that there are often many different ways to design the same data structure, and that a data structure should be designed for an anticipated pattern of use. 1. Theory. The most obvious way to represent a...
in C++ For this program, you are going to implement a stack using an array and...
in C++ For this program, you are going to implement a stack using an array and dynamic memory allocation. A stack is a special type of data structure that takes in values (in our case integers) one at a time and processes them in a special order. Specifically, a stack is what's called a first-in-last-out (FILO) data structure. That is to say, the first integer inserted into the stack is the last value to be processed. The last value in...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT