-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathsll.java
76 lines (73 loc) · 1.39 KB
/
sll.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
//S.L.L stands for singly linked list and this code works even when position entered> size,it removes last element
public class sll
{
node head;
static class node
{
int data;
node next;
node(int data)
{
this.data=data;
next=null;
}
}
public static sll insert(sll list,int data,int pos) //inserting at a position pos
{
node new_node=new node(data);
if(list.head==null)
{
list.head=new_node;
return list;
}
node temp=list.head;int count=1;
while(temp.next!=null && count!=pos)
{
count++;
temp=temp.next;
}
if(temp.next==null)
temp.next=new_node;
else
{
new_node.next=temp.next;
temp.next=new_node;
}
return list;
}
public static void display(sll list)
{
node temp=list.head;
while(temp!=null)
{
System.out.print(temp.data+"\t");
temp=temp.next;
}
}
public static sll remove(sll list,int pos)
{
int count=0;node temp=list.head;
while(count!=pos-1)
{
if(temp.next.next==null)
break;
else
temp=temp.next;
}
if(temp.next.next!=null)
temp.next=temp.next.next;
else
temp.next=null;
return list;
}
public static void main(String args[])
{
sll list=new sll();
list=insert(list,1,1);
list=insert(list,2,1);
list=insert(list,3,10);
list=insert(list,4,1);
list=remove(list,1);
display(list);
}
}