You are on page 1of 7

A linked list is a linear collection of data elements called nodes.

Each node is divided into two parts the


first part contains the information of the element (i.e. data) and the second part is another node in
itself. Thus by definition linked list is a recursive data structure (i.e. a data structure whose definition
includes itself as one of the data members).
Advantage of using linked list to represent linear lists over arrays:
During instantiation of arrays static memory allocation takes place, i.e. contagious memory addresses
are reserved to store the array elements and thus the length cannot be increased during runtime if need
arises for the same. Again if all the addresses allocated during instantiation of arrays are not used, then
the same cannot be released for any other use which leads to wastage of available memory in the
system.
Linked lists use the technique of dynamic memory allocation, i.e. a node is added to a list if and only if
need arises. Thus no predetermined length needs to be assigned for a list. Moreover the memory
addresses occupied by the nodes of a linked list are not necessarily contagious because it is randomly
allocated from the free pool of memory during creation of new nodes.
class Node
{
45
23
17
39
null
int data;
Node link;
public Node()
45
{
23
link=null;
17
data=0;
39
null
}
public Node(int k,Node e)
{
data=k;
link=e;
}
}
---------------------------------------------------------------------------------------------------------------------------------------class LinkedList
{Node start;
public LinkedList()
{start=null;
}
public void insertBeg(int n)//insertion of node at the beginning of a list....ISC 2010(specimen)
{Node tmp=new Node(n,start);
start=tmp;
}
public void insertEnd(int n)//insertion of node at the end of a list....ISC 2013(specimen),ISC 2014
{Node tmp=start;
1

if(tmp==null)
{
tmp.data=n;
tmp.link=null;
}
else
{while(tmp.link!=null)
{
tmp=tmp.link;
}
tmp.link=new Node(n,null);
}
}
public int deleteEnd(Node start)//to delete a node from a list....ISC 2011
{ Node tmp=start;
int x;
if(tmp==null)
{
System.out.println("\nList is Empty!");
return -999;
}
else if (tmp.link==null)
{
x=tmp.data;
tmp=null;
}
else
{
while(tmp.link.link!=null)
{
tmp=tmp.link;
}
x=tmp.link.data;
tmp.link=null;
}
return x;
}
public int deleteFront(Node start)//deletion of the first node in a list....ISC 2010(specimen)
{ Node tmp=start;
if(tmp==null)
{
System.out.println("\n List is Empty!");
2

return -999;
}
else
{
this.start=this.start.link;
return tmp.data;
}
}
public boolean delete(int n)//deletion of first node that stores data=n
{boolean flag=false;
if(start==null)
flag=false;
else if(start.data==n)//if first node has data as n
{start=start.link;
flag=true;
}
else
{
Node tmp=start;
while(tmp.link!=null)
{if(tmp.link.data==n)
{tmp.link= tmp.link.link;
flag=true;
break;
}
tmp=tmp.link;
}
}
return flag;
}
public int search(int n)//Linear Search for node with data=n, returns position of occurrence if found else
returns 0
{boolean flag=false;
int c=0;
Node tmp=start;
while(tmp!=null)
{c++;
if(tmp.data==n)
{flag=true;
break;
}
3

tmp=tmp.link;
}
if(flag)
return c;
return 0;
}
public int listSum(Node start)//to return the sum of all the data stored in a linked list....ISC 2010
{int sum=0;
Node tmp=start;
while(tmp!=null)
{sum=sum+tmp.data;
tmp=tmp.link;
}
return sum;
}
public void reverse()//To reverse the elements in the list non recursively
{Node rev=null;
Node temp=start;
while(temp!=null)
{rev=new Node(temp.data,rev);
temp=temp.link;
}
start=rev;
}
public int Count(Node start)//To count the number of nodes....ISC 2013
{int c=0;
Node tmp=start;
while(tmp!=null)
{c=c+1;
tmp=tmp.link;
}
return c;
}
public int count(Node start)//To count the number of nodes recursively
{if(start==null)
return 0;
else
return 1+count(start.link);
}
public void display()//To display the list non recursive-overloaded version 1
4

{Node tmp=start;
if(tmp==null)
System.out.println("List is empty");
while(tmp!=null)
{System.out.print(tmp.data+"\t");
tmp=tmp.link;
}
}
public void display(Node start)//To display the list recursive-overloaded version 2
{
if(start!=null)
{ System.out.print(start.data+"\t");
display(start.link);
}
}
}
-----------------------------------------------------------------------------------------------------------------------------------------import java.util.*;
class Execute
{public static void main()//To execute all the functions
{Scanner sc =new Scanner(System.in);
int n,ch,x;
LinkedList list=new LinkedList();
do
{
System.out.println("\nEnter 1 to INSERT AN ELEMENT AT THE BEGINNING OF THE LIST");
System.out.println("Enter 2 to INSERT AN ELEMENT AT THE END OF THE LIST");
System.out.println("Enter 3 to REMOVE AN ELEMENT FROM THE BEGINNING OF THE LIST");
System.out.println("Enter 4 to REMOVE AN ELEMENT FROM THE END OF THE LIST");
System.out.println("Enter 5 to DELETE A NODE WITH USER INPUT DATA VALUE");
System.out.println("Enter 6 to SEARCH A NODE WITH USER INPUT DATA VALUE");
System.out.println("Enter 7 TO FIND THE SUM OF ELEMENTS INM THE LIST");
System.out.println("Enter 8 TO REVERSE THE SEQUENCE OF ELEMEMTS IN THE LIST");
System.out.println("Enter 9 TO COUNT THE NUMBER OF NODES IN THE LIST");
System.out.println("Enter 10 TO COUNT THE NUMBER OF NODES IN THE LIST USING RECURSIVE
FUNCTION");
System.out.println("Enter 11TO DISPLAY THE LIST-USING NON RECURSIVE FUNCTION");
System.out.println("Enter 12 TO DISPLAY THE LIST-USING RECURSIVE FUNCTION");
System.out.println("PRESS 0 TO EXIT");
System.out.print("PLEASE ENTER YOUR CHOICE:");
ch=sc.nextInt();
5

switch(ch)
{case 1:
System.out.print("Please Enter a number ");
n=sc.nextInt();
sc.nextLine();
list.insertBeg(n);
break;
case 2:
System.out.print("Please Enter a number ");
n=sc.nextInt();
sc.nextLine();
list.insertEnd(n);
break;
case 3:
x=list.deleteFront(list.start);
if(x!=-999)
System.out.print("The deleted data from the beginning of the list is "+x+"\n");
break;
case 4:
x=list.deleteFront(list.start);
if(x!=-999)
System.out.println("The deleted data from the end of the list is "+x+"\n");
break;
case 5:
System.out.print("Please Enter the number to be deleted ");
n=sc.nextInt();
sc.nextLine();
if(list.delete(n))
System.out.println("Deleted Node with data as "+n);
else
System.out.println("Node with data "+n+" does not exist");
break;
case 6:
System.out.print("Please Enter the number to be searched in the list ");
n=sc.nextInt();
sc.nextLine();
int p=list.search(n);
if(p>0)
System.out.println ("Node with data as "+n+" exists at position "+p);
else
System.out.println("Node with data "+n+" does not exist");
break;
6

case 7:
System.out.println("The sum of data in the linked list is "+list.listSum(list.start)+"\n");
break;
case 8:
System.out.println("The original List was:");
list.display();
list.reverse();
System.out.println("\nThe reversed List is:");
list.display();
break;
case 9:
System.out.println("The number of nodes in the linked list is "+list.Count(list.start)+"\n");
break;
case 10:
System.out.println("The number of nodes in the linked list is "+list.count(list.start)+"\n");
break;
case 11:
System.out.println("The List is:");
list.display();
System.out.println();
break;
case 12:
System.out.println("The List is:");
if(list.start==null)
System.out.println("List is empty");
list.display(list.start);
System.out.println();
break;
case 0:
System.out.println("Aborted....Have a Nice Day!");
break;
default:
System.out.println("Sorry!!! Invalid Choice!... Please Re- Enter");
}
}while(ch!=0);
}
}

You might also like