forked from rathoresrikant/HacktoberFestContribute
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DoublyLink.java
125 lines (111 loc) · 2.57 KB
/
DoublyLink.java
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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
import java.util.*;
class Node {
int data;
Node next;
Node prev;
public Node (int data) {
this.data = data;
this.next = null;
this.prev = null;
}
}
class Doubly{
Node head;
public Doubly(){
this.head = null;
}
public void print(){
Node temp;
for(temp = this.head;temp != null;temp = temp.next){
System.out.println(temp.data);
}
}
public void inspos(int data,int pos) {
Node temp=this.head;
int i=1;
Node newnode = new Node (data);
if(head==null && pos==1) {
head=newnode;
return;
}
if (head==null && pos!=1){
System.out.println("Please enter a valid position");
return;
}
if(head!=null && pos==1) {
newnode.next=head;
head.prev=newnode;
head=newnode;
return;
}
while(i<pos-1 && temp!=null) {
temp=temp.next;
i++;
}
if(i==pos-1) {
if(temp.next!=null) {
newnode.next=temp.next;
temp.next.prev=newnode;
}
temp.next=newnode;
newnode.prev=temp;
}
else
System.out.println("Invalid ");
}
public void insfront(int data) {
Node newnode= new Node (data);
if(head!=null) {
head.prev=newnode;
}
newnode.next=head;
head=newnode;
}
public void delpos(int elem) {
Node temp = head;
if(head == null)
return;
if (head.data==elem) {
head = temp.next;
return;
}
while(temp.data!=elem)
{
temp=temp.next;
}
temp.next.next.prev=temp;
temp.next=temp.next.next;
}
}
class DoublyLink {
public static void main(String[] args) {
Doubly l = new Doubly();
Scanner m = new Scanner(System.in);
int ch =1;
while(ch==1){
System.out.println("Enter the choice 1. Insert at first 2.Insert at end 3.Insert at pos 4.Print 5.Delete at front 6.Delete at End 7.Delete at pos");
int c =m.nextInt();
switch(c)
{
case 1:System.out.println("Enter the element");
int r= m.nextInt();
l.insfront(r);
break;
case 2:System.out.println("Enter the element");
int q= m.nextInt();
System.out.println("Enter the position");
int w= m.nextInt();
l.inspos(q,w);
break;
case 3:l.print();
break;
case 4:System.out.println("Enter the element");
int h= m.nextInt();
l.delpos(h);
break;
}
System.out.println("Do you want to continue 1.Yes 0.No");
ch=m.nextInt();
}
}
}