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.
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race...
The program (​ stack-ptr.c​ ) implements stack using a linked list, however, it contains a race condition and is not appropriate for a concurrent environment. Using Pthreads mutex locks, fix the race condition. For reference, see Section 7.3.1 of SGG book.(Section 7.3.1 is about mutex and semaphores it does explain how to implement I'm just having a hard time finding the race condition within the code) /* * Stack containing race conditions */ #include #include #include typedef int value_t; //...
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...
Can you make this singular linked list to doubly linked list Create a Doubly Linked List....
Can you make this singular linked list to doubly linked list Create a Doubly Linked List. Use this to create a Sorted Linked List, Use this to create a prioritized list by use. Bring to front those links recently queried. -----link.h------ #ifndef LINK_H #define LINK_H struct Link{ int data; Link *lnkNxt; }; #endif /* LINK_H */ ----main.cpp---- //System Level Libraries #include <iostream> //I/O Library using namespace std; //Libraries compiled under std #include"Link.h" //Global Constants - Science/Math Related //Conversions, Higher Dimensions...
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)
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT