Linked List

Document Sample
Linked List Powered By Docstoc
					/*
AIM: WAP to implement Link List. Create list, insert, delete, search a node, display.
INPUT:
*/
import java.util.*;
class Node
{
int data;
Node next;
Node (int ele)
{
 data =ele;
 next=null;
}
}
class LL
{
Node first;
void insert( int ele)
{
 Node p = new Node (ele);
 if(first==null)
  first=p;
 else
 {
  Node q=first;
  while(q.next!=null)
   q=q.next;
  q.next=p;
 }
}
void delete ()
{
 if(first==null)
  System.out.println("LL is empty");
 else
 {
  Node p=first,q=null;
  while(p.next!=null)
  {
   q=p;
   p=p.next;
  }
  if (p==first)
   first=null;
  else
   q.next=null;
  System.out.println("Deleted element is "+p.data);
 }
}
void display()
{
 if(first==null)
  System.out.println("LL is empty");
 else
 {
  Node q=first;
  while(q!=null)
  {
   System.out.print(q.data+" ");
   q=q.next;
  }
  System.out.println();
 }
}
boolean search(int ele)
{
 if(first==null)
 {
  System.out.println("LL is empty");
  return false;
 }
 else
 {
  Node q=first;
  while(q!=null)
  {
   if(q.data==ele)
    return true;
   q=q.next;
  }
  return false;
 }
}
}
class LLDemo
{
public static void main(String args[])
{
 LL l= new LL();
 Scanner sc = new Scanner (System.in);
 int ch;
 do
 {
  System.out.println("Enter\n1.Insert in Link List\n2.Delete last in Link List\n3.Display all elements in Link List\n4.Search for a value
in Link List\n5.Exit");
  ch = sc.nextInt();
  int val;
  switch (ch)
  {
   case 1:System.out.println("Enter value to be inserted");
    val=sc.nextInt();
    l.insert(val);
    break;
   case 2: l.delete();
    break;
   case 3:l.display();
    break;
   case 4:System.out.println("Enter value to be searched");
    val=sc.nextInt();
    if(l.search(val))
     System.out.println("Element exists");
    else
     System.out.println("Element does not exist");
  }
 }while(ch!=5);
}
}
/*
OUTPUT:
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
1
Enter value to be inserted
10
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
1
Enter value to be inserted
20
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
1
Enter value to be inserted
30
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
1
Enter value to be inserted
40
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
1
Enter value to be inserted
50
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
3
10 20 30 40 50
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
2
Deleted element is 50
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
4
Enter value to be searched
50
Element does not exist
Enter
1.Insert in Link List
2.Delete last in Link List
3.Display all elements in Link List
4.Search for a value in Link List
5.Exit
5
*/

				
DOCUMENT INFO
Shared By:
Categories:
Tags:
Stats:
views:10
posted:8/31/2012
language:Unknown
pages:3