Skip to content

Commit e342f68

Browse files
committed
adding linkedlist
1 parent e78d53d commit e342f68

File tree

7 files changed

+568
-0
lines changed

7 files changed

+568
-0
lines changed
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import java.util.*;
2+
3+
public class DoubelLL
4+
{
5+
static class Node{
6+
int data;
7+
Node next;
8+
Node prev;
9+
10+
Node(int data){
11+
this.data=data;
12+
next=prev=null;
13+
}
14+
}
15+
static Node head;
16+
static Node tail;
17+
public void addFirst(int data){
18+
Node newNode=new Node(data);
19+
if(head==null){
20+
head=tail=newNode;
21+
return;
22+
}
23+
newNode.next=head;
24+
head.prev=newNode;
25+
head=newNode;
26+
}
27+
static void removeLast(){
28+
if(head==null){
29+
System.out.println("Empty...");
30+
return;
31+
}
32+
else if(head.next==null){
33+
head=tail=null;
34+
return;
35+
}
36+
Node curr=head;
37+
while(curr.next!=tail){
38+
curr=curr.next;
39+
}
40+
curr.next.prev=null;
41+
curr.next=null;
42+
tail=curr;
43+
}
44+
static void removeFirst(){
45+
if(head==null){
46+
System.out.println("Empty...");
47+
return;
48+
}
49+
else if(head.next==null){
50+
head=tail=null;
51+
return;
52+
}
53+
head=head.next;
54+
head.prev=null;
55+
56+
57+
}
58+
59+
static void show(){
60+
Node curr=head;
61+
while(curr!=null){
62+
System.out.print(curr.data+" ");
63+
curr=curr.next;
64+
}
65+
System.out.println();
66+
}
67+
static void printInReverse(){
68+
Node curr=tail;
69+
while(curr!=null){
70+
System.out.print(curr.data+" ");
71+
curr=curr.prev;
72+
}
73+
}
74+
public static void main(String[] args) {
75+
DoubelLL dl=new DoubelLL();
76+
dl.addFirst(5);
77+
dl.addFirst(4);
78+
dl.addFirst(3);
79+
dl.addFirst(2);
80+
dl.addFirst(1);
81+
show();
82+
removeLast();
83+
show();
84+
removeFirst();
85+
show();
86+
System.out.println("Now print in Reverse Order...");
87+
printInReverse();
88+
}
89+
}
Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
import java.util.*;
2+
public class LinkedListt{
3+
static class Node{
4+
int data;
5+
Node next;
6+
Node(int data){
7+
this.data=data;
8+
next=null;
9+
}
10+
}
11+
public static Node head;
12+
public static Node tail;
13+
public void addFirst(int data){
14+
Node newNode=new Node(data);
15+
if(head==null){
16+
head=tail=newNode;
17+
return;
18+
}
19+
20+
newNode.next=head;
21+
head=newNode;
22+
}
23+
public void addlast(int data){
24+
Node newNode=new Node(data);
25+
if(head==null){
26+
head=tail=newNode;
27+
return;
28+
}
29+
// while(tail.next!=null){
30+
// tail=tail.next;
31+
// }
32+
tail.next=newNode;
33+
tail=newNode;
34+
}
35+
public void addMiddle(int data,int index){
36+
Node newNode=new Node(data);
37+
int i=1;
38+
Node curr=head;
39+
while(i<index){
40+
curr=curr.next;
41+
i++;
42+
}
43+
newNode.next=curr.next;
44+
curr.next=newNode;
45+
}
46+
public void removeFirst(){
47+
if(head==null){
48+
System.out.println("LL is empty()");
49+
return;
50+
}
51+
else if (head.next==null){
52+
head=tail=null;
53+
return;
54+
}
55+
System.out.println("deleted data:"+head.data);
56+
head=head.next;
57+
return;
58+
}
59+
public void removeLast(){
60+
if(head==null){
61+
System.out.println("LL is empty()");
62+
return;
63+
}
64+
else if(head.next==null){
65+
head=tail=null;
66+
return;
67+
}
68+
Node curr=head;
69+
while(curr.next!=tail){
70+
curr=curr.next;
71+
}
72+
System.out.println("deleted data:"+tail.data);
73+
curr.next=null;
74+
tail=curr;
75+
}
76+
public void show(){
77+
Node curr=head;
78+
while(curr!=null){
79+
System.out.print(curr.data+" ");
80+
curr=curr.next;
81+
}
82+
System.out.println();
83+
}
84+
85+
public int size(){
86+
Node curr=head;
87+
int count=0;
88+
while(curr!=null){
89+
count++;
90+
curr=curr.next;
91+
}
92+
return count;
93+
}
94+
95+
public static void main(String args[]){
96+
LinkedListt ll=new LinkedListt();
97+
ll.addFirst(1);
98+
ll.addFirst(2);
99+
ll.addFirst(3);
100+
ll.addlast(4);
101+
ll.addlast(5);
102+
ll.addlast(6);
103+
ll.show();
104+
int siz=ll.size();
105+
System.out.println("Size of LinkedList:"+siz);
106+
// int index=siz%2==0?siz/2 : siz/2 +1;
107+
ll.addMiddle(55,siz/2);
108+
ll.show();
109+
System.out.println("Remove the first data");
110+
ll.removeFirst();
111+
ll.show();
112+
ll.removeLast();
113+
ll.show();
114+
115+
}
116+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
import java.util.*;
2+
3+
public class LinekedList
4+
{
5+
static class Node{
6+
int data;
7+
Node next;
8+
9+
Node(int data){
10+
this.data=data;
11+
next=null;
12+
}
13+
}
14+
static Node head;
15+
static Node tail;
16+
static void addLast(int data){
17+
Node newNode=new Node(data);
18+
if(head==null){
19+
head=tail=newNode;
20+
return;
21+
}
22+
tail.next=newNode;
23+
tail=newNode;
24+
}
25+
static void show(Node head){
26+
Node curr=head;
27+
while(curr!=null){
28+
System.out.print(curr.data+" ");
29+
curr=curr.next;
30+
}
31+
System.out.println();
32+
}
33+
static Node getMid(Node head){
34+
Node slow=head;
35+
Node fast=head.next;
36+
37+
while(fast!=null && fast.next!=null){
38+
slow=slow.next;
39+
fast=fast.next.next;
40+
}
41+
return slow;
42+
}
43+
static Node reverse(Node mid){
44+
45+
Node curr=mid;
46+
Node prev=null;
47+
while(curr!=null){
48+
Node temp=curr.next;
49+
curr.next=prev;
50+
prev=curr;
51+
curr=temp;
52+
}
53+
return prev;
54+
}
55+
public static void main(String[] args) {
56+
LinekedList ll=new LinekedList();
57+
ll.addLast(1);
58+
ll.addLast(2);
59+
ll.addLast(3);
60+
ll.addLast(4);
61+
ll.addLast(5);
62+
63+
Node mid=getMid(head);
64+
Node head2=reverse(mid);
65+
66+
Node head1=head;
67+
Node dummy=new Node(-1);
68+
Node temp=dummy;
69+
while(head1!=mid && head2!=null){
70+
temp.next=head1;
71+
temp=temp.next;
72+
head1=head1.next;
73+
temp.next=head2;
74+
head2=head2.next;
75+
temp=temp.next;
76+
}
77+
show(dummy.next);
78+
}
79+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
public class LinkedList{
3+
static class Node{
4+
int data;
5+
Node next;
6+
Node(int data){
7+
this.data=data;
8+
next=null;
9+
}
10+
}
11+
static Node head;
12+
static Node tail;
13+
void addFirst(int data){
14+
Node newNode=new Node(data);
15+
if(head==null){
16+
head=tail=newNode;
17+
return;
18+
}
19+
newNode.next=head;
20+
head=newNode;
21+
}
22+
void addLast(int data){
23+
Node newNode=new Node(data);
24+
if(head==null){
25+
head=tail=newNode;
26+
return;
27+
}
28+
tail.next=newNode;
29+
tail=newNode;
30+
}
31+
int search(Node curr,int val,int i){
32+
if(curr==null){
33+
return -1;
34+
}
35+
if(curr.data==val){
36+
return i+1;
37+
}
38+
return search(curr.next,val,i+1);
39+
}
40+
public static void main(String args[]){
41+
LinkedList ll=new LinkedList();
42+
ll.addFirst(4);
43+
ll.addFirst(3);
44+
ll.addLast(2);
45+
46+
System.out.println(ll.search(head,3,0));
47+
48+
}
49+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
import java.util.*;
2+
3+
public class LinkedList
4+
{
5+
static class Node{
6+
int data;
7+
Node next;
8+
Node(int data){
9+
this.data=data;
10+
next=null;
11+
}
12+
}
13+
static Node head;
14+
static Node tail;
15+
void add(int data){
16+
Node newNode=new Node(data);
17+
if(head==null){
18+
head=tail=newNode;
19+
return;
20+
}
21+
newNode.next=head;
22+
head=newNode;
23+
}
24+
void show(){
25+
Node curr=head;
26+
while(curr!=null){
27+
System.out.print(curr.data+" ");
28+
curr=curr.next;
29+
}
30+
System.out.println();
31+
}
32+
void segregate(){
33+
Node odd=head;
34+
Node even=head.next;
35+
odd.next=even.next;
36+
odd=odd.next;
37+
System.out.println(odd.data);
38+
}
39+
public static void main(String[] args) {
40+
LinkedList ll=new LinkedList();
41+
ll.add(5);
42+
ll.add(4);
43+
ll.add(3);
44+
ll.add(2);
45+
ll.add(1);
46+
ll.show();
47+
ll.segregate();
48+
}
49+
}

0 commit comments

Comments
 (0)