-
Notifications
You must be signed in to change notification settings - Fork 88
Expand file tree
/
Copy pathlinkedlist-circular.java
More file actions
161 lines (146 loc) · 4.16 KB
/
linkedlist-circular.java
File metadata and controls
161 lines (146 loc) · 4.16 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
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.util.*;
/* Class Node */
class Node
{
protected double data;
protected Node next, prev;
/* Constructor */
public Node()
{
next = null;
prev = null;
data = 0;
}
/* Constructor */
public Node(double data, Node next, Node prev)
{
this.data = data;
this.next = next;
this.prev = prev;
}
/* Function to set link to next node */
public void setLinkNext(Node n)
{
next = n;
}
/* Function to set link to previous node */
public void setLinkPrev(Node p)
{
prev = p;
}
/* Function to get link to next node */
public Node getLinkNext()
{
return next;
}
/* Function to get link to previous node */
public Node getLinkPrev()
{
return prev;
}
/* Function to set data to node */
public void setData(double d)
{
data = d;
}
/* Function to get data from node */
public double getData()
{
return data;
}
}
/* Class linkedList */
class linkedList
{
protected Node start;
protected Node end ;
public int size;
/* Constructor */
public linkedList()
{
start = null;
end = null;
size = 0;
}
/*Function to insert element */
public void insert(double d)
{
Node nptr = new Node(d, null, null);
if (start == null)
{
nptr.setLinkNext(nptr);
nptr.setLinkPrev(nptr);
start = nptr;
end = start;
}
else
{
nptr.setLinkPrev(end);
end.setLinkNext(nptr);
start.setLinkPrev(nptr);
nptr.setLinkNext(start);
end = nptr;
}
size++;
}
/* Function to display status of list */
public void display()
{
System.out.print("\nCircular Doubly Linked List = ");
Node ptr = start;
if (size == 0)
{
System.out.print("empty\n");
return;
}
if (start.getLinkNext() == start)
{
System.out.print(start.getData()+ " "+ptr.getData()+ "\n");
return;
}
System.out.print(start.getData()+ " ");
ptr = start.getLinkNext();
while (ptr.getLinkNext() != start)
{
System.out.print(ptr.getData()+ " ");
ptr = ptr.getLinkNext();
}
System.out.print(ptr.getData()+ " ");
ptr = ptr.getLinkNext();
System.out.print(ptr.getData()+ "\n");
}
public Iterator<Integer> Iterator()
{
return null;
}
}
/* Class CircularDoublyLinkedList */
public class CircularDoublyLinkedList
{
public static void main(String[] args)
{
Scanner scan = new Scanner(System.in);
/* Creating object of linkedList */
linkedList list = new linkedList();
System.out.println("run :");
System.out.println("exit - to end the loop");
/* Perform list operations */
for(int i=1; i<=5; i++)
{
try
{
System.out.println("Enter the Node");
list.insert( scan.nextDouble() );
}
catch (InputMismatchException e)
{
list.display();
System.exit(0);
}
}
/* For Displaying the List*/
list.display();
/* Scanner Close*/
scan.close();
}
}