-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathLinearList.java
110 lines (109 loc) · 1.82 KB
/
LinearList.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
package list;
public class LinearList<D> implements List<D>
{
ListObject<D> head, tail, temp;
Integer j=0;
public LinearList()
{
tail=null;
head=null;
}
public void add(D v)
{
if(head==null)
{
ListObject<D> obj=new ListObject<D>(v);
head=tail=obj;
}
else
{
ListObject<D> obj=new ListObject<D>(v);
tail.next=obj;
tail=obj;
}
}
public void traverse()
{
for(ListObject<D> i=head; i!=null; i=i.next)
{
System.out.print(i.val+" ");
}
System.out.println();
}
public void add(D v, Integer pos)
{
if(pos==0)
{
ListObject<D> newNode=new ListObject<D>(v);
newNode.next=head;
head=newNode;
}
else
{
ListObject<D> temp=head;
for(j=0;j<pos-1;j++)
temp=temp.next;
ListObject<D> addNode=new ListObject<D>(v);
addNode.next=temp.next;
temp.next=addNode;
}
}
public void remove(Integer pos)
{
checkIndex(pos);
if(pos==0)
head=head.next;
else
{
j=1;
for(temp=head; j<pos;j++)
temp=temp.next;
temp.next = temp.next.next;
}
}
public void checkIndex(Integer pos)
{
Integer size=getSize();
if(pos<0||pos>size)
{
throw new IndexOutOfBoundsException("index = "+pos+"\nsize = "+size+"\n");
}
else System.out.println("index position found");
}
public Integer getSize()
{
j=0;
for(ListObject<D> i=head; i!=null; i=i.next)
++j;
return j;
}
public void reverse()
{
Object arr[]=new Object[getSize()];
j=0;
for(ListObject<D> i=head; i!=null; i=i.next,j++)
arr[j]=i.val;
--j;
for(ListObject<D> i=head; i!=null; i=i.next,j--)
i.val=(D)arr[j];
}
public D get(Integer pos)
{
checkIndex(pos);
temp=head;
for(j=0;j<pos;j++)
temp=temp.next;
return temp.val;
}
public Integer search(D v)
{
j=0;
for(ListObject<D> i=head; i!=null; i=i.next)
{
if(i.val.equals(v))
return j;
j++;
}
return -1;
}
}