Hello I need to create an implementation of a linked list in
Hello, I need to create an implementation of a linked list in Java. I use NetBeans 8.1. The linked list implementation should be in its own class named LinkedList. The linkedlist should hold integers and must contain the following methods:
Write a Stack class. This class should use your LinkedList class. Your stack should hold integers. It must contain the following methods:
Stack() – Default constructor
push(valueToAdd) – Adds a new element to the top of the
stack pop() – Removes the element from the top of the stack and returns its value.
getHeight() – Returns the height of the stack.
display() – Displays the stack and its contents.
It should display the top element first. See output below for an example.
 Write a Queue class. This class should use your LinkedList class.
Your queue should hold integers. It must contain the following methods:
Queue() – Default constructor
enqueue(valueToAdd) – Adds a new element to back of the queue
dequeue() – Removes the element from the front of the queue and returns its value.
getLength() – Returns the length of the
queue. display() – Displays the queue and its contents.
It should display the first element in the queue first. See output below for an example.
This is the LinkList.java:
public class LinkedListTest {
public static void main(String[] args) {
LinkedList list = new LinkedList();
System.out.println(\"Adding elements to the front...\");
list.addFront(8);
list.display();
list.addFront(33);
list.display();
list.addFront(58);
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Adding elements to the back...\");
list.addBack(1);
list.display();
list.addBack(34);
list.display();
list.addBack(20);
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Removing elements from the front...\");
list.removeFront();
list.display();
list.removeFront();
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
System.out.println();
System.out.println(\"Removing elements from the back...\");
list.removeBack();
list.display();
list.removeBack();
list.display();
System.out.println(\"Length of linked list: \"+list.getLength());
    
        System.out.println();
}
}
Stack Pushing elements. 33 >8 58 33- 8 Height of stack: 3 Popping elements. Popped: 58 33 >8 Popped: 33 Popped 8 Queue 17 >17 102 17- 2 Length of queue 3 De queued: 2 102- 17 De queued: 17 De queued: 102Solution
stack implementation:
 
 package listclasses;
 public class singlelinkedlist {
    int data;
    singlelinkedlist next;
    singlelinkedlist(int data){
        this.data=data;
       
    }
    singlelinkedlist(int data,singlelinkedlist next){
        this.data=data;
        this.next=next;
    }
    public String tostring(){
        return data+\"\";
    }
 }
 main. class:
 package listclasses;
 import java.util.*;
 public class singlelinkedlistmain {
 static singlelinkedlist head;
 static int size;
 static int top;
 static {
    head=null;
    size=0;
    top=5;
 }
    public static void main(String[] args) {
      
        push(10);
        push(10);
        push(10);
        push(10);
        push(10);
 
   
    display();
    System.out.println(\"\ height:\"+size);
      
      
    display();
   
    pop();
    display();
    }
    public static void push(int data){
        if(size<top){
        if(head==null){
            head=new singlelinkedlist(data,null);
           
        }else{
            singlelinkedlist curent=head;
            while(curent.next!=null){
                curent=curent.next;
            }
            singlelinkedlist newlist=new singlelinkedlist(data,null);
            curent.next=newlist;
        }
           
        size++;}else
            System.out.println(\"\ stack overflow\");
    }
    public static void pop(){
        if(head==null){
            System.out.println(\"\ stack undflow\");
            return;
        }
        if(head.next==null){
            head=null;
            size--;
            return;
        }
              
        singlelinkedlist curent=head;
        while(curent.next.next!=null){
            curent=curent.next;
        }
        curent.next=null;
        size--;
          
    }
    public static void display(){
        singlelinkedlist curent=head;
        while(curent!=null){
            System.out.print(curent.data+\" \");
            curent=curent.next;
        }
        //System.out.println(\"\ \"+size);
       
    }
 public static int height(){
        return size;
    }
 ouput:
 push and pop of elements;
 10 20 30 40 50
 height:5
 10 20 30 40
 
 
 
 queue implementation:
 package listclasses;
 import java.util.*;
 public class singlelinkedlistmain {
 static singlelinkedlist head;
 static int size;
 static int rear;
 static {
    head=null;
    size=0;
    rear=5;
 }
    public static void main(String[] args) {
      
        enque(10);
        enque(20);
        enque(30);
        enque(40);
        enque(50);
 
   
    display();
    System.out.println(\"\ height:\"+size);
    dequee();
      
    display();
       }
 public static void dequee(){
        if(head==null){
            System.out.println(\"queue is empty:\");
            return;
        }
        if(head.next!=null){
            head=head.next;
            size--;
        }
       
    }
 public static void enque(int data){
        if(size<rear){
        singlelinkedlist curnt=head;
        if(head==null){
            head=new singlelinkedlist(data,null);
            size++;
           
        }else{
            while(curnt.next!=null){
                curnt=curnt.next;
            }
            singlelinkedlist newlist=new singlelinkedlist(data,null);
            curnt.next=newlist;
            size++;
        }
        }else{
            System.out.println(\"queeu is full:\");
        }
       
    }
 public static int height(){
        return size;
    }
 public static void display(){
        singlelinkedlist curent=head;
        while(curent!=null){
            System.out.print(curent.data+\" \");
            curent=curent.next;
        }
}
}
output:
10 20 30 40 50
 height:5
 20 30 40 50





