-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedlist
More file actions
94 lines (75 loc) · 2.36 KB
/
linkedlist
File metadata and controls
94 lines (75 loc) · 2.36 KB
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
import java.util.*;
import java.util.Iterator;
void deleteNode(Node del)
{
void insert(int new_data)
{
Node new_node = new Node(new_data);
new_node.next = head;
new_node.prev=null;
if(head != null )
head.prev = new_node;
head = new_node;
}
void insertAfter(Node prev,int new_data)
{
if(prev == null)
{return;}
Node new_node = new Node(new_data);
new_node.next = prev.next;
prev.next = new_node;
new_node.prev = prev;
new_node.next.prev = new_node;
}
// Base case
if (head == null || del == null) {
return;
}
if (head == del) {
head = del.next;
}
if (del.next != null) {
del.next.prev = del.prev;
}
if (del.prev != null) {
del.prev.next = del.next;
}
return;
}
class Main {
public static void main(String args[])
{
LinkedList <String> list= new LinkedList<String>();
list.add("shirish");
list.add("new");
list.add("project");
list.add("working");
list.add(1, "Yellow"); //add the element between position
list.set(1, "Orange"); //replace 2nd element with new value
list.offerLast("Pink"); //add at last position
list.remove(2); // Remove the element in third position from the said linked list
list.clear();// remove all elemtn from linked list
Collections.swap(list, 0, 2); // swap two elements from LinkedList
Collections.shuffle(list); // shuffle all elements in linked list
System.out.println(list.pop()); // remove and return the first element of a linked list.
System.out.println(list.get(1)); // get element according to postion
System.out.println(list.getFirst()); // get first elemtn of linked list
System.out.println(list);
// list1.addAll(list2); for adding one list with another
// https://www.w3resource.com/java-exercises/collection/java-collection-linked-list-exercise-24.php
for(String s : list)
{
System.out.println(s);
}
// iterate in forward irection
Iterator p = list.listIterator(0);
while(p.hasNext()){
System.out.println(p.next());
}
// iterate in reverse direction
Iterator it =list.descendingIterator();
while(it.hasNext()){
System.out.println(it.next());
}
}
}