-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDoubly using Java.java
More file actions
66 lines (66 loc) · 1.51 KB
/
Doubly using Java.java
File metadata and controls
66 lines (66 loc) · 1.51 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
import java.util.Scanner;
class Node{
int data;
Node next;
Node prev;
Node(int data){
this.data=data;
this.next=null;
this.prev=null;
}
}
class doubly{
Node head=null;
void insert(int data){
Node n=new Node(data);
if(head==null){
head=n;
n.prev=null;
}else{
Node temp=head;
while(temp.next!=null){
temp=temp.next;
}
temp.next=n;
n.prev=temp;
}
}
void display(){
Node temp=head;
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.next;
}
System.out.println("NULL");
}
void reverse(){
Node temp=head;
if(head==null){
System.out.print("List is empty");
}
while(temp.next!=null){
temp=temp.next;
}
while(temp!=null){
System.out.print(temp.data+"->");
temp=temp.prev;
}
System.out.println("NULL");
}
}
public class Main{
public static void main(String[] args){
Scanner sc=new Scanner(System.in);
doubly list=new doubly();
System.out.print("Enter the Node:");
int k=sc.nextInt();
for(int i=1;i<=k;i++){
int data=sc.nextInt();
list.insert(data);
}
System.out.print("List is forward:");
list.display();
System.out.print("List in Backward:");
list.reverse();
}
}