Advertisement
ralphdc09

DoublyLinkedList-Ralph

Oct 25th, 2021
59
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.35 KB | None | 0 0
  1. package com.its101.prwk05_06;
  2.  
  3. public class DoublyLinkedList {
  4.  
  5.     // node of the doubly linked list
  6.     public static class Node {
  7.         String data;
  8.         Node previous;
  9.         Node next;
  10.  
  11.         public Node(String data) {
  12.             this.data = data;
  13.         }
  14.     }
  15.  
  16.     // declare head node and last node
  17.     Node head, last = null;
  18.  
  19.     // createNode() creates new node to the list
  20.     public void createNode(String data) {
  21.         // create new node
  22.         Node newNode = new Node(data);
  23.  
  24.         // if list is empty
  25.         if(head == null) {
  26.             // both head and last will point to newNode
  27.             head = last = newNode;
  28.             // head node's previous will point to null
  29.             head.previous = null;
  30.             // last node's next will point to null, as it is the last node of the list
  31.             last.next = null;
  32.         } else {
  33.             // newNode will be added after last node such that last node's next will point to newNode
  34.             last.next = newNode;
  35.             // newNode's previous will point to last node
  36.             newNode.previous = last;
  37.             // newNode will become new last node
  38.             last = newNode;
  39.             // it is last node, last node next will point to null
  40.             last.next = null;
  41.         }
  42.     }
  43.  
  44.     // printNode() prints the nodes of the list
  45.     public void printNode() {
  46.         // node current will point to head
  47.         Node current = head;
  48.         if (head == null) {
  49.             System.out.println("List is empty");
  50.             return;
  51.         }
  52.         System.out.println("Nodes of doubly linked list: ");
  53.         while (current != null) {
  54.             // prints each node by incrementing the pointer.
  55.  
  56.             System.out.print(current.data + " ");
  57.             current = current.next;
  58.         }
  59.         System.out.println();
  60.     }
  61.  
  62.     public static void main(String[] args) {
  63.  
  64.         DoublyLinkedList studentsList = new DoublyLinkedList();
  65.  
  66.         //Create new nodes to the Linked List and assign a data value
  67.         studentsList.createNode("ralph");
  68.         studentsList.createNode("espe");
  69.         studentsList.createNode("glen");
  70.         studentsList.createNode("nomer");
  71.         studentsList.createNode("angelo");
  72.  
  73.         //Prints the nodes-value present in the list
  74.         studentsList.printNode();
  75.  
  76.     }
  77. }
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement