-
Notifications
You must be signed in to change notification settings - Fork 0
/
Operations_linked_list.java
156 lines (137 loc) · 4.31 KB
/
Operations_linked_list.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
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
import java.util.Scanner;
public class Operations_linked_list {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
LinkedList l1 = new LinkedList();
while (true) {
System.out.println("enter 1 to insert at front");
System.out.println("enter 2 to insert at end");
System.out.println("enter 3 to insert at position");
System.out.println("enter 4 to delete at front");
System.out.println("enter 5 to delete at end");
System.out.println("enter 6 to delete at position");
System.out.println("enter 7 to display");
System.out.println("enter 8 to revrese");
System.out.println("enter 9 to exit");
int choice = sc.nextInt();
switch (choice) {
case 1:
System.out.println("enter the data to be inserted");
int data = sc.nextInt();
l1.insertAtFront(data);
break;
case 2:
System.out.println("enter the data to be inserted");
int data1 = sc.nextInt();
l1.insertAtEnd(data1);
break;
case 3:
System.out.println("enter the data to be inserted");
int data2 = sc.nextInt();
System.out.println("enter the position");
int pos = sc.nextInt();
l1.insertAtPosition(data2, pos);
break;
case 4:
l1.deleteAtFront();
break;
case 5:
l1.deleteAtLast();
break;
case 6:
System.out.println("enter the position");
int pos1 = sc.nextInt();
l1.deleteAtPosition(pos1);
break;
case 7:
l1.display();
break;
case 8:
l1.reverse();
break;
case 9:
System.exit(0);
}
}
}
}
class LinkedList {
Node first;
public void insertAtFront(int data) {
Node newnode = new Node(data);
newnode.link = first;
first=newnode;
}
public void insertAtEnd(int data) {
Node newnode = new Node(data);
Node save = first;
while (save.link != null) {
save = save.link;
}
save.link = newnode;
}
public void insertAtPosition(int data, int pos) {
Node newnode = new Node(data);
Node save = first;
int i = 1;
while (i < pos - 1) {
save = save.link;
i++;
}
newnode.link = save.link;
save.link = newnode;
}
public void deleteAtFront() {
if (first == null) {
System.out.println("List is empty");
}
first = first.link;
}
public void deleteAtLast() {
if (first == null) {
System.out.println("List is empty");
}
Node save = first;
while (save.link.link != null) {
save = save.link;
}
save.link = null;
}
public void deleteAtPosition(int data) {
if (first == null) {
System.out.println("List is empty");
} else {
Scanner sc = new Scanner(System.in);
int key = sc.nextInt();
}
}
public void display() {
// System.out.println(first);
Node save = first;
while (save != null) {
System.out.println(save.data);
save = save.link;
}
}
public void reverse(){
Node prev =null;
Node current=first;
Node next=first.link;
while(current.link !=null){
current.link=prev;
prev=current;
current=next;
next=current.link;
}
current.link=prev;
first=current;
}
}
class Node {
int data;
Node link;
Node(int data) {
this.data = data;
this.link = null;
}
}