Bitcoin

Bitcoin
Bitcoin

Linked List

In computer science, a linked list is a linear collection of data elements, in which linear order is not given by their physical placement in memory. Each pointing to the next node by means of a pointer. It is a data structure consisting of a group of nodes which together represent a sequence. Under the simplest form, each node is composed of data and a reference (in other words, a link) to the next node in the sequence. This structure allows for efficient insertion or removal of elements from any position in the sequence during iteration. More complex variants add additional links, allowing efficient insertion or removal from arbitrary element references.

A linked list whose nodes contain two fields: an integer value and a link to the next node. The last node is linked to a terminator used to signify the end of the list.
A CODE FOR LINKED LIST
/***************************************************************************
 * A Linked List class with a private static inner Node class.
 *
 *****************************************************************************/
  1. import java.util.Scanner;
  2.  
  3. /*  Class Node  */
  4. class Node
  5. {
  6.     protected int data;
  7.     protected Node link;
  8.  
  9.     /*  Constructor  */
  10.     public Node()
  11.     {
  12.         link = null;
  13.         data = 0;
  14.     }    
  15.     /*  Constructor  */
  16.     public Node(int d,Node n)
  17.     {
  18.         data = d;
  19.         link = n;
  20.     }    
  21.     /*  Function to set link to next Node  */
  22.     public void setLink(Node n)
  23.     {
  24.         link = n;
  25.     }    
  26.     /*  Function to set data to current Node  */
  27.     public void setData(int d)
  28.     {
  29.         data = d;
  30.     }    
  31.     /*  Function to get link to next node  */
  32.     public Node getLink()
  33.     {
  34.         return link;
  35.     }    
  36.     /*  Function to get data from current Node  */
  37.     public int getData()
  38.     {
  39.         return data;
  40.     }
  41. }
  42.  
  43. /* Class linkedList */
  44. class linkedList
  45. {
  46.     protected Node start;
  47.     protected Node end ;
  48.     public int size ;
  49.  
  50.     /*  Constructor  */
  51.     public linkedList()
  52.     {
  53.         start = null;
  54.         end = null;
  55.         size = 0;
  56.     }
  57.     /*  Function to check if list is empty  */
  58.     public boolean isEmpty()
  59.     {
  60.         return start == null;
  61.     }
  62.     /*  Function to get size of list  */
  63.     public int getSize()
  64.     {
  65.         return size;
  66.     }    
  67.     /*  Function to insert an element at begining  */
  68.     public void insertAtStart(int val)
  69.     {
  70.         Node nptr = new Node(val, null);    
  71.         size++ ;    
  72.         if(start == null) 
  73.         {
  74.             start = nptr;
  75.             end = start;
  76.         }
  77.         else 
  78.         {
  79.             nptr.setLink(start);
  80.             start = nptr;
  81.         }
  82.     }
  83.     /*  Function to insert an element at end  */
  84.     public void insertAtEnd(int val)
  85.     {
  86.         Node nptr = new Node(val,null);    
  87.         size++ ;    
  88.         if(start == null) 
  89.         {
  90.             start = nptr;
  91.             end = start;
  92.         }
  93.         else 
  94.         {
  95.             end.setLink(nptr);
  96.             end = nptr;
  97.         }
  98.     }
  99.     /*  Function to insert an element at position  */
  100.     public void insertAtPos(int val , int pos)
  101.     {
  102.         Node nptr = new Node(val, null);                
  103.         Node ptr = start;
  104.         pos = pos - 1 ;
  105.         for (int i = 1; i < size; i++) 
  106.         {
  107.             if (i == pos) 
  108.             {
  109.                 Node tmp = ptr.getLink() ;
  110.                 ptr.setLink(nptr);
  111.                 nptr.setLink(tmp);
  112.                 break;
  113.             }
  114.             ptr = ptr.getLink();
  115.         }
  116.         size++ ;
  117.     }
  118.     /*  Function to delete an element at position  */
  119.     public void deleteAtPos(int pos)
  120.     {        
  121.         if (pos == 1) 
  122.         {
  123.             start = start.getLink();
  124.             size--; 
  125.             return ;
  126.         }
  127.         if (pos == size) 
  128.         {
  129.             Node s = start;
  130.             Node t = start;
  131.             while (s != end)
  132.             {
  133.                 t = s;
  134.                 s = s.getLink();
  135.             }
  136.             end = t;
  137.             end.setLink(null);
  138.             size --;
  139.             return;
  140.         }
  141.         Node ptr = start;
  142.         pos = pos - 1 ;
  143.         for (int i = 1; i < size - 1; i++) 
  144.         {
  145.             if (i == pos) 
  146.             {
  147.                 Node tmp = ptr.getLink();
  148.                 tmp = tmp.getLink();
  149.                 ptr.setLink(tmp);
  150.                 break;
  151.             }
  152.             ptr = ptr.getLink();
  153.         }
  154.         size-- ;
  155.     }    
  156.     /*  Function to display elements  */
  157.     public void display()
  158.     {
  159.         System.out.print("\nSingly Linked List = ");
  160.         if (size == 0) 
  161.         {
  162.             System.out.print("empty\n");
  163.             return;
  164.         }    
  165.         if (start.getLink() == null) 
  166.         {
  167.             System.out.println(start.getData() );
  168.             return;
  169.         }
  170.         Node ptr = start;
  171.         System.out.print(start.getData()+ "->");
  172.         ptr = start.getLink();
  173.         while (ptr.getLink() != null)
  174.         {
  175.             System.out.print(ptr.getData()+ "->");
  176.             ptr = ptr.getLink();
  177.         }
  178.         System.out.print(ptr.getData()+ "\n");
  179.     }
  180. }
  181.  
  182. /*  Class SinglyLinkedList  */
  183. public class SinglyLinkedList
  184. {    
  185.     public static void main(String[] args)
  186.     {             
  187.         Scanner scan = new Scanner(System.in);
  188.         /* Creating object of class linkedList */
  189.         linkedList list = new linkedList(); 
  190.         System.out.println("Singly Linked List Test\n");          
  191.         char ch;
  192.         /*  Perform list operations  */
  193.         do
  194.         {
  195.             System.out.println("\nSingly Linked List Operations\n");
  196.             System.out.println("1. insert at begining");
  197.             System.out.println("2. insert at end");
  198.             System.out.println("3. insert at position");
  199.             System.out.println("4. delete at position");
  200.             System.out.println("5. check empty");
  201.             System.out.println("6. get size");            
  202.             int choice = scan.nextInt();            
  203.             switch (choice)
  204.             {
  205.             case 1 : 
  206.                 System.out.println("Enter integer element to insert");
  207.                 list.insertAtStart( scan.nextInt() );                     
  208.                 break;                          
  209.             case 2 : 
  210.                 System.out.println("Enter integer element to insert");
  211.                 list.insertAtEnd( scan.nextInt() );                     
  212.                 break;                         
  213.             case 3 : 
  214.                 System.out.println("Enter integer element to insert");
  215.                 int num = scan.nextInt() ;
  216.                 System.out.println("Enter position");
  217.                 int pos = scan.nextInt() ;
  218.                 if (pos <= 1 || pos > list.getSize() )
  219.                     System.out.println("Invalid position\n");
  220.                 else
  221.                     list.insertAtPos(num, pos);
  222.                 break;                                          
  223.             case 4 : 
  224.                 System.out.println("Enter position");
  225.                 int p = scan.nextInt() ;
  226.                 if (p < 1 || p > list.getSize() )
  227.                     System.out.println("Invalid position\n");
  228.                 else
  229.                     list.deleteAtPos(p);
  230.                 break;
  231.             case 5 : 
  232.                 System.out.println("Empty status = "+ list.isEmpty());
  233.                 break;                   
  234.             case 6 : 
  235.                 System.out.println("Size = "+ list.getSize() +" \n");
  236.                 break;                         
  237.              default : 
  238.                 System.out.println("Wrong Entry \n ");
  239.                 break;   
  240.             }
  241.             /*  Display List  */ 
  242.             list.display();
  243.             System.out.println("\nDo you want to continue (Type y or n) \n");
  244.             ch = scan.next().charAt(0);                        
  245.         } while (ch == 'Y'|| ch == 'y');               
  246.     }
  247. }
OUTPUT
Singly Linked List Test
 
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true
 
Singly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
5
 
Singly Linked List = 5
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
7
 
Singly Linked List = 7->5
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
4
 
Singly Linked List = 7->5->4
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
2
Enter integer element to insert
2
 
Singly Linked List = 7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
1
Enter integer element to insert
9
 
Singly Linked List = 9->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
3
Enter position
3
 
Singly Linked List = 9->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
3
Enter integer element to insert
2
Enter position
2
 
Singly Linked List = 9->2->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
6
Size = 7
 
 
Singly Linked List = 9->2->7->3->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
4
 
Singly Linked List = 9->2->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2
 
Singly Linked List = 9->7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1
 
Singly Linked List = 7->5->4->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
3
 
Singly Linked List = 7->5->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1
 
Singly Linked List = 5->2
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
2
 
Singly Linked List = 5
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
4
Enter position
1
 
Singly Linked List = empty
 
Do you want to continue (Type y or n)
 
y
 
Singly Linked List Operations
 
1. insert at begining
2. insert at end
3. insert at position
4. delete at position
5. check empty
6. get size
5
Empty status = true
 
Singly Linked List = empty
 
Do you want to continue (Type y or n)
 
n

No comments:

Post a Comment

Facebook