-
Notifications
You must be signed in to change notification settings - Fork 1k
/
Copy pathlinkedList.java
167 lines (166 loc) · 3.86 KB
/
linkedList.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
import java.util.*;
public class Main
{
static Node head;
class Node{
public int data;
public Node next;
Node(int data){
this.data=data;
}
}
public void insert_At_beginning(int data) {
Node ptr = new Node(data);
if(head!=null)
{
ptr.next=head;
head=ptr;
}
else
{
ptr.next=null;
head=ptr;
}
}
public boolean isEmpty()
{
if(head==null)
return true;
return false;
}
public void print() {
Node ctr=head;
while(ctr!=null)
{
System.out.print(ctr.data);
ctr=ctr.next;
}
System.out.println();
}
public void insert_At_End(int data)
{ Node ctr=head;
Node ptr = new Node(data);
if(ctr==null)
{ head=ptr;
ptr.next=null;
return ;
}
else {
while(ctr.next!=null)
ctr=ctr.next;
ctr.next=ptr;
ptr.next=null;
}
}
public void delete_From_Beginning()
{ if(head!=null)
head = head.next;
else
System.out.println("Linked list is empty");
}
public void delete_From_End()
{
Node ctr=head;
if(ctr!=null && ctr.next!=null)
{
while(ctr.next.next!=null)
ctr=ctr.next;
ctr.next=null;
}
else if( ctr!=null && ctr.next==null)
head = null;
else
System.out.println("Linked list is empty");
}
public void delete_At_Position(int c)
{
int i=1;Node ctr=head,ptr;
if(ctr!=null && ctr.next!=null)
{
while(i<c-1 && ctr.next!=null)
ctr=ctr.next;
ptr=ctr.next;
ctr.next=ptr.next;
}
else if( ctr!=null && ctr.next==null)
head = null;
else
System.out.println("Linked list is empty");
return ;
}
public void insert_At_Position(int c,int data)
{
int i=1;Node ctr=head;
if(ctr!=null)
{
while(i<c-1 && ctr.next!=null)
{
ctr=ctr.next;
i++;
}
Node ptr = new Node(data);
ptr.next=ctr.next;
ctr.next=ptr;
}
else
{
Node ptr = new Node(data);
ptr.next=null;
head=ptr;
}
}
public void count()
{
Node ptr = head;int count = 0;
while (ptr!=null)
{
count++;
ptr=ptr.next;
}
System.out.println(count);
}
public static void main(String[] args) {
Scanner sc=new Scanner(System.in);
Main ll = new Main();
System.out.println("Enter 1 to insert at beginning, 2 to insert at end, 3 to insert at position, 4 to delete from beginning, 5 to delete from end, 6 to delete at position and 7 to find the length of Linked List");
System.out.println("Enter choice from 1-7");
boolean f=true;int choice;
while(f)
{
choice = sc.nextInt();
switch(choice)
{
case 1: {
int data=sc.nextInt();
ll.insert_At_beginning(data);ll.print();
break;
}
case 2:{
int data=sc.nextInt();
ll.insert_At_End(data);ll.print();break;
}
case 3:{
int data=sc.nextInt();
int c=sc.nextInt();
ll.insert_At_Position(c, data);ll.print();break;
}
case 4:{
ll.delete_From_Beginning();ll.print();break;
}
case 5:{
ll.delete_From_End();ll.print();break;
}
case 6:{
int c=sc.nextInt();
ll.delete_At_Position(c);ll.print();break;
}
case 7:{
ll.count();
break;
}
default:f=false;
}
System.out.println("Enter choice from 1-7");
}
}
}