-
Notifications
You must be signed in to change notification settings - Fork 25
/
linkedlist.java
72 lines (70 loc) · 1.69 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
package Data_Structures;
//Write a program in java for creating a linked list with elements 5,9,13,25,100,200 and 300. Perform an insertion operation where an element 10 is inserted at the begining.
//Element 500 is inserted at 3rd from last and element 1 is inserted at the last.
//Also perform a deletion operation where the 5th element, element 5 and the last element is deleted.
class linkedlist
{
static node head;
static int j=1;
static class node
{
int data;
node next;
node prev;
node(int d)
{
prev = null;
data = d;
next = null;
}
}
public void add(int data)
{
node node = new node(data);
node.next = head;
head = node;
j++;
}
public void insert(node prev, int data)
{
node node = new node(data);
node.next = prev.next;
prev.next = node;
j++;
}
public void print()
{
node node = head;
while(node!=null)
{
System.out.println(node.data);
node = node.next;
}
}
public static void main(String args[])
{
linkedlist list = new linkedlist();
list.head = null;
list.add(5);
list.add(9);
list.add(13);
list.add(25);
list.add(100);
list.add(200);
list.add(300);
node temp;
temp = head;
for(int i=1;i<2;i++)
{
temp = temp.next;
}
list.insert(temp,500);
temp = head;
for(int i=1;i<j-1;i++)
{
temp = temp.next;
}
list.insert(temp, 1);
list.print();
}
}