Program:
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class linkedList
{
public Node first;
public linkedList()
{
first=null;
}
public void insertfirst(int value) //insert at the first of the list
{
Node n1=new Node(value);
n1.next=first;
first=n1;
}
public void display()
{
System.out.print("");
Node current=first;
while(current!=null)
{
current.display();
current=current.next;
}
}
public void insertlast(int value) //insert at the last of the list
{
Node n1=new Node(value);
Node current=first;
if(current==null)
{
n1.next=first;
first=n1;
}
else
{
while(current.next!=null)
{
current=current.next;
}
current.next=n1;
}
}
public int find(int key) //Search an element in the list
{
Node current=first;int pos=1;
while(current.data!=key)
{
if(current.next==null)
return -1;
else
{
current=current.next;pos++;
}
}
return pos;
}
public void delete(int key) //Delete an element in the list
{
Node current=first;
if(current==null)
{
System.out.println("Error:Empty.");
return ;
}
Node previous= null;
while(current.data!=key)
{
if(current.next==null)
{
System.out.println("Error:Element not present.");
return ;
}
else
{
previous=current;
current=current.next;
}
}
if(current==first)
first=first.next;
else
previous.next=current.next;
}
public int numberofnodes() //Find number of nodes in the list
{
Node current=first;int i=0;
while(current!=null)
{
current=current.next;
i++;
}
return i;
}
public void insertatp(int p,int x) // //insert at a particular position in the list
{
int i;
if(p<1 || p-1>numberofnodes())
System.out.println("error");
else
{
Node newnode= new Node(x);
if(p==1)
{
newnode.next=first;
first=newnode;
}
else
{
Node current=first;
for(int k=1;k<p-1;k++)
current=current.next;
Node temp=current.next;
current.next=newnode;
newnode.next=temp;
}
}
}
public void sort() //Sort the list
{
Node fnode,snode;
int t;
fnode=first;
while(fnode!=null)
{
snode=fnode.next;
while(snode!=null)
{
if(fnode.data>snode.data)
{
t=fnode.data;
fnode.data=snode.data;
snode.data=t;
}
snode=snode.next;
}
fnode=fnode.next;
}
}
}
class list
{
public static void main(String arg[]) throws IOException
{
linkedList l1= new linkedList();
System.out.print("Enter the number of elements:");
DataInputStream x= new DataInputStream(System.in);
int n=Integer.parseInt(x.readLine());
System.out.print("Enter the elements:");
for(int i=0;i<n;i++)
l1.insertlast(Integer.parseInt(x.readLine()));
l1.display();
boolean proceed=true;
do{
System.out.print("\n\n~~MENU~~\n1.Insert\n2.Search an element\n3.Delete an element\n4.Find number of nodes\n5.Sort\n6.Exit>>\nEnter your choice:");
int n1=Integer.parseInt(x.readLine());
switch(n1)
{
case 1: System.out.print("\n\n~~MENU~~\n1.Insert at first \n2.Insert at last\n3.Insert at particular position\n4.Cancel\nEnter your choice:");
int n2=Integer.parseInt(x.readLine());
switch(n2)
{
case 1: System.out.println("\nInsert at first:\nEnter element: ");
l1.insertfirst(Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 2: System.out.println("\nInsert at last:\nEnter element: ");
l1.insertlast(Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 3: System.out.println("\nInsert at a particular position:\nEnter position & element: ");
l1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 4: l1.display();
default: break;
}
break;
case 2: System.out.println("\nEnter the element to find:");
int f=l1.find(Integer.parseInt(x.readLine()));
if(f>=0)
System.out.print("The element is present at position "+f);
else
System.out.print("The element is no more....");
break;
case 3: System.out.println("\nEnter the element to delete:");
l1.delete(Integer.parseInt(x.readLine()));
System.out.println("After deletion:");
l1.display();
case 4: System.out.println("\nNumber of nodes = "+l1.numberofnodes());
break;
case 5: System.out.println("Sorting.....");
l1.sort();
l1.display();
break;
case 6:
default:proceed=false;
break;
}
}while(proceed);
}
}
Output:
nn@linuxmint ~ $ javac list.java
Note: list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java list
Enter the number of elements:3
Enter the elements:2
4
5
2 4 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1
Insert at first:
Enter element:
1
After Insertion.....
1 2 4 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:2
Insert at last:
Enter element:
6
After Insertion.....
1 2 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:3
Insert at a particular position:
Enter position & element:
3
3
After Insertion.....
1 2 3 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:3
Enter the element to delete:
1
After deletion:
2 3 4 5 6
Number of nodes = 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1
Insert at first:
Enter element:
7
After Insertion.....
7 2 3 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:4
Number of nodes = 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:5
Sorting.....
2 3 4 5 6 7
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:4
2 3 4 5 6 7
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:6
nn@linuxmint ~ $
import java.io.*;
class Node
{
public int data;
public Node next;
public Node(int x)
{
data=x;
}
public void display()
{
System.out.print(data+" ");
}
}
class linkedList
{
public Node first;
public linkedList()
{
first=null;
}
public void insertfirst(int value) //insert at the first of the list
{
Node n1=new Node(value);
n1.next=first;
first=n1;
}
public void display()
{
System.out.print("");
Node current=first;
while(current!=null)
{
current.display();
current=current.next;
}
}
public void insertlast(int value) //insert at the last of the list
{
Node n1=new Node(value);
Node current=first;
if(current==null)
{
n1.next=first;
first=n1;
}
else
{
while(current.next!=null)
{
current=current.next;
}
current.next=n1;
}
}
public int find(int key) //Search an element in the list
{
Node current=first;int pos=1;
while(current.data!=key)
{
if(current.next==null)
return -1;
else
{
current=current.next;pos++;
}
}
return pos;
}
public void delete(int key) //Delete an element in the list
{
Node current=first;
if(current==null)
{
System.out.println("Error:Empty.");
return ;
}
Node previous= null;
while(current.data!=key)
{
if(current.next==null)
{
System.out.println("Error:Element not present.");
return ;
}
else
{
previous=current;
current=current.next;
}
}
if(current==first)
first=first.next;
else
previous.next=current.next;
}
public int numberofnodes() //Find number of nodes in the list
{
Node current=first;int i=0;
while(current!=null)
{
current=current.next;
i++;
}
return i;
}
public void insertatp(int p,int x) // //insert at a particular position in the list
{
int i;
if(p<1 || p-1>numberofnodes())
System.out.println("error");
else
{
Node newnode= new Node(x);
if(p==1)
{
newnode.next=first;
first=newnode;
}
else
{
Node current=first;
for(int k=1;k<p-1;k++)
current=current.next;
Node temp=current.next;
current.next=newnode;
newnode.next=temp;
}
}
}
public void sort() //Sort the list
{
Node fnode,snode;
int t;
fnode=first;
while(fnode!=null)
{
snode=fnode.next;
while(snode!=null)
{
if(fnode.data>snode.data)
{
t=fnode.data;
fnode.data=snode.data;
snode.data=t;
}
snode=snode.next;
}
fnode=fnode.next;
}
}
}
class list
{
public static void main(String arg[]) throws IOException
{
linkedList l1= new linkedList();
System.out.print("Enter the number of elements:");
DataInputStream x= new DataInputStream(System.in);
int n=Integer.parseInt(x.readLine());
System.out.print("Enter the elements:");
for(int i=0;i<n;i++)
l1.insertlast(Integer.parseInt(x.readLine()));
l1.display();
boolean proceed=true;
do{
System.out.print("\n\n~~MENU~~\n1.Insert\n2.Search an element\n3.Delete an element\n4.Find number of nodes\n5.Sort\n6.Exit>>\nEnter your choice:");
int n1=Integer.parseInt(x.readLine());
switch(n1)
{
case 1: System.out.print("\n\n~~MENU~~\n1.Insert at first \n2.Insert at last\n3.Insert at particular position\n4.Cancel\nEnter your choice:");
int n2=Integer.parseInt(x.readLine());
switch(n2)
{
case 1: System.out.println("\nInsert at first:\nEnter element: ");
l1.insertfirst(Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 2: System.out.println("\nInsert at last:\nEnter element: ");
l1.insertlast(Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 3: System.out.println("\nInsert at a particular position:\nEnter position & element: ");
l1.insertatp(Integer.parseInt(x.readLine()),Integer.parseInt(x.readLine()));
System.out.println("After Insertion.....");
l1.display();
break;
case 4: l1.display();
default: break;
}
break;
case 2: System.out.println("\nEnter the element to find:");
int f=l1.find(Integer.parseInt(x.readLine()));
if(f>=0)
System.out.print("The element is present at position "+f);
else
System.out.print("The element is no more....");
break;
case 3: System.out.println("\nEnter the element to delete:");
l1.delete(Integer.parseInt(x.readLine()));
System.out.println("After deletion:");
l1.display();
case 4: System.out.println("\nNumber of nodes = "+l1.numberofnodes());
break;
case 5: System.out.println("Sorting.....");
l1.sort();
l1.display();
break;
case 6:
default:proceed=false;
break;
}
}while(proceed);
}
}
Output:
nn@linuxmint ~ $ javac list.java
Note: list.java uses or overrides a deprecated API.
Note: Recompile with -Xlint:deprecation for details.
nn@linuxmint ~ $ java list
Enter the number of elements:3
Enter the elements:2
4
5
2 4 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1
Insert at first:
Enter element:
1
After Insertion.....
1 2 4 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:2
Insert at last:
Enter element:
6
After Insertion.....
1 2 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:3
Insert at a particular position:
Enter position & element:
3
3
After Insertion.....
1 2 3 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:3
Enter the element to delete:
1
After deletion:
2 3 4 5 6
Number of nodes = 5
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:1
Insert at first:
Enter element:
7
After Insertion.....
7 2 3 4 5 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:4
Number of nodes = 6
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:5
Sorting.....
2 3 4 5 6 7
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:1
~~MENU~~
1.Insert at first
2.Insert at last
3.Insert at particular position
4.Cancel
Enter your choice:4
2 3 4 5 6 7
~~MENU~~
1.Insert
2.Search an element
3.Delete an element
4.Find number of nodes
5.Sort
6.Exit>>
Enter your choice:6
nn@linuxmint ~ $
0 comments:
Post a Comment