/* File Name DLLinked.java
Program to Implement Linked List
Theory:-A linked list is a Linear Data Structure which is a collection of nodes that are linked one another. Linked list is dynamic
1.Insert
2.Delete
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.BufferedReader;
import java.util.*;
class Node
{
int info;
Node next,prev;
Node()
{
prev=next=null;
}
void insert(int d)
{
Node p=new Node();
p.info=d;
if(this.next==null)
this.next=p;
else
{
Node tmp;
tmp=this.next;
while(tmp.next!=null)
tmp=tmp.next;
tmp.next=p;
p.prev=tmp;
}
}
void delete()
{
Node tmp,p=null;
tmp=this.next;
if(tmp==null)
System.out.println("\n\t\t LINKED LIST IS EMPTY.");
while(tmp.next!=null)
{
tmp=tmp.next;
}
if(tmp==this.next){
this.next=null;
tmp=null;
}
else{
int d=tmp.info;
System.out.println("\n\t\t Deleted Node is::"+d);
p=tmp.prev;
p.next=null;
tmp.next=prev.next=null;
tmp=null;
}
}
void display()
{
Node tmp;
tmp=this.next;
if(tmp!=null){
System.out.println("\n\t\t LINKED LIST");
while(tmp!=null)
{
System.out.println("\n\t\t"+tmp.info);
tmp=tmp.next;
}
}
else
System.out.println("\n\t\t LINKED LIST IS EMPTY");
}
}
class DLLinked {
public static void main(String args[])
{
Node ll=new Node();
Scanner sc=new Scanner(System.in);
int ch;
while(true)
{
System.out.println("\n\t\t\t\t Linked List");
System.out.println("\t\t1.Insert");
System.out.println("\t\t2.Delete");
System.out.println("\t\t3.Display");
System.out.println("\t\t4.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\n\t\tEnter Number : ");
int n1=sc.nextInt();
ll.insert(n1);
break;
case 2:
ll.delete();
break;
case 3:
ll.display();
break;
case 4:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
}
}/*
*************************************************OUTPUT*****************************************************
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 12
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 32
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
LINKED LIST
12
32
3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Deleted Node is::3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 4 */
Program to Implement Linked List
Theory:-A linked list is a Linear Data Structure which is a collection of nodes that are linked one another. Linked list is dynamic
1.Insert
2.Delete
Learner:-ABDUL MUGEESH
RollNo:-12CO104 */
import java.io.BufferedReader;
import java.util.*;
class Node
{
int info;
Node next,prev;
Node()
{
prev=next=null;
}
void insert(int d)
{
Node p=new Node();
p.info=d;
if(this.next==null)
this.next=p;
else
{
Node tmp;
tmp=this.next;
while(tmp.next!=null)
tmp=tmp.next;
tmp.next=p;
p.prev=tmp;
}
}
void delete()
{
Node tmp,p=null;
tmp=this.next;
if(tmp==null)
System.out.println("\n\t\t LINKED LIST IS EMPTY.");
while(tmp.next!=null)
{
tmp=tmp.next;
}
if(tmp==this.next){
this.next=null;
tmp=null;
}
else{
int d=tmp.info;
System.out.println("\n\t\t Deleted Node is::"+d);
p=tmp.prev;
p.next=null;
tmp.next=prev.next=null;
tmp=null;
}
}
void display()
{
Node tmp;
tmp=this.next;
if(tmp!=null){
System.out.println("\n\t\t LINKED LIST");
while(tmp!=null)
{
System.out.println("\n\t\t"+tmp.info);
tmp=tmp.next;
}
}
else
System.out.println("\n\t\t LINKED LIST IS EMPTY");
}
}
class DLLinked {
public static void main(String args[])
{
Node ll=new Node();
Scanner sc=new Scanner(System.in);
int ch;
while(true)
{
System.out.println("\n\t\t\t\t Linked List");
System.out.println("\t\t1.Insert");
System.out.println("\t\t2.Delete");
System.out.println("\t\t3.Display");
System.out.println("\t\t4.Exit");
System.out.print("\n\t\tEnter your choice : ");
ch=sc.nextInt();
switch(ch)
{
case 1:
System.out.print("\n\t\tEnter Number : ");
int n1=sc.nextInt();
ll.insert(n1);
break;
case 2:
ll.delete();
break;
case 3:
ll.display();
break;
case 4:
return;
default:
System.out.println("\n \t \t Please Select From 1 To 5 ");
}
}
}
}/*
*************************************************OUTPUT*****************************************************
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 12
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 32
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 1
Enter Number : 3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 3
LINKED LIST
12
32
3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 2
Deleted Node is::3
Linked List
1.Insert
2.Delete
3.Display
4.Exit
Enter your choice : 4 */
Comments
Post a Comment