/**
* Insertion operations on a single linked list
*
* @author: Nitendra Verma
* @version: Sept 8,2013
*/
class listTest
{
protected static linkedList lst;
public static void main()
{
int num;
lst=new linkedList();
lst.insert(5);
lst.insert(3);
lst.insert(8);
lst.insert(9);
lst.insert(2);
lst.display();
}
}
class Node
{
protected int data;
protected Node link;
public Node()
{
data=0;
link=null;
}
public Node(int d,Node n)
{
data=d;
link=n;
}
public void setdata(int d)
{
data=d;
}
public void setlink(Node n)
{
link=n;
}
public int getdata()
{
return data;
}
public Node getlink()
{
return link;
}
}
class linkedList
{
protected Node start; //start denotes the first node of the list
public linkedList() //default constructor
{
start=null;
}
public void insert(int val)
{
Node nptr,ptr,save=null;
nptr=new Node(val,null);
boolean ins=false;
//Case1: list is empty
if(start==null)
start=nptr;
//Case2: Insertion in the beginning
else if(val<start.getdata()) //checking whether ITEM is to be inserted in the beginning
{
save=start; //save START's value(save is also a reference)
start=nptr; //assign nptr to START
nptr.link=save; //makes nptr points to previous START
}
//Case3:INsertion in middle
else
{
save=start;
ptr=start.getlink();
while(ptr!=null)
{
if(nptr.data>ptr.data)
{
save=ptr;
ptr=ptr.link;
}
else //insert in between to nodes
{
save.link=nptr;
nptr.link=ptr;
break; //jump out of loop
}
}
//Case4:Insertion at the end
if(ptr==null)
{
save.link=nptr;
nptr.link=null;
//save.setlink(nptr);
}
}
}
public void display()
{
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()+"!!!"); //last node info printed
System.out.println();
}
}
/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in
* Insertion operations on a single linked list
*
* @author: Nitendra Verma
* @version: Sept 8,2013
*/
class listTest
{
protected static linkedList lst;
public static void main()
{
int num;
lst=new linkedList();
lst.insert(5);
lst.insert(3);
lst.insert(8);
lst.insert(9);
lst.insert(2);
lst.display();
}
}
class Node
{
protected int data;
protected Node link;
public Node()
{
data=0;
link=null;
}
public Node(int d,Node n)
{
data=d;
link=n;
}
public void setdata(int d)
{
data=d;
}
public void setlink(Node n)
{
link=n;
}
public int getdata()
{
return data;
}
public Node getlink()
{
return link;
}
}
class linkedList
{
protected Node start; //start denotes the first node of the list
public linkedList() //default constructor
{
start=null;
}
public void insert(int val)
{
Node nptr,ptr,save=null;
nptr=new Node(val,null);
boolean ins=false;
//Case1: list is empty
if(start==null)
start=nptr;
//Case2: Insertion in the beginning
else if(val<start.getdata()) //checking whether ITEM is to be inserted in the beginning
{
save=start; //save START's value(save is also a reference)
start=nptr; //assign nptr to START
nptr.link=save; //makes nptr points to previous START
}
//Case3:INsertion in middle
else
{
save=start;
ptr=start.getlink();
while(ptr!=null)
{
if(nptr.data>ptr.data)
{
save=ptr;
ptr=ptr.link;
}
else //insert in between to nodes
{
save.link=nptr;
nptr.link=ptr;
break; //jump out of loop
}
}
//Case4:Insertion at the end
if(ptr==null)
{
save.link=nptr;
nptr.link=null;
//save.setlink(nptr);
}
}
}
public void display()
{
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()+"!!!"); //last node info printed
System.out.println();
}
}
/*---------------Program developed by: Nitendra Verma---------------*/
//For more details visit http://javawithnitendra.blogspot.in
No comments:
Post a Comment
Ur comments r most welcome...