-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlinkedList.java
More file actions
140 lines (138 loc) · 3.4 KB
/
linkedList.java
File metadata and controls
140 lines (138 loc) · 3.4 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
// package dataStructure;
import java.util.*;
class Node{
int val;
Node next;
Node(int val , Node link){
this.val=val;
this.next=link;
}
Node(int val){
this.val=val;
}
}
class LinkedList{
Node head;
Node tail;
int size;
LinkedList(){
this.head=null;
this.tail=null;
this.size=0;
}
void addFirst(int val){
Node temp = new Node(val);
if(this.head==null){
this.head=temp;
this.size++;
this.tail=temp;
}
else{
temp.next=head;
this.head=temp;
this.size++;
}
}
void addLast(int val){
Node temp = new Node(val);
if(this.head==null){
this.head=temp;
this.tail=temp;
this.size++;
}else{
this.tail.next=temp;
this.tail=temp;
this.size++;
}
}
int size(){
return this.size;
}
void addAtIndex(int val , int index){
if(index>this.size+1 || index<0){
throw new IndexOutOfBoundsException("Index should range between 0 and "+(this.size+1));
}
else if(index==0){
addFirst(val);
}else if(index==this.size){
addLast(val);
}else{
int i=0;
Node nodeIterater = this.head;
while(i<index-1){
nodeIterater = nodeIterater.next;
i++;
}
Node ref = nodeIterater.next;
Node temp = new Node(val,ref);
nodeIterater.next=temp;
this.size++;
}
}
void deleteFirst(){
if(this.size==0) return;
this.head=this.head.next;
this.size--;
}
void deleteLast(){
if(this.size==0) return;
Node temp = this.head;
while(temp.next.next!=null){
temp=temp.next;
}
this.tail=temp;
temp.next=null;
this.size--;
}
void deleteAtIndex(int index){
if(index>=0 && index<=this.size-1){
if(index==0){
deleteFirst();
}else{
int i=0;
Node nodeIterater = this.head;
while(i<index-1){
nodeIterater=nodeIterater.next;
i++;
}
nodeIterater.next=nodeIterater.next.next;
}
}
}
void display(){
StringBuilder sb = new StringBuilder();
Node i = this.head;
while(i!=null){
sb.append(i.val+"->");
i=i.next;
}
System.out.println(sb.substring(0,sb.length()-2));
}
}
class main{
static void bubbleSort(LinkedList list){
Node head = list.head;
int size = list.size;
for(Node i=head;i.next!=null;i=i.next){
for(Node j=i.next;j!=null;j=j.next){
if(j.val<i.val){
int temp = j.val;
j.val=i.val;
i.val=temp;
}
}
}
}
public static void main(String args[]){
LinkedList list = new LinkedList();
list.addAtIndex(3,0);
list.addAtIndex(2,0);
list.addAtIndex(1,0);
list.addLast(12);
list.addAtIndex(13,0);
list.display();
// list.deleteAtIndex(0);
bubbleSort(list);
list.display();
}
}