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

JAVA Write a class for a Stack of characters using a linked list implementation. Write a...
JAVA Write a class for a Stack of characters using a linked list implementation. Write a class for a Queue of characters using a linked list implementation. Write a class for a Queue of integers using a circular array implementation.
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. ·...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language...
IN JAVA LANGUAGE Linked List-Based Stack Implementation Implement Stack using a Linked List Use the language library LinkedList Stack methods will call the LinkedList methods You can use string as the object Instead of using an array, as the StackLab did, here you will use a Linked List from your language's library. Implement all the methods of Stack : push(), pop(), size(), printStackDown(), etc, using calls to the linked list methods that correspond to the actions need. In the array...
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 a program using Java language that is- Implement Stack with a linked list, and demonstrate...
write a program using Java language that is- Implement Stack with a linked list, and demonstrate that it can solve the Tower of Hanoi problem. Write implementation body of method “infixToPrefix(String[] e)” of class ArithmeticExpression to convert infix expressions into prefix expressions.
How to write code for stack with singly linked list using C? Please show examples for...
How to write code for stack with singly linked list using C? Please show examples for create, free, isempty, push, top, pop functions.
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
1a) Write the start of the class declaration for a node in a linked list (give...
1a) Write the start of the class declaration for a node in a linked list (give the name of the class and the instance variables). The name of the node should be SpecialNode. The data in each SpecialNode will include both a String and a Song object. b)Using the SpecialNode class you created in question above, write a constructor forSpecialNode that has three parameters and initializes all the SpecialNode instance variables. c) Write a line of code to instantiate a...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT