-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathDoubly.java
107 lines (91 loc) · 2.57 KB
/
Doubly.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
import java.util.*;
class Doublyll{
private Node head;
class Node{
int data;
Node prev;
Node next;
}
public Doublyll(){
head=null;
}
public boolean insert(int data){
Node newNode = new Node();
newNode.data=data;
newNode.prev=null;
newNode.next=null;
if(newNode==null)
return false;
Node temp;
if(head!=null){
temp=head;
head=newNode;
newNode.next=temp;
}
head=newNode;
return true;
}
public void display(){
Node temp=head;
while(temp!=null){
System.out.println(temp.data);
temp=temp.next;
}
}
public int Delete(int data){
Node temp=head;
if(temp==null)
return -1;
while(temp!=null&&temp.data!=data){
temp=temp.next;
}
if(temp==null)
return -1;
else{
if(temp.prev!=null){
(temp.prev).next=temp.next;
(temp.next).prev=temp.prev;
}
else
head=null;
display();
return 1;
}
}
}
public class Doubly{
static public void main(String []args){
Doublyll d= new Doublyll();
boolean temp=true;
int choice;
Scanner s= new Scanner(System.in);
while(temp){
System.out.println("1 to insert");
System.out.println("2 to delete");
System.out.println("3 to display");
System.out.println("4 to exit");
choice=s.nextInt();
switch(choice){
case 1:
choice=s.nextInt();
if(d.insert(choice));
d.display();
break;
case 2:
choice=s.nextInt();
choice=d.Delete(choice);
if(choice!=-1);
d.display();
break;
case 3:
d.display();
break;
case 4:
temp=false;
break;
default:
System.out.println("Enter a proper choice");
}
}
}
}