-
Notifications
You must be signed in to change notification settings - Fork 1
/
LinkedListMain.java
191 lines (178 loc) · 4.33 KB
/
LinkedListMain.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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
import java.io.*;
import java.util.*;
/*Node Class or node of the linked list*/
class Node{
int data;
Node next;
Node(int data){
this.data=data;
next = null;
}
}
/*Linked list class*/
class LinkedList{
Node head;
/*insert function insert at last index*/
public void insert(int d){
Node newNode=new Node(d);
if(head==null){
head=newNode;
}else{
Node last=head;
while(last.next!=null){
last=last.next;
}
last.next=newNode;
}
}
/*Inserting at specific index*/
public void insertAt(int index,int data){
if(index>size()){
System.out.println("Invalid Index!");
}else{
Node newNode = new Node(data);
Node node=head;
int count=0;
if(index==0){
insertAtStart(data);
}else{
while(count<index-1){
node = node.next;
count++;
}
Node temp=node.next;
node.next=newNode;
node.next.next=temp;
}
}
}
/*Deleting last element from the LinkedList*/
public void delete(){
if(isEmpty()){
System.out.println("List is empty!");
}else{
Node last = head;
while(last.next.next!=null){
last=last.next;
}
last.next=null;
}
}
/*Deleting element at specific index*/
public void deleteAt(int index){
if(index==0){
deleteAtStart();
}else if(index>size()-1){
System.out.println("Invalid Index!");
}
else{
int count = 0;
Node last = head;
while(count<index-1){
last=last.next;
count++;
}
last.next=last.next.next;
}
}
/*Deleting element frok start*/
public void deleteAtStart(){
head=head.next;
}
/*Inserting at start*/
public void insertAtStart(int data){
Node newNode = new Node(data);
if(head==null){
insert(data);
}else{
Node temp=head;
head=newNode;
head.next=temp;
}
}
/*Size of the Linked List*/
public int size(){
Node curr=head;
int count=0;
while(curr!=null){
curr=curr.next;
count++;
}
return count;
}
/*Reversing the LinkedList*/
public void reverse(){
Node curr=head;
Node prev=null;
Node forward=null;
while(curr.next!=null){
forward=curr.next;
curr.next=prev;
prev=curr;
curr=forward;
}
head=curr;
head.next=prev;
}
/*Printing the LinkedList*/
public void printList(){
Node node=head;
while(node.next!=null){
System.out.print(node.data+" ");
node=node.next;
}System.out.print(node.data+" ");
System.out.println();
}
public boolean isEmpty(){
if(head==null)
return true;
return false;
}
}
/*Main Clss */
class LinkedListMain{
public static void main(String[] args){
LinkedList list = new LinkedList();
Scanner scan = new Scanner(System.in);
PrintStream ps = new PrintStream(System.out);
while(true){
ps.println("Enter the operations!\n 1. Insert \n 2. Insert At Index \n 3. Display \n 4. Delete From last \n 5. Delet At Index \n 6. Delete At start \n 7. Size \n 8. CheckForEmpty \n 9. Reverse the linkedList \n 10.Exit");
int T=scan.nextInt();
switch(T){
case 1: ps.println("Enter the no. of elements you want to insert in the LinkedList!");
int n = scan.nextInt();
while(n-->0){
list.insert(scan.nextInt());
}
break;
case 2: ps.println("Enter the index and element");
int index = scan.nextInt();
int element=scan.nextInt();
list.insertAt(index,element);
break;
case 3: list.printList();
break;
case 4: list.delete();
break;
case 5: ps.println("Enter the index!");
list.deleteAt(scan.nextInt());
break;
case 6: list.deleteAtStart();
break;
case 7: ps.println("Size of the Linked List is "+ list.size());
break;
case 8: if(list.isEmpty()){
ps.println("List is Empty!");
}else{
ps.println("List is not Empty!");
}
break;
case 9: list.reverse();
list.printList();
break;
case 10: System.exit(0);
default: ps.println("Enter the valid options!");
}
}
}
}