-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoublylinkedlist
More file actions
82 lines (68 loc) · 1.8 KB
/
Copy pathDoublylinkedlist
File metadata and controls
82 lines (68 loc) · 1.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
import java.util.LinkedList;
import java.util.Scanner;
class DoublyLinkedList<T>{
private LinkedList<T> list=new LinkedList<>();
public void addfront(T element) {
list.addFirst(element);
}
public void addlast(T element) {
list.addLast(element);
}
public void insertatpos(int pos,T element) {
list.add(pos, element);
}
public void delete(int index) {
list.remove(index);
}
public void display(){
for(T element:list) {
System.out.print(element+" ");
}
System.out.println();
}
}
public class DoublyLinkDemo {
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
DoublyLinkedList<Integer> doublylinkedlist=new DoublyLinkedList<>();
int ch,element,index;
boolean condition=true;
while(condition) {
System.out.println("Enter your choice:\n1.Insert an element at front\n2.Insertion At End\n3.Insertion At Any Position\n4.Delete\n5.Display\n6.Exit");
ch=sc.nextInt();
switch (ch) {
case 1:
System.out.println("Enter the element:");
element=sc.nextInt();
doublylinkedlist.addfront(element);
break;
case 2:
System.out.println("Enter the element:");
element=sc.nextInt();
doublylinkedlist.addlast(element);
break;
case 3:
System.out.println("Enter the element:");
element=sc.nextInt();
System.out.println("Enter the position:");
index=sc.nextInt();
doublylinkedlist.insertatpos(index,element);
break;
case 4:
System.out.println("Enter the position:");
index=sc.nextInt();
doublylinkedlist.delete(index);
break;
case 5:
System.out.println("The elements are:");
doublylinkedlist.display();
break;
case 6:
condition=false;
break;
default:
System.out.println("Invalid Entry");
break;
}
}
}}