Pages

Saturday, 7 March 2020

LinkedList

Each element in the LinkedList is called the Node. Each Node of the Linked List contains two items:
1)Content of the element
2)Pointer/Address/Reference to the Next Node in the LinkedList.
Note:
1. Head of the LinkedList only contains the Address of the First element of the List.
2. The Last element of the LinkedList contains null in the pointer part of the node because it is the end of the List so it doesn’t point to anything as shown in the above diagram.
3. The diagram which is shown above represents a singly linked list. There is another complex type variation of LinkedList which is called doubly linked list,node of a doubly linked list contains three parts: 
1) Pointer to the previous node of the linked list 
2) content of the element 
3) pointer to the next node of the linked list.

The following features are provided by Linked List:
1. Linked list allows dynamic memory allocation, which means memory allocation is done at the run time by the compiler and we do not need to mention the size of the list during linked list declaration.

2. Linked list elements don’t need contiguous memory locations because elements are linked with each other using the reference part of the node that contains the address of the next node of the list.

3. Insert and delete operations in the Linked list are not expensive because adding and deleting an element from the linked list does not require element shifting, only the pointer of the previous and the next node requires change.


Types of Linked List:
  • Simple Linked List − Item navigation is forward only.
  • Doubly Linked List − Items can be navigated forward and backward.
  • Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous.

Basic Operations
Following are the basic operations supported by a list.

  • Insertion − Adds an element at the beginning of the list
  • Deletion − Deletes an element at the beginning of the list.
  • Display − Displays the complete list.
  • Search − Searches an element using the given key.
  • Delete − Deletes an element using the given key.
Insertion Operation
Adding a new node in linked list is a more than one step activity. We shall learn this with diagrams here. First, create a node using the same structure and find the location where it has to be inserted.
Imagine that we are inserting a node B (NewNode), between  A (LeftNode) and C (RightNode). Then point B.next to C
NewNode.next −> RightNode;

Now, the next node at the left should point to the new node.
LeftNode.next −> NewNode;
This will put the new node in the middle of the two. The new list should look like this 
Similar steps should be taken if the node is being inserted at the beginning of the list. While inserting it at the end, the second last node of the list should point to the new node and the new node will point to NULL.

Deletion Operation
Deletion is also a more than one step process. We shall learn with pictorial representation. First, locate the target node to be removed, by using searching algorithms.

The left (previous) node of the target node now should point to the next node of the target node −
LeftNode.next −> TargetNode.next;

This will remove the link that was pointing to the target node. Now, using the following code, we will remove what the target node is pointing at.
TargetNode.next −> NULL;

We need to use the deleted node. We can keep that in memory otherwise we can simply deallocate memory and wipe off the target node completely.

//Program to illustrate Single Linked List Operations
import java.util.*;
class lists
{
int data;
lists next;
}
class linklist
{
public static void main(String []args)
{
Scanner sc=new Scanner(System.in);
slist sl=new slist();
int opt,y,z;
while(true)
  {
System.out.println("1.creation");
System.out.println("2.Insertion");
System.out.println("3.Deletion");
System.out.println("4.display");
System.out.println("5.exit");
System.out.println("enter ur choice:");
opt=sc.nextInt();
switch(opt)
{
case 1: System.out.println("Enter the number:");
y=sc.nextInt();
sl.create(y);
break;
case 2: System.out.println("Enter the position:");
y=sc.nextInt();
System.out.println("Enter the Data:");
z=sc.nextInt();
sl.insert(y,z);
break;
case 3: System.out.println("Enter the position:");
y=sc.nextInt();
sl.deletes(y);
break;
case 4: sl.display();
break;
case 5: System.exit(0);
}
  }
}
}

  class slist
  {
lists head,last,new1,temp;
slist()
{
head=null;
}
public void create(int x)
{
new1=new lists();
if(new1==null)
{
System.out.println("creation is not poosible");
return;
}
new1.data=x;
if(head==null)
{
head=last=new1;
last.next=null;
}
else
{
last.next=new1;
last=new1;
last.next=null;
}
}

public void display()
{
if(head==null)
{
System.out.println("Empty list");
return;
}
System.out.println("The Elements are:");
System.out.printf("%h---",head);
for(temp=head;temp!=null;temp=temp.next)
{
System.out.printf(" %d || %h ",temp.data,temp.next);
}
System.out.println();
}
public void insert(int x,int d)
{
int pos,i;
pos=x;
new1=new lists();
if(new1==null)
{
System.out.println("Insufficient Memory");
return;
}
new1.data=d;
if(head==null && pos==1)
{
head=last=new1;
new1.next=null;
}
else if(head!=null && pos==1)
{
new1.next=head;
head=new1;
}
else
{
i=2;
temp=head;
while(i<pos)
{
temp=temp.next;
i++;
}
if(temp==null)
{
last.next=new1;
last=new1;
last.next=null;
}
else
{
new1.next=temp.next;
temp.next=new1;
}
}
}
public void deletes(int p)
 {
int pos,i;
lists curr;
pos=p;
if(head==null)
{
System.out.println("Empty Linked List");
return;
}
if(pos==1)
{
temp=head;
head=head.next;
temp=null;
}
else
{
i=2;
temp=head;
curr=temp.next;
while(i<pos)
{
temp=temp.next;
curr=temp.next;
i++;
}
temp.next=curr.next;
curr=null;
}
 }
}

No comments:

Post a Comment

Constructors & Destructors in c++

  Constructors :  A Constructor is a special member function, which is used to initialize the objects of its class. The Constructor is invok...