Question

In: Computer Science

(a) Write a stack class that is based on a linked list. It can be just...

(a) Write a stack class that is based on a linked list. It can be just pop(), push(), and anything you need for those methods or testing.

(b) Write a queue class that is based on a linked list. As above, it can be just enqueue() and dequeue(), as well as anything you need for those methods or testing.

(c) Write some test cases, trying to include edge cases. Why did you choose those tests? Did you get the results you expected?

Solutions

Expert Solution

save the file name as LinkedList.java before compiling for both the parts

(A) Stack class based on a linkedlist with functions to pop() and push() with cornor case testing

public class LinkedList {

// defining the linkedlist with node and value
   public Node head;
   public class Node {
       int value;
       Node next;
       public Node(int value) {
           this.value = value;
           this.next = null;
       }
   }
// function to push elements to stack
   public void push(int data) {
       Node temp = new Node(data);
       temp.next = this.head;
       this.head = temp;
   }
  

//function to pop elements from stack
   public int pop() {

//if the stack is empty and you use pop funtion the function will return -1

if(this.head==null){
return -1;
   }
       int popValue = this.head.value;
       this.head = this.head.next;
       return popValue;
   }
  
   public static void main(String[] args) {
       LinkedList linkedList = new LinkedList();
       linkedList.head = null;
      
       linkedList.push(1);
       linkedList.push(2);
      
       System.out.println(linkedList.pop());
       System.out.println(linkedList.pop());
       System.out.println(linkedList.pop()); // edge case when there is nothing in the stack
   }
  
}

(B) Queue class based on linked list with queue() and deque() funtions with cornor case testing

public class LinkedList {
   public Node head;
   // defining the linkedlist with node and value
   public class Node {
       int value;
       Node next;
       public Node(int value) {
           this.value = value;
           this.next = null;
       }
   }
  

// function to enqueus elements to queue
   public void queue(int data) {
       if(this.head==null){
   this.head = new Node(data);
   return;
   }
       Node temp = this.head;
       while(temp.next!=null) {
           temp = temp.next;
       }
       Node newNode = new Node(data);
       temp.next = newNode;
   }

// function to dequeue elements from queue
  
   public int deque() {

// is head is already empty and then you deque element it will return -1
       if(this.head==null){
   return -1;
   }
       int dequeValue = this.head.value;
       this.head = this.head.next;
       return dequeValue;
   }
  
   public static void main(String[] args) {
       LinkedList linkedList = new LinkedList();
       linkedList.head = null;
      
       linkedList.queue(1);
       linkedList.queue(2);
System.out.println(linkedList.deque());
       System.out.println(linkedList.deque());

System.out.println(linkedList.deque()); // dequeeing with no elements in the queue will return -1 i.e. cornor case
          }
   }

(C)

Test cases

Stack

linkedList.push(1);

linkedList.push(2);

linkedList.pop();

linkedList.pop();

linkedList.pop(); // cornor case will return -1 since no elements in stack

Queue

linkedList.queue(1);

linkedList.queue(2);

linkedList.deque()

linkedList.deque()

linkedList.deque() // cornor case will return -1 since no elements in queue


Related Solutions

Solve this Write a C++ class that implements a stack using a linked list. The type...
Solve this Write a C++ class that implements a stack using a linked list. The type of data contained in the stack should be double. The maximum size of the stack is 30. Implement the following methods: . · Constructor and destructor; // 5 pts · void push (double value); // pushes an element with the value into the stack. 5 pts. · double pop (); // pops an element from the stack and returns its value. 5 pts. ·...
Write a code to implement a python stack class using linked list. use these operations isEmpty...
Write a code to implement a python stack class using linked list. use these operations isEmpty   • push. • pop.   • peek. • size Time and compare the performances ( this is optional but I would appreciate it)
How do you implement stack by using linked list? No code just explain it.
How do you implement stack by using linked list? No code just explain it.
Write the code for postfix expression in C++ using a linked stack that can take numbers...
Write the code for postfix expression in C++ using a linked stack that can take numbers bigger than 9 (any size the user gives) and pushes the final result onto the top of the stack
Write in C++: create a Doubly Linked List class that holds a struct with an integer...
Write in C++: create a Doubly Linked List class that holds a struct with an integer and a string. It must have append, insert, remove, find, and clear.
This is the code what I have for doubly linked list for STACK. This is Python...
This is the code what I have for doubly linked list for STACK. This is Python language and I want anyone to help me with the following questions. Can you check for me if it is good Doubly Linked List? ####THIS IS THE ENTIRE ASSIGNMENT#### ADD the Following feature: Include a class attribute in the container class called name. In the implementation - Pod: You should ask the user to enter the name of the container and the program should...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack...
Exercise 3: Stack Write a program in Java to manipulate a Stack List: 1. Create Stack List 2. Display the list 3. Create the function isEmply 4. Count the number of nodes 5. Insert a new node in the Stack List. 6. Delete the node in the Stack List. 7. Call all methods above in main method with the following data: Test Data : Input the number of nodes : 4 Input data for node 1 : 5 Input data...
Write a code to implement a python queue class using a linked list. use these operations...
Write a code to implement a python queue class using a linked list. use these operations isEmpty • enqueue. • dequeue    • size Time and compare the performances of the operations ( this is optional but I would appreciate it)
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should...
b. Implement StackFromList, a templated stack class backed by the above singlylinked list. The stack should have a private linked list member, and utilize the linked list methods to implement its functionality. The stack should include a constructor, a destructor, a push, a pop, and an isEmpty method (which returns a bool). c. Implement, QueueFromList, a templated queue class backed by the above singlylinked list. The queue should have a private linked list member, and utilize the linked list methods...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or...
Implement the SimpleQueue interface with the MyQueue class. You can use either a linked list or a dynamic array to implement the data structure. A queue is a specialised form of list in which you can only get and remove the first element in the queue. The class should be able to work with the following code: SimpleQueue queue = new MyQueue(); NB: You cannot import anything from the standard library for this task. The data structure must be of...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT