Single Linked List (General Linked List)

Posted: March 6, 2012 in Java Programs

import java.util.*;
class Node
{
int data;
Node next;
public Node(int k)
{
data=k;
next=null;
}
public static void main(String g[])
{
int ch,pele;
Scanner src=new Scanner(System.in);
System.out.println(“Enter no of elements to create”);
int n=src.nextInt();
LinkedList obj=new LinkedList();
obj.create(n);
while(true)
{
System.out.println(“enter choice 1:Insert beg 2:insert end”);
System.out.println(“enter choice 3:Insert pos 4:delete beg 5:delete end 6:delete pos”);
System.out.println(“enter choice 7:delete element 8:display 9: search 10:Exit”);
ch=src.nextInt();
if(ch==10)
break;
switch(ch)
{
case 1:
System.out.println(“enter the element to insert”);
int ele=src.nextInt();
obj.insertBeg(ele);
break;
case 2:
System.out.println(“enter the element to insert”);
ele=src.nextInt();
obj.insertEnd(ele);
break;
case 3:
System.out.println(“enter the element to insert”);
ele=src.nextInt();
System.out.println(“enter the position”);
int pos=src.nextInt();
obj.insertPos(ele,pos);
break;
case 4:
if(obj.empty())
System.out.println(“Linklist is empty”);
else
{
pele=obj.deleteBeg();
System.out.println(“deleted element is”+pele);
}
break;
case 5:
if(obj.empty())
System.out.println(“Linklist is empty”);
else
{
pele=obj.deleteEnd();
System.out.println(“deleted element is”+pele);
}
break;
case 6:
if(obj.empty())
System.out.println(“Linklist is empty”);
else
{
System.out.println(“Eneter the position”);
pos=src.nextInt();
pele=obj.deletePos(pos);
System.out.println(“deleted element is”+pele);
}
break;
case 7:
if(obj.empty())
System.out.println(“Linklist is empty”);
else
{
ele=src.nextInt();
obj.deleteEle(ele);
}
break;
case 8:
if(obj.empty())
System.out.println(“linked list is empty”);
else
obj.display();
break;
case 9:
System.out.println(“enter the element to search”);
int x=src.nextInt();
if(obj.search(x))
System.out.println(x+”is in the linked list”);
else
System.out.println(x+”is not present”);
break;
}
}
}
}
class LinkedList
{
private Node start=null;
public boolean empty()
{
if (start==null)
return true;
else return false;
}
public void create(int n)
{
Node p,q;
for (int i=0;i<n;i++)
{
Scanner src=new Scanner(System.in);
System.out.println(“Enter data”);
int ele=src.nextInt();
p=new Node(ele);
if(start==null)
start=p;
else
{
q=start;
while(q.next!=null)
q=q.next;
q.next=p;
}
}
}
public void insertBeg(int ele)
{
Node z=new Node(ele);
z.next=start;
start=z;
}
public void insertEnd(int ele)
{
Node z=new Node(ele);
if (start==null)
start=z;
else
{
Node q=start;
while (q.next!=null)
q=q.next;
q.next=z;
}
}
public void insertPos(int ele,int pos)
{
Node z=new Node(ele);
Node q=start;
for (int i=0;i<pos-2;i++)
q=q.next;
z.next=q.next;
q.next=z;
}
public int deleteBeg()
{
Node p=start;
start=start.next;
return p.data;
}
public int deleteEnd()
{
Node p,q=start;
if (start.next==null)
{
p=start;
start=null;
}
else
{
while (q.next.next!=null)
q=q.next;
p=q.next;
q.next=null;
}
return p.data;
}
public int deletePos(int pos)
{
Node p,q=start;
for (int i=0;i<pos;i++)
q=q.next;
p=q.next;
q.next=p.next;
return p.data;
}
public void deleteEle(int ele)
{
Node q=start;
if (start.data==ele)
{
System.out.println(ele+”is deleted”);
start=start.next;
}
else
{
while(q.next!=null)
{
if (q.next.data==ele)
break;
}
if (q.next==null)
System.out.println(ele+”not found”);
else
{
System.out.println(ele+”is deleted”);
q.next=q.next.next;
}
}
}
public void display()
{
Node q=start;
while(q!=null)
{
System.out.print(q.data+” “);
q=q.next;
}
System.out.println();
}
public boolean search(int ele)
{
Node q=start;
while(q!=null)
{
if (q.data==ele)
return true;
q=q.next;
}
return false;
}
}

 

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s