Creating a LinkedList class from scratch

后端 未结 11 782
慢半拍i
慢半拍i 2020-12-02 06:52

We were given an assignment to create a LinkedList from scratch, and there are absolutely no readings given to guide us on this migrane-causing task. Also everything online

11条回答
  •  旧时难觅i
    2020-12-02 07:23

    Pleas find bellow Program

    class Node {
        int data;
        Node next;
    
        public Node(int data) {
            this.data = data;
            this.next = null;
        }
    }
    
    public class LinkedListManual {
    
        Node node;
    
        public void pushElement(int next_node) {
            Node nd = new Node(next_node);
            nd.next = node;
            node = nd;
        }
    
        public int getSize() {
            Node temp = node;
            int count = 0;
            while (temp != null) {
                count++;
                temp = temp.next;
            }
            return count;
        }
    
        public void getElement() {
            Node temp = node;
            while (temp != null) {
                System.out.println(temp.data);
                temp = temp.next;
            }
        }
    
        public static void main(String[] args) {
            LinkedListManual obj = new LinkedListManual();
            obj.pushElement(1);
            obj.pushElement(2);
            obj.pushElement(3);
            obj.getElement(); //get element
            System.out.println(obj.getSize());  //get size of link list
        }
    
    }
    

提交回复
热议问题