Question

Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure.

Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure.

Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data structure. Do not use the built-in Java Stack class or the built-in Java Queue interface or the built-in Java linked list (you should create your own code for these classes).

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//Java program

//node class
class Node{
   private int data;
   private Node next;
  
   public Node(int d) {
       data = d;
       next=null;
   }
   public int getData() {
       return data;
   }
   public Node getNext() {
       return next;
   }
   public void setNext(Node n) {
       next = n;
   }
}
//stack class
class Stack{
   private Node top;
  
   public Stack() {
       top=null;
   }
   public void push(int d) {
       Node node = new Node(d);
       node.setNext(top);
       top = node;
   }
   public void pop() {
       if(top==null)return;
       top = top.getNext();
   }
   public int getTop() {
       return top.getData();
   }
}

//Queue class
class Queue{
   private Node front,rear;
  
   public Queue() {
       front=null;
       rear = null;
   }
   public void enqueue(int d) {
       Node node = new Node(d);
       if(rear==null) {
           rear = node;
           front = rear;
           return;
       }
       rear.setNext(node);
       rear = node;
   }
   public void dequeue() {
       if(front==null)return;
       if(front==rear) {
           front=null;
           rear=null;
           return;
       }
       front = front.getNext();
   }
   public int getFront() {
       return front.getData();
   }
}


Add a comment
Know the answer?
Add Answer to:
Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

    Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...

  • » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...

    Using C Please comment » Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions that take a stack o void push(Stack * stack, int value) ● int pop(Stack * stack) Prompt the user to input 5 integers, and use the stack to reverse the integers Print the result to the screen. o o » Part B: Queue o Create a Queue struct that is a wrapper for...

  • Basic data structures such as arrays are not sufficient for some applications. In some cases, more...

    Basic data structures such as arrays are not sufficient for some applications. In some cases, more advanced data structures are more suitable. In this assignment you will examine Stacks and Queues using linked lists. Complete the following: Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure.

  • e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return t...

    e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...

  • C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked...

    C++ queue data sructure ising linked list. Design and implement a Queue data structure using linked list. Support the following usual operations: (a) default constructor (b) parameterized constructor to create a queue of user-specified capacity (c) enqueue (d) dequeue (e) is_full (f) is_empty display (h) destructor that deallocates all nodes (i) copy constructor (j) overloaded assignment operator Demonstrate using a main function.

  • 1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push(...

    1. a. Stack b. Queue c. Priority Queue d. List - (ADTs)  Given the following steps: push( "Jane" ); push( "Jess" ); push( "Jill" ); push( pop() ); push( "Jim" ); String name = pop(); push( peek() ); Write separate programs for each of the data structures Stack, Queue, PriorityQueue, and List. Use the appropriate push(), pop(), peek() for each of the respective ADT's. Use the Java class library of the ADT's as opposed to the author's implementation. What is in...

  • 3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain...

    3. Some circular queue implementations use the mod operator % in enqueue and dequeue operations. Explain why this is inefficient. 4. If a queue is implemented using a singly linked list with a head and tail pointer, you should always insert at the tail and remove from the head. Explain why this is so. 5. What is a Priority Queue, and how does it differ from a standard queue? 6. Priority Queues are almost always implemented with an ordered data...

  • I already created a doubly linked list class. I need help doing this and adding the...

    I already created a doubly linked list class. I need help doing this and adding the doubly linked list class to this. I need this for Java language if someone can please help me Step 2 Stack and Queue Using the linked list class you created in Step 1 create stack and queue classes. Iwill leave it up to you as to whether to use composition or inheritance but whatever way you choose to go you should be able to...

  • Implement the stack queue data structure with a linked list implementation to get the given test...

    Implement the stack queue data structure with a linked list implementation to get the given test code in driver.cpp to work properly: driver.cpp code: #include <iostream> #include "stackLL.h" using namespace std; int main() { /////////////Test code for stack /////////////// stackLL stk; stk.push(5); stk.push(13); stk.push(7); stk.push(3); stk.push(2); stk.push(11); cout << "Popping: " << stk.pop() << endl; cout << "Popping: " << stk.pop() << endl; stk.push(17); stk.push(19); stk.push(23); while( ! stk.empty() ) { cout << "Popping: " << stk.pop() << endl; }...

  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT