Skip to content

Commit c57bf5e

Browse files
authored
Added the Linked List Program
1 parent d4f6b3d commit c57bf5e

1 file changed

Lines changed: 249 additions & 0 deletions

File tree

Linked_List.java

Lines changed: 249 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,249 @@
1+
//https://www.facebook.com/zawed.akhtar.7923/posts/965508713932178
2+
//Subscribed by MySirG.com
3+
import java.util.*;
4+
import java.lang.*;
5+
class Node
6+
{
7+
private int data;
8+
private Node next;
9+
10+
public Node()
11+
{
12+
data=0;
13+
next=null;
14+
}
15+
public Node(int d,Node n)
16+
{
17+
data=d;
18+
next=n;
19+
}
20+
public void setData(int d)
21+
{
22+
data=d;
23+
}
24+
public void setNext(Node n)
25+
{
26+
next=n;
27+
}
28+
public int getData()
29+
{
30+
return(data);
31+
}
32+
public Node getNext()
33+
{
34+
return(next);
35+
}
36+
}
37+
class LinkedList
38+
{
39+
private int size;
40+
private Node start;
41+
public LinkedList()
42+
{
43+
size=0;
44+
start=null;
45+
}
46+
public boolean isEmpty()
47+
{
48+
if(start==null)
49+
return(true);
50+
else
51+
return(false);
52+
}
53+
public int getListSize()
54+
{
55+
return(size);
56+
}
57+
public void viewList()
58+
{
59+
Node t;
60+
if(isEmpty())
61+
{
62+
System.out.println("List is empty");
63+
}
64+
else
65+
{
66+
t=start;
67+
for(int i=1;i<=size;i++)
68+
{
69+
System.out.print(" "+t.getData());
70+
t=t.getNext();
71+
}
72+
}
73+
System.out.print("\n");
74+
}
75+
public void insertAtFirst(int val)
76+
{
77+
Node n;
78+
n=new Node();
79+
n.setData(val);
80+
n.setNext(start);
81+
start=n;
82+
size=size+1;
83+
}
84+
public void insertAtLast(int val)
85+
{
86+
Node n,t;
87+
n=new Node();
88+
n.setData(val);
89+
if(start==null)
90+
{
91+
start=n;
92+
}
93+
else
94+
{
95+
t=start;
96+
while(t.getNext()!=null)
97+
{
98+
t=t.getNext();
99+
}
100+
t.setNext(n);
101+
}
102+
size=size+1;
103+
}
104+
public void insertAtPos(int val,int pos)
105+
{
106+
Node n,t;
107+
if(pos==1)
108+
{
109+
insertAtFirst(val);
110+
}
111+
else if(pos==size+1)
112+
{
113+
insertAtLast(val);
114+
}
115+
else if(pos>1 && pos<=size)
116+
{
117+
n=new Node(val,null);
118+
t=start;
119+
for(int i=1;i<pos-1;i++)
120+
{
121+
t=t.getNext();
122+
}
123+
n.setNext(t.getNext());
124+
t.setNext(n);
125+
size=size+1;
126+
}
127+
else
128+
{
129+
System.out.println("Insertion not possible at position" +pos);
130+
}
131+
}
132+
public void deleteFirst()
133+
{
134+
if(start==null)
135+
{
136+
System.out.println("List is empty");
137+
}
138+
else
139+
{
140+
start=start.getNext();
141+
size=size-1;
142+
}
143+
}
144+
public void deleteLast()
145+
{
146+
if(start==null)
147+
{
148+
System.out.println("List is empty");
149+
}
150+
else if(size==1)
151+
{
152+
start=null;
153+
size=size-1;
154+
}
155+
else
156+
{
157+
Node t;
158+
t=start;
159+
for(int i=1;i<size-1;i++)
160+
{
161+
t=t.getNext();
162+
}
163+
t.setNext(null);
164+
}
165+
size=size-1;
166+
}
167+
public void deleteAtPos(int pos)
168+
{
169+
if(start==null)
170+
System.out.println("List is empty");
171+
else if(pos<1 || pos>size)
172+
System.out.println("Invalid Position");
173+
else if(pos==1)
174+
deleteFirst();
175+
else if(pos==size)
176+
deleteLast();
177+
else
178+
{
179+
Node t,t1;
180+
t=start;
181+
for(int i=1;i<pos-1;i++)
182+
t=t.getNext();
183+
t1=t.getNext();
184+
t.setNext(t1.getNext());
185+
size--;
186+
}
187+
}
188+
}
189+
public class Test
190+
{
191+
public static void main(String[] args)
192+
{
193+
Scanner sc=new Scanner(System.in);
194+
LinkedList list=new LinkedList();
195+
boolean flag=true;
196+
while(flag)
197+
{
198+
System.out.println("1.Add item to the list of start");
199+
System.out.println("2.Add item to the list of last");
200+
System.out.println("3.Add item to the list of position");
201+
System.out.println("4.Delete First item from the list");
202+
System.out.println("5.Delete Last item from the list");
203+
System.out.println("6.Delete item from the list which given position");
204+
System.out.println("7.View List");
205+
System.out.println("8:Exit");
206+
System.out.println("Enter your choice");
207+
int ch=sc.nextInt();
208+
int position,value;
209+
switch(ch)
210+
{
211+
case 1:
212+
System.out.println("Enter value of list item:");
213+
value=sc.nextInt();
214+
list.insertAtFirst(value);
215+
break;
216+
case 2:
217+
System.out.println("Enter value of list item:");
218+
value=sc.nextInt();
219+
list.insertAtLast(value);
220+
break;
221+
case 3:
222+
System.out.println("Enter the position:");
223+
position=sc.nextInt();
224+
System.out.println("Enter value of list item:");
225+
value=sc.nextInt();
226+
list.insertAtPos(value,position);
227+
break;
228+
case 4:
229+
list.deleteFirst();
230+
break;
231+
case 5:
232+
list.deleteLast();
233+
break;
234+
case 6:
235+
System.out.println("Enter the position:");
236+
position=sc.nextInt();
237+
list.deleteAtPos(position);
238+
break;
239+
case 7:
240+
list.viewList();
241+
break;
242+
default:
243+
System.out.println("Invalid Choice. . . .");
244+
245+
246+
}
247+
}
248+
}
249+
}

0 commit comments

Comments
 (0)