Question

In: Computer Science

Create a generic Linked List that does NOT use the Java library linked list. Make sure...

Create a generic Linked List that does NOT use the Java library linked list. Make sure it contains or access a subclass named Node (also Generic). And has the methods: addFirst(), addLast(), add(), removeFirst(), removeLast() and getHead(). In a separate Java class provide a main that creates an instance of your LinkedList class that creates an instance of your LinkedList that contains String types. Add the five names (you pick them) to the list and then iterate through the list to print all the names.

Solutions

Expert Solution


import java.io.*;
class Node <T>{ 

        T data; 
        Node next; 

        // Constructor 
        Node(T d) 
        { 
                data = d; 
                next = null; 
        } 
} 


// Java program to implement 
// a Singly Linked List 
class LinkedList <T>{ 

        Node head=null,tail=null;
        
        void addFirst(T val) {
                if(head==null) {
                        head = tail =new Node(val);
                }else {
                        Node temp=head;
                        head = new Node(val);
                        head.next = temp;
                }
        }
        
        void addLast(T val) {
                if(head==null) {
                        head = tail =new Node(val);
                }else {
                        tail.next = new Node(val);
                        tail = tail.next;
                }
        }
        
        
        void removeFirst() {
                if(head==null) {
                        return;
                }
                
                head= head.next;
        }
        void removeLast() {
                if(head==null) {
                        return;
                }
                Node temp=head;
                
                while(temp.next.next!=null) {
                        temp = temp.next;
                }
                
                tail = temp;
        }
        
        
        T getHead() {
                return (T) head.data;
        }
        
        void printList() {
                Node temp=head;
                
                while(temp!=null) {
                        System.out.print(temp.data + " ");
                        temp = temp.next;
                }
                System.out.println();
        }
        

        
} 


class Main{
        // **************MAIN METHOD************** 

                
                public static void main(String[] args) 
                { 
                        
                        LinkedList<String> list = new LinkedList<String>(); 
                        
                        list.addFirst("Jack");
                        list.addLast("Heron");
                        list.addFirst("Steve");
                        list.addLast("Carla");
                        list.addFirst("John");

                        
                        list.printList();
                        
                } 
}

OUTPUT:


Related Solutions

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...
Use C++ please You will be building a linked list. Make sure to keep track of...
Use C++ please You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt)...
Using Linked List, create a Java program that does the following without using LinkedList from the...
Using Linked List, create a Java program that does the following without using LinkedList from the Java Library. and please include methods for each function. Create a menu that contains the following options : 1. Add new node at the end of LL. ( as a METHOD ) 2. Add new node at the beginning of LL. ( as a METHOD ) 3. Delete a node from the end of LL. ( as a METHOD ) 4. Delete a node...
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO)....
by java Implement a linked list generic queue. Remember queues are first in first out (FIFO). Use the driver to then test each of the methods. Simply download it and place it with the other source files. Create a class GenLLQueue which has the following: Internal class ListNode which contains: Instance variable data of type T Instance variable link of type ListNode Default constructor that sets both instance variables to null Instance Variables head which is of type ListNode which...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with...
c. You are given the following Java files: SLL.java that implements generic Singly Linked List, with class SLLNode listed as inner class. TestIntegerSLL.java that tests the SLL class by using a linked list of Integer. In SLL class add the following method:                                                                    public void moveToEnd (int i) It will move the element at the i -th position to the end of the list. You can assume i to be within the list, and that the first element has the...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks...
plz use doubly linked list. java Q1) Create a program that do the following: 1. Asks the user to enter n marks for n students, read the marks and the names and store them in a double linked list. 2. Write a method to find the largest mark and print the name of the student having that mark 3. Write a method to print the content of the list (name, mark) 4. Write a method to search the list for...
First, create a project that creates an ordered linked list class (use the code provided, make...
First, create a project that creates an ordered linked list class (use the code provided, make sure to update the insert function so that it maintains an ordered list). To this class, add a function that prints the list in REVERSE order. Making use of recursion is the easiest and best way to do this, but certainly not the only way. Submit a .zip of your entire project.
You will be building a linked list. Make sure to keep track of both the head...
You will be building a linked list. Make sure to keep track of both the head and tail nodes. (1) Create three files to submit. PlaylistNode.h - Class declaration PlaylistNode.cpp - Class definition main.cpp - main() function Build the PlaylistNode class per the following specifications. Note: Some functions can initially be function stubs (empty functions), to be completed in later steps. Default constructor (1 pt) Parameterized constructor (1 pt) Public member functions InsertAfter() - Mutator (1 pt) SetNext() - Mutator...
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given....
**JAVA** Create a Linked List and conduct the following operations. Portion of the program is given. The operations are: Add an “H” to the list Add an “I” to the list Add “100” to the list Print the content of the list and its size Add a “H” to the first place of the list Add a “R” to the last place of the list Get the element of position 3 and print it Get the last element and print...
Make following changes. step(A) step 1 Implementation a Generic Linked List using the program developed in...
Make following changes. step(A) step 1 Implementation a Generic Linked List using the program developed in the class. step 2  Implement StackImpl class using the generic linked list. step 3 Test the program with different type and number of matching and un-matching brackets. This is how it works! When you run the class MatchBrackets a popup dialog appears asking you to select a file. You are provided two input files in the project. input1.txt contains matching brackets and input2.txt does not...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT