diff --git a/src/main/java/com/thealgorithms/datastructures/LinkedList/Doubly/doubly_ll.java b/src/main/java/com/thealgorithms/datastructures/LinkedList/Doubly/doubly_ll.java new file mode 100644 index 000000000000..74dde6844d2f --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/LinkedList/Doubly/doubly_ll.java @@ -0,0 +1,89 @@ +import java.util.*; + +public class DoubelLL +{ + static class Node{ + int data; + Node next; + Node prev; + + Node(int data){ + this.data=data; + next=prev=null; + } + } + static Node head; + static Node tail; + public void addFirst(int data){ + Node newNode=new Node(data); + if(head==null){ + head=tail=newNode; + return; + } + newNode.next=head; + head.prev=newNode; + head=newNode; + } + static void removeLast(){ + if(head==null){ + System.out.println("Empty..."); + return; + } + else if(head.next==null){ + head=tail=null; + return; + } + Node curr=head; + while(curr.next!=tail){ + curr=curr.next; + } + curr.next.prev=null; + curr.next=null; + tail=curr; + } + static void removeFirst(){ + if(head==null){ + System.out.println("Empty..."); + return; + } + else if(head.next==null){ + head=tail=null; + return; + } + head=head.next; + head.prev=null; + + + } + + static void show(){ + Node curr=head; + while(curr!=null){ + System.out.print(curr.data+" "); + curr=curr.next; + } + System.out.println(); + } + static void printInReverse(){ + Node curr=tail; + while(curr!=null){ + System.out.print(curr.data+" "); + curr=curr.prev; + } + } + public static void main(String[] args) { + DoubelLL dl=new DoubelLL(); + dl.addFirst(5); + dl.addFirst(4); + dl.addFirst(3); + dl.addFirst(2); + dl.addFirst(1); + show(); + removeLast(); + show(); + removeFirst(); + show(); + System.out.println("Now print in Reverse Order..."); + printInReverse(); + } +} diff --git a/src/main/java/com/thealgorithms/datastructures/LinkedList/Linked_list.java b/src/main/java/com/thealgorithms/datastructures/LinkedList/Linked_list.java new file mode 100644 index 000000000000..3ee5c22ec054 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/LinkedList/Linked_list.java @@ -0,0 +1,116 @@ +import java.util.*; +public class LinkedListt{ + static class Node{ + int data; + Node next; + Node(int data){ + this.data=data; + next=null; + } + } + public static Node head; + public static Node tail; + public void addFirst(int data){ + Node newNode=new Node(data); + if(head==null){ + head=tail=newNode; + return; + } + + newNode.next=head; + head=newNode; + } + public void addlast(int data){ + Node newNode=new Node(data); + if(head==null){ + head=tail=newNode; + return; + } +// while(tail.next!=null){ +// tail=tail.next; +// } + tail.next=newNode; + tail=newNode; + } + public void addMiddle(int data,int index){ + Node newNode=new Node(data); + int i=1; + Node curr=head; + while(i"); + curr=curr.next; + } + System.out.println(); + } + + + + public static void main(String[] args) { + LinkedList li=new LinkedList(); + li.addFirst(1); + li.addFirst(2); + li.addFirst(3); + li.addFirst(4); + li.addFirst(5); + // li.show(); + Node mid=li.findMid(); + Node prev=li.reverseMid(mid);//2nd half ka head prev hi hoga + li.show(prev); + + + + + } +} diff --git a/src/main/java/com/thealgorithms/datastructures/LinkedList/zig_zag_ll.java b/src/main/java/com/thealgorithms/datastructures/LinkedList/zig_zag_ll.java new file mode 100644 index 000000000000..492499074f79 --- /dev/null +++ b/src/main/java/com/thealgorithms/datastructures/LinkedList/zig_zag_ll.java @@ -0,0 +1,114 @@ +import java.util.*; +public class LinkedList +{ + static class Node{ + char item; + Node next; + Node(char item){ + this.item=item; + next=null; + } + } + static Node head1; + static Node tail1; + static Node head2; + static Node tail2; + + // static void join_ll(Node left,Node right){ + + // if(left==null || right==null){ + // if(left==null){ + // temp.next=right; + // return; + // } + // else{ + // temp.next=left; + // return; + // } + // } + // temp.next=left; + // temp=temp.next; + // temp.next=right; + // temp=temp.next; + // join_ll(left.next,right.next); + // } + static Node join_ll(Node left,Node right){ + Node dummy=new Node('z'); + Node temp=dummy; + + while(left!=null && right!=null){ + temp.next=left; + left=left.next; + temp=temp.next; + temp.next=right; + right=right.next; + temp=temp.next; + } + while(left!=null){ + temp.next=left; + left=left.next; + temp=temp.next; + } + while(right!=null){ + temp.next=right; + right=right.next; + temp=temp.next; + } + + return dummy.next; + + } + static void show(Node head_new){ + Node curr=head_new; + while(curr!=null){ + System.out.print(curr.item+"->"); + curr=curr.next; + } + System.out.println(); + } + static void addLast_left(char item){ + Node newNode=new Node(item); + if(head1==null){ + head1=tail1=newNode; + return; + } + tail1.next=newNode; + tail1=newNode; + } + static void addLast_right(char item){ + Node newNode=new Node(item); + if(head2==null){ + head2=tail2=newNode; + return; + } + tail2.next=newNode; + tail2=newNode; + } + public static void main(String[] args) { +// a->b->c->d; +// 1->2->3; +// a->1->b->2->c-3->d; + LinkedList left=new LinkedList(); + left.addLast_left('a'); + left.addLast_left('b'); + left.addLast_left('c'); + left.addLast_left('d'); + left.addLast_left('e'); + left.addLast_left('f'); + + LinkedList right=new LinkedList(); + right.addLast_right('1'); + right.addLast_right('2'); + right.addLast_right('3'); + // if(right==null && right==null){ + // System.out.println("Botha re null"); + // } + // + // print_ll(dummy.next); + // show(head2); + // show(head1); + Node new_pointer=join_ll(head1,head2); + show(new_pointer); + +} +}