linked list data structure
Operation in java of linked list
/* * Java Program to Implement Singly Linked List */ import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(int d,Node n) { data = d; link = n; } /* Function to set link to next Node */ public void setLink(Node n) { link = n; } /* Function to set data to current Node */ public void setData(int d) { data = d; } /* Function to get link to next node */ public Node getLink() { return link; } /* Function to get data from current Node */ public int getData() { return data; } } /* Class linkedList */ class linkedList { protected Node start; protected Node end ; public int size ; /* Constructor */ public linkedList() { start = null; end = null; size = 0; } /* Function to check if list is empty */ public boolean isEmpty() { return start == null; } /* Function to get size of list */ public int getSize() { return size; } /* Function to insert an element at begining */ public void insertAtStart(int val) { Node nptr = new Node(val, null); size++ ; if(start == null) { start = nptr; end = start; } else { nptr.setLink(start); start = nptr; } } /* Function to insert an element at end */ public void insertAtEnd(int val) { Node nptr = new Node(val,null); size++ ; if(start == null) { start = nptr; end = start; } else { end.setLink(nptr); end = nptr; } } /* Function to insert an element at position */ public void insertAtPos(int val , int pos) { Node nptr = new Node(val, null); Node ptr = start; pos = pos - 1 ; for (int i = 1; i < size; i++) { if (i == pos) { Node tmp = ptr.getLink() ; ptr.setLink(nptr); nptr.setLink(tmp); break; } ptr = ptr.getLink(); } size++ ; } /* Function to delete an element at position */ public void deleteAtPos(int pos) { if (pos == 1) { start = start.getLink(); size--; return ; } if (pos == size) { Node s = start; Node t = start; while (s != end) { t = s; s = s.getLink(); } end = t; end.setLink(null); size --; return; } Node ptr = start; pos = pos - 1 ; for (int i = 1; i < size - 1; i++) { if (i == pos) { Node tmp = ptr.getLink(); tmp = tmp.getLink(); ptr.setLink(tmp); break; } ptr = ptr.getLink(); } size-- ; } /* Function to display elements */ public void display() { System.out.print("\nSingly Linked List = "); if (size == 0) { System.out.print("empty\n"); return; } if (start.getLink() == null) { System.out.println(start.getData() ); return; } Node ptr = start; System.out.print(start.getData()+ "->"); ptr = start.getLink(); while (ptr.getLink() != null) { System.out.print(ptr.getData()+ "->"); ptr = ptr.getLink(); } System.out.print(ptr.getData()+ "\n"); } } /* Class SinglyLinkedList */ public class SinglyLinkedList { public static void main(String[] args) { Scanner scan = new Scanner(System.in); /* Creating object of class linkedList */ linkedList list = new linkedList(); System.out.println("Singly Linked List Test\n"); char ch; /* Perform list operations */ do {
single linked list class
Public class slinkedlist { protected node head //head node of the list protected long size//numbers of nodes in list Public slinkedlist (){ head=null; size=0; } }
linked list add first operation
Public void addFirst (any type element){ head = new node <anytype>(element,head); }
why we used linked list vs array
as array is static structure not easily to be increased in sized and array is expensive in insert and delete new items
each element in linked list
is a seperate object
linked list type
it is dynamic data structure so we can added and delete easily than array and increased in size zoo easy than array
linked list def
linear data structure where each element is a separate object , and each element of a list comprised of two items the data and the reference to the next node
___________________ |_____data__|____refrence to next node __|______|
linked lsit graph
disadvantage of linkedlist
not allow to direct acces to element and use more memory comparing to array
linkedlist node class
public class node { private int element ; private node next; Public node (int a,node n){// this create node with the data and a reference to the next element element =s next =n } Public int getElement (){return element;}//this return element of this node that mean data Public node getNext (){return next ;}// return the next node of that node Public void set element (int newelemnt){element =new element }//set element of node Public void setnext (node nextnode){next=new next}//set the next node of that node }
types of linked list
singly linked list doubly linked list circular linked list
what are the last node in linked list referenced to
the last node in linked list referenced to null
insertat head in slinkedlist
void addfirst (node n) { n.setnext (head); head=n; size++; } work when the list is empty and have O(1)