Header Linked List in Data Structure – The Crazy Programmer

class Node {    int data;    Node next;    Node(int data)    {        this.data = data;        next = null;    }} // Header Node Classclass Header_Node{    int count; // count of nodes.    int max;   // max value among all nodes.    int min;   // min value among all nodes.        Node next; // reference to beginning of the actual list.        Header_Node()    {        count = 0;        max = Integer.MIN_VALUE;        min = Integer.MAX_VALUE;        next = null;    }}  public class HeaderLinkedList {    static Node last; // maintains Last node of the list.    // print the linked list elements  static void printList(Header_Node head)  {    Node curr = head.next;        // We print header node separately.    System.out.print(“[“+head.count+” “+head.max+” “+head.min+”] -> “);        // then traverse and print actual nodes.    while (curr != null)     {     if(curr.next!=null)            System.out.print(curr.data + ” -> “);     else        System.out.print(curr.data);             curr = curr.next;    }    System.out.println();  }    //add nodes to end of the list.  static void addNode(Header_Node head,Node node)  {      // if there are no nodes and only header node is present.      if(head.next==null)      {      head.next = node;      last = node;      }            // Otherwise add it to the end.      else      {       last.next = node;       last = node;      }            // Update max and min of list accordingly.      if(head.max < node.data)      {          head.max = node.data;      }      if(head.min > node.data)      {          head.min = node.data;      }            // Increment count of nodes.      head.count = head.count+1;        }    // Deletes node from end of the list.  static void deleteFromEnd(Header_Node head)  {         // if node to delete is min value node   // We need to update the 2nd minimum to header node then delete it.   if(last.data == head.min)   {       int new_min = Integer.MAX_VALUE;              Node start = head.next;              while(start.next.next!=null)       {       start = start.next;       new_min = Math.min(new_min,start.data);       }       // Print Node to Delete.       System.out.println(“Node deleted is: “+start.next.data);              //Updating head min.       head.min = new_min;              start.next = null;       last = start;   }      // if node to delete is max value node   // We need to update the 2nd maximum to header node then delete it.   else if(last.data == head.max)   {       int new_max = Integer.MIN_VALUE;       Node start = head.next;       while(start.next.next!=null)       {       start = start.next;       new_max = Math.max(new_max,start.data);       }       // Print Node to Delete.       System.out.println(“Node deleted is: “+start.next.data);              //Updating head max       head.min = new_max;               start.next = null;       last = start;   }   // Otherwise we do normal deletion.   else   {   Header_Node current = head;   Node start = current.next;    while(start.next.next!=null)   {       start=start.next;   }   // Print Node to Delete.   System.out.println(“Node deleted is: “+start.next.data);      start.next = null;   last = start;   }      // decrement count of nodes in header node.   head.count = head.count-1;  }  // Driver Code to Test Above Methods.  public static void main(String[] args)  {    // Creating the Header Node.    Header_Node head = new Header_Node();        // Adding the nodes to end of list.     addNode(head,new Node(99));    addNode(head,new Node(37));    addNode(head,new Node(5));    addNode(head,new Node(25));        System.out.print(“The Nodes in Linked List : “);    printList(head);        // Printing Contents of Head Node.    System.out.println(“Total Nodes in Linked List: “+head.count);    System.out.println(“Maximum Item in List: “+head.max);    System.out.println(“Minimum Item in List: “+head.min);    System.out.println();        // Deleting node from end of list.    deleteFromEnd(head);    System.out.println();        System.out.print(“The Nodes in Linked List : “);    printList(head);        System.out.println(“Total Nodes in Linked List: “+head.count);    System.out.println(“Maximum Item in List: “+head.max);    System.out.println(“Minimum Item in List: “+head.min);    System.out.println();        deleteFromEnd(head);    System.out.println();        System.out.print(“The Nodes in Linked List : “);    printList(head);        System.out.println(“Total Nodes in Linked List: “+head.count);    System.out.println(“Maximum Item in List: “+head.max);    System.out.println(“Minimum Item in List: “+head.min);    System.out.println();        }}

Coded at

Share your love

Leave a Reply