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
Write C++ programs to implement Queue ADT data structure using Linked List.
Write C++ programs to implement Queue ADT data structure using Linked List.
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...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application...
Write a C++ or Java application to create BOTH Stack & Queue data structures. The application also creates a "DisplayStackElement" and "DisplayQueueElement" routine. The application must be menu driven (with an option to terminate the application) and provide the following features. Allow insertion of a "Circle" object/structure in the Stack data structures. The Circle contains a "radius" data member. The Circle also uses functions/methods "setRadius", "getRadius" and calculateArea (returns a double data type). Allow insertion of a "Circle" object/structure in...
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...
Create: post fix calculator By Using: stack concept LinkedList is original line Queue is the waitlist...
Create: post fix calculator By Using: stack concept LinkedList is original line Queue is the waitlist Requirements - Orchestra passes are on sale. Enter first names of the people to form the line. Assume there is certain criteria that will have to be checked before purchase of passes (must be an annual ticket holder to purchase pass). You will have to eliminate those not meeting it from the line. A lottery will be done and the outcome will insert that...
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.
(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 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 }...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT