From 5e82aa544e9e9788f821c4c49586297139233f07 Mon Sep 17 00:00:00 2001 From: GAURANG-21 Date: Mon, 23 Oct 2023 12:09:51 +0530 Subject: [PATCH 1/2] Contributed Linked List related Java codes by Gaurang Agarwal --- .../Linked List/Circular LL Traversal.java | 31 ++++ java_programs/Linked List/Circular LL.java | 101 +++++++++++ .../Delete first node of singly LL.java | 40 ++++ .../Delete head of circular LL.java | 170 +++++++++++++++++ .../Linked List/Delete head of doubly LL.java | 71 ++++++++ .../Delete last node of singly LL.java | 37 ++++ .../Find n-th node from end in LL.java | 63 +++++++ .../Linked List/Implementation of LL.java | 22 +++ .../Insert at beginning of circular LL.java | 61 +++++++ .../Insert at beginning of doubly LL.java | 44 +++++ .../Insert at beginning of singly LL.java | 50 +++++ .../Insert at end of circular LL.java | 54 ++++++ .../Insert at end of singly LL.java | 62 +++++++ ...Insert at given position in singly LL.java | 102 +++++++++++ java_programs/Linked List/Middle of LL.java | 101 +++++++++++ .../Linked List/Printing elements.java | 29 +++ .../Linked List/Reverse a doubly LL.java | 48 +++++ .../Search an element in singly LL.java | 171 ++++++++++++++++++ .../Linked List/Sort insert in LL.java | 76 ++++++++ 19 files changed, 1333 insertions(+) create mode 100644 java_programs/Linked List/Circular LL Traversal.java create mode 100644 java_programs/Linked List/Circular LL.java create mode 100644 java_programs/Linked List/Delete first node of singly LL.java create mode 100644 java_programs/Linked List/Delete head of circular LL.java create mode 100644 java_programs/Linked List/Delete head of doubly LL.java create mode 100644 java_programs/Linked List/Delete last node of singly LL.java create mode 100644 java_programs/Linked List/Find n-th node from end in LL.java create mode 100644 java_programs/Linked List/Implementation of LL.java create mode 100644 java_programs/Linked List/Insert at beginning of circular LL.java create mode 100644 java_programs/Linked List/Insert at beginning of doubly LL.java create mode 100644 java_programs/Linked List/Insert at beginning of singly LL.java create mode 100644 java_programs/Linked List/Insert at end of circular LL.java create mode 100644 java_programs/Linked List/Insert at end of singly LL.java create mode 100644 java_programs/Linked List/Insert at given position in singly LL.java create mode 100644 java_programs/Linked List/Middle of LL.java create mode 100644 java_programs/Linked List/Printing elements.java create mode 100644 java_programs/Linked List/Reverse a doubly LL.java create mode 100644 java_programs/Linked List/Search an element in singly LL.java create mode 100644 java_programs/Linked List/Sort insert in LL.java diff --git a/java_programs/Linked List/Circular LL Traversal.java b/java_programs/Linked List/Circular LL Traversal.java new file mode 100644 index 0000000..a5bbfed --- /dev/null +++ b/java_programs/Linked List/Circular LL Traversal.java @@ -0,0 +1,31 @@ +public class _26 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void Traversal(node head) + { + if(head==null) + return; + node curr=head; + do{ + System.out.print(head.data+" "); + head=head.next; + }while(head!=curr); + } + public static void main(String[] args) { + node head=new node(10); + node temp1=new node(20); + node temp2=new node(30); + node temp3=new node(40); + head.next=temp1; + temp1.next=temp2; + temp2.next=temp3; + temp3.next=head; + Traversal(head); + } +} diff --git a/java_programs/Linked List/Circular LL.java b/java_programs/Linked List/Circular LL.java new file mode 100644 index 0000000..f2c42e6 --- /dev/null +++ b/java_programs/Linked List/Circular LL.java @@ -0,0 +1,101 @@ +import java.util.*; +public class _22 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node Insert(node head, int pos, int x) + { + node curr=new node(x); + if(head==null) + { curr.next=curr; + return curr; + } + int c=0; + if(head.next==null) + { + head.next=curr; + curr.next=head; + return head; + } + pos-=2; //This was done when doing......... + node temp=null; + node copy=head.next,copy1=head; + while(copy!=copy1) + { + c++; + if(pos==c) + { + temp=copy.next; + copy.next=curr; + curr.next=temp; + break; + } + copy=copy.next; + } + return head; + + } + static void printList(node head) + { + System.out.println(head.data+" "); + if(head.next==null) + { + } + else{ + + node curr=head; + boolean od=true; + while(head!=curr || od) + { + od=false; + System.out.print(head.data+" "); + head=head.next; + } + } + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("Enter number of elements you want to print."); + int n=sc.nextInt(); + node head=null; + int m=0; + for(int i=0;im+1 ||pos<0) + { + System.out.println("The position you entered is incorrect."); + i--; + } + else{ + head=Insert(head, pos, x); + printList(head); + m++; + } + + } + + } + // System.out.println(); + // System.out.println(head.data+" "+head.next.data+" "+head.next.next.data+" "+head.next.next.next.data); + } +} \ No newline at end of file diff --git a/java_programs/Linked List/Delete first node of singly LL.java b/java_programs/Linked List/Delete first node of singly LL.java new file mode 100644 index 0000000..f85139d --- /dev/null +++ b/java_programs/Linked List/Delete first node of singly LL.java @@ -0,0 +1,40 @@ + +public class _10 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node DeleteFirst(node head) + { + if(head==null) + System.exit(0); + else + head=head.next; + return head; + + } + public static void main(String[] args) { + node head=new node(0); + head.next=new node(10); + head.next.next=new node(20); + head.next.next.next=new node(30); + printList(head); + // head=head.next; + System.out.println(); + // printList(head); + head=DeleteFirst(head); + printList(head); + } +} diff --git a/java_programs/Linked List/Delete head of circular LL.java b/java_programs/Linked List/Delete head of circular LL.java new file mode 100644 index 0000000..a741fc7 --- /dev/null +++ b/java_programs/Linked List/Delete head of circular LL.java @@ -0,0 +1,170 @@ +import java.util.*; +public class _29 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node Insert(node head, int x, int pos) + { + node temp=new node(x); + if(head==null) + { + temp.next=temp; + return temp; + } + else if(pos==1) + { + temp.next=head.next; + head.next=temp; + int t=head.data; + head.data=temp.data; + temp.data=t; + return head; + } + else + { + node curr=head; + int c=2; + boolean p=true; + while(curr!=head || p) + { + p=false; + if(pos==c) + { + temp.next=curr.next; + curr.next=temp; + break; + } + curr=curr.next;c++; + } + return head; + } + } + static node Delete(node head, int pos) + { + if(head.next==head) + return null; + else{ + node curr=head; + int c=2; + while(curr.next!=head) + { + if(pos==c) + { + curr.next=curr.next.next; + break; + } + c++; + curr=curr.next; + } + if(pos==1) + { + curr.next=curr.next.next; + return curr.next; + } + else return head; + } + } + static node reverse(node head) + { + node temp1=head.next, prev=null; + while(temp1!=null) + { + head.next=prev; + prev=head; + head=temp1; + temp1=temp1.next; + printList(head); + } + System.out.println(prev.data); + head.next=prev; + return head; + } + static void printList(node head) + { + node curr=head; + while(curr.next!=head) + { + System.out.print(curr.data+" "); + curr=curr.next; + } + System.out.println(curr.data); + + } +public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + node head=null; + System.out.println("If you want to delete elements, enter 'D'.\nIf you want to enter elements, enter 'E'"); +System.out.println("Since this is your first input, 'E' is selected by default"); +char ch='E'; +int x,pos=1,c=0; +do{ + if(ch=='E') + { + do{ + if(c==0) + { + System.out.println("Please enter an element. Also, if you want to stop inserting, press '-1' as input;"); + x=sc.nextInt(); + head=Insert(head,x, pos); + c++; + printList(head); + } + else + { + System.out.println(); + System.out.println("Enter element."); + x=sc.nextInt(); + if(x==-1) + break; + System.out.println("Enter position"); + pos=sc.nextInt(); + if(pos>c +1 || pos<1) + System.out.println("The position you entered is INCORRECT. Please re-enter."); + c++; + Insert(head, x, pos); + printList(head); + } + }while(x!=-1); + System.out.println("Do you wish to delete some elements. If yes, press 'D'. To exit, press 'F'"); + ch=sc.next().charAt(0); +} +if(ch=='D') +{ + do{ + System.out.println("Enter the position from where you want to delete element"); + pos=sc.nextInt(); + // if(pos==-1) + // break; + if(pos>c || pos<0) + {if(pos!=-1) + System.out.println("The position you entered is incorrect. Please re-enter");} + else + { + head=Delete(head, pos); + c--; + if(head!=null) + printList(head); + } + if(c==0) + { + System.out.println("All the elements have been deleted. If you like to add some, press 'E' or if you like to exit, press 'F'."); + ch=sc.next().charAt(0); + break; + } + }while(pos!=-1); + System.out.println("If you wish to enter elements, press 'E' else press 'F' to exit."); + ch=sc.next().charAt(0); +} + +}while(ch!='F'); + head= reverse(head); + printList(head); +} +} + + diff --git a/java_programs/Linked List/Delete head of doubly LL.java b/java_programs/Linked List/Delete head of doubly LL.java new file mode 100644 index 0000000..fe89b8a --- /dev/null +++ b/java_programs/Linked List/Delete head of doubly LL.java @@ -0,0 +1,71 @@ +import java.util.Scanner; +public class _20 { + static class node{ + int data; + node next,prev; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node Delete(node head, int pos) + { + node temp=null; + if(pos == 1) + { + temp=head.next; + temp.prev=null; + head.next=null; + return temp; + } + int c=1; + node curr=head; + boolean y=false; + while(curr.next!=null) + { + if(pos==c) + { + curr.prev.next=curr.next; + curr.next.prev=curr.prev; + y=true; + } + c++; + curr=curr.next; + } + if(y==false) + { + curr.prev.next=null; + curr.prev=null; + } + return head; + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + node pb=new node(100); + node head=new node(10); + node temp1=new node(20); + node temp2=new node(30); + node temp3=new node(40); + head.next=temp1; + temp1.prev=head; + temp1.next=temp2; + temp2.prev=temp1; + temp2.next=temp3; + temp3.prev=temp2; + printList(head); + System.out.println("Enter position of element you want to delete"); + int pos=sc.nextInt(); + head=Delete(head, pos); + printList(head); + } + } + + diff --git a/java_programs/Linked List/Delete last node of singly LL.java b/java_programs/Linked List/Delete last node of singly LL.java new file mode 100644 index 0000000..c1ea4b2 --- /dev/null +++ b/java_programs/Linked List/Delete last node of singly LL.java @@ -0,0 +1,37 @@ +public class _11 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void PrintList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node DeleteLast(node head) + { + if(head==null) + System.exit(0); + node curr=head; + while(curr.next.next!=null) + curr=curr.next; + curr.next=null; + return head; + } + public static void main(String[] args) { + node head=new node(0); + head.next=new node(10); + head.next.next=new node(20); + head.next.next.next=new node(30); + PrintList(head); + head=DeleteLast(head); + PrintList(head); + } +} diff --git a/java_programs/Linked List/Find n-th node from end in LL.java b/java_programs/Linked List/Find n-th node from end in LL.java new file mode 100644 index 0000000..b8c762b --- /dev/null +++ b/java_programs/Linked List/Find n-th node from end in LL.java @@ -0,0 +1,63 @@ +import java.util.*; +public class _34 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + + node head=null; + System.out.println("Enter first element"); + int x=sc.nextInt(); + head=new node(x); + node curr=head; + int c=1; + System.out.println("If you do not wish to enter element, enter -1"); + do{ + System.out.println("\nEnter element"); + x=sc.nextInt(); + if(x==-1) + break; + c++; + node temp=new node(x); + curr.next=temp; + curr=curr.next; + printList(head); + }while(true); + System.out.println("Enter the position where you want to delete element from end"); + int pos=sc.nextInt(); + if(c-pos+1<=0) + System.out.println("The position you entered is incorrect"); + /* METHOD-1 + else + { + for(int i=0;im+1 || pos==0) + { + System.out.println("\nThe position you entered is not correct. Please re-enter."); + i--; + } + else + { + head=Insert(head, x, pos); + m=m+1; + } + PrintList(head); + } + } + } +} diff --git a/java_programs/Linked List/Middle of LL.java b/java_programs/Linked List/Middle of LL.java new file mode 100644 index 0000000..8c92ac8 --- /dev/null +++ b/java_programs/Linked List/Middle of LL.java @@ -0,0 +1,101 @@ +import java.util.*; +public class _33 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static int middle(node head) + { + node slow=head; + node fast=head; + if(head==null) + return -1; + // else if(head.next==null) + // return head.next.data; + else + { + while(fast!=null && fast.next!=null) + { + if(fast.next.next==null) + { + slow=slow.next; + break; + } + fast=fast.next.next; + slow=slow.next; + } + return slow.data; + } + } + static node InsertSort(node head, int x) + { + node temp=new node(x); + node curr=head; + if(head==null) + return temp; + // else if(head.next==null) + // { + // if(head.data<=x) + // { + // head.next=temp; + // return curr; + // } + // else + // { + // temp.next=head; + // return temp; + // } + // } + else + { + do{ + if(head.data>=x) + { + temp.next=head; + return temp; + } + if(head.next==null && head.data<=x) + { + head.next=temp; + return curr; + } + if(head.next.data>=x) + { + temp.next=head.next; + head.next=temp; + return curr; + } + + head=head.next; + }while(head!=null); + } + return curr; + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + System.out.println("how many elements you want ot print"); + Scanner sc=new Scanner(System.in); + node head=null; + int n=sc.nextInt(); + for(int i=0;im+1 || pos==0) + { + System.out.println("\nThe position you entered is not correct. Please re-enter."); + i--; + } + else + { + head=Insert(head, x, pos); + m=m+1; + } + PrintList(head); + } + } + System.out.println("If you want to search element, press 1."); + int t=sc.nextInt(); + if(t==1) + Search(head); + System.out.println(turr.data); + System.out.println("If you want to delete element, press 2."); + t=sc.nextInt(); + + if(t==2) + { + System.out.println("Enter the position where you want to delete the element."); + int pos=sc.nextInt(); + if(pos>m || pos<=0) + System.out.println("The position you entered is incorrect."); + else + { + + do{ + turr=Delete(turr, pos); + PrintList(turr); + m--; + System.out.println("Enter the position where you want to delete the element. To exit, press -1"); + pos=sc.nextInt(); + }while(m>1 && pos!=-1); + } } + } +} diff --git a/java_programs/Linked List/Sort insert in LL.java b/java_programs/Linked List/Sort insert in LL.java new file mode 100644 index 0000000..92714e5 --- /dev/null +++ b/java_programs/Linked List/Sort insert in LL.java @@ -0,0 +1,76 @@ +import java.util.*; +public class _32 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node InsertSort(node head, int x) + { + node temp=new node(x); + node curr=head; + if(head==null) + return temp; + // else if(head.next==null) + // { + // if(head.data<=x) + // { + // head.next=temp; + // return curr; + // } + // else + // { + // temp.next=head; + // return temp; + // } + // } + else + { + do{ + if(head.data>=x) + { + temp.next=head; + return temp; + } + if(head.next==null && head.data<=x) + { + head.next=temp; + return curr; + } + if(head.next.data>=x) + { + temp.next=head.next; + head.next=temp; + return curr; + } + + head=head.next; + }while(head!=null); + } + return curr; + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + System.out.println("how many elements you want ot print"); + Scanner sc=new Scanner(System.in); + node head=null; + int n=sc.nextInt(); + for(int i=0;i Date: Mon, 23 Oct 2023 12:09:51 +0530 Subject: [PATCH 2/2] Linked List basics --- .../Linked List/Circular LL Traversal.java | 31 ++++ java_programs/Linked List/Circular LL.java | 101 +++++++++++ .../Delete first node of singly LL.java | 40 ++++ .../Delete head of circular LL.java | 170 +++++++++++++++++ .../Linked List/Delete head of doubly LL.java | 71 ++++++++ .../Delete last node of singly LL.java | 37 ++++ .../Find n-th node from end in LL.java | 63 +++++++ .../Linked List/Implementation of LL.java | 22 +++ .../Insert at beginning of circular LL.java | 61 +++++++ .../Insert at beginning of doubly LL.java | 44 +++++ .../Insert at beginning of singly LL.java | 50 +++++ .../Insert at end of circular LL.java | 54 ++++++ .../Insert at end of singly LL.java | 62 +++++++ ...Insert at given position in singly LL.java | 102 +++++++++++ java_programs/Linked List/Middle of LL.java | 101 +++++++++++ .../Linked List/Printing elements.java | 29 +++ .../Linked List/Reverse a doubly LL.java | 48 +++++ .../Search an element in singly LL.java | 171 ++++++++++++++++++ .../Linked List/Sort insert in LL.java | 76 ++++++++ 19 files changed, 1333 insertions(+) create mode 100644 java_programs/Linked List/Circular LL Traversal.java create mode 100644 java_programs/Linked List/Circular LL.java create mode 100644 java_programs/Linked List/Delete first node of singly LL.java create mode 100644 java_programs/Linked List/Delete head of circular LL.java create mode 100644 java_programs/Linked List/Delete head of doubly LL.java create mode 100644 java_programs/Linked List/Delete last node of singly LL.java create mode 100644 java_programs/Linked List/Find n-th node from end in LL.java create mode 100644 java_programs/Linked List/Implementation of LL.java create mode 100644 java_programs/Linked List/Insert at beginning of circular LL.java create mode 100644 java_programs/Linked List/Insert at beginning of doubly LL.java create mode 100644 java_programs/Linked List/Insert at beginning of singly LL.java create mode 100644 java_programs/Linked List/Insert at end of circular LL.java create mode 100644 java_programs/Linked List/Insert at end of singly LL.java create mode 100644 java_programs/Linked List/Insert at given position in singly LL.java create mode 100644 java_programs/Linked List/Middle of LL.java create mode 100644 java_programs/Linked List/Printing elements.java create mode 100644 java_programs/Linked List/Reverse a doubly LL.java create mode 100644 java_programs/Linked List/Search an element in singly LL.java create mode 100644 java_programs/Linked List/Sort insert in LL.java diff --git a/java_programs/Linked List/Circular LL Traversal.java b/java_programs/Linked List/Circular LL Traversal.java new file mode 100644 index 0000000..a5bbfed --- /dev/null +++ b/java_programs/Linked List/Circular LL Traversal.java @@ -0,0 +1,31 @@ +public class _26 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void Traversal(node head) + { + if(head==null) + return; + node curr=head; + do{ + System.out.print(head.data+" "); + head=head.next; + }while(head!=curr); + } + public static void main(String[] args) { + node head=new node(10); + node temp1=new node(20); + node temp2=new node(30); + node temp3=new node(40); + head.next=temp1; + temp1.next=temp2; + temp2.next=temp3; + temp3.next=head; + Traversal(head); + } +} diff --git a/java_programs/Linked List/Circular LL.java b/java_programs/Linked List/Circular LL.java new file mode 100644 index 0000000..f2c42e6 --- /dev/null +++ b/java_programs/Linked List/Circular LL.java @@ -0,0 +1,101 @@ +import java.util.*; +public class _22 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node Insert(node head, int pos, int x) + { + node curr=new node(x); + if(head==null) + { curr.next=curr; + return curr; + } + int c=0; + if(head.next==null) + { + head.next=curr; + curr.next=head; + return head; + } + pos-=2; //This was done when doing......... + node temp=null; + node copy=head.next,copy1=head; + while(copy!=copy1) + { + c++; + if(pos==c) + { + temp=copy.next; + copy.next=curr; + curr.next=temp; + break; + } + copy=copy.next; + } + return head; + + } + static void printList(node head) + { + System.out.println(head.data+" "); + if(head.next==null) + { + } + else{ + + node curr=head; + boolean od=true; + while(head!=curr || od) + { + od=false; + System.out.print(head.data+" "); + head=head.next; + } + } + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + System.out.println("Enter number of elements you want to print."); + int n=sc.nextInt(); + node head=null; + int m=0; + for(int i=0;im+1 ||pos<0) + { + System.out.println("The position you entered is incorrect."); + i--; + } + else{ + head=Insert(head, pos, x); + printList(head); + m++; + } + + } + + } + // System.out.println(); + // System.out.println(head.data+" "+head.next.data+" "+head.next.next.data+" "+head.next.next.next.data); + } +} \ No newline at end of file diff --git a/java_programs/Linked List/Delete first node of singly LL.java b/java_programs/Linked List/Delete first node of singly LL.java new file mode 100644 index 0000000..f85139d --- /dev/null +++ b/java_programs/Linked List/Delete first node of singly LL.java @@ -0,0 +1,40 @@ + +public class _10 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node DeleteFirst(node head) + { + if(head==null) + System.exit(0); + else + head=head.next; + return head; + + } + public static void main(String[] args) { + node head=new node(0); + head.next=new node(10); + head.next.next=new node(20); + head.next.next.next=new node(30); + printList(head); + // head=head.next; + System.out.println(); + // printList(head); + head=DeleteFirst(head); + printList(head); + } +} diff --git a/java_programs/Linked List/Delete head of circular LL.java b/java_programs/Linked List/Delete head of circular LL.java new file mode 100644 index 0000000..a741fc7 --- /dev/null +++ b/java_programs/Linked List/Delete head of circular LL.java @@ -0,0 +1,170 @@ +import java.util.*; +public class _29 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node Insert(node head, int x, int pos) + { + node temp=new node(x); + if(head==null) + { + temp.next=temp; + return temp; + } + else if(pos==1) + { + temp.next=head.next; + head.next=temp; + int t=head.data; + head.data=temp.data; + temp.data=t; + return head; + } + else + { + node curr=head; + int c=2; + boolean p=true; + while(curr!=head || p) + { + p=false; + if(pos==c) + { + temp.next=curr.next; + curr.next=temp; + break; + } + curr=curr.next;c++; + } + return head; + } + } + static node Delete(node head, int pos) + { + if(head.next==head) + return null; + else{ + node curr=head; + int c=2; + while(curr.next!=head) + { + if(pos==c) + { + curr.next=curr.next.next; + break; + } + c++; + curr=curr.next; + } + if(pos==1) + { + curr.next=curr.next.next; + return curr.next; + } + else return head; + } + } + static node reverse(node head) + { + node temp1=head.next, prev=null; + while(temp1!=null) + { + head.next=prev; + prev=head; + head=temp1; + temp1=temp1.next; + printList(head); + } + System.out.println(prev.data); + head.next=prev; + return head; + } + static void printList(node head) + { + node curr=head; + while(curr.next!=head) + { + System.out.print(curr.data+" "); + curr=curr.next; + } + System.out.println(curr.data); + + } +public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + node head=null; + System.out.println("If you want to delete elements, enter 'D'.\nIf you want to enter elements, enter 'E'"); +System.out.println("Since this is your first input, 'E' is selected by default"); +char ch='E'; +int x,pos=1,c=0; +do{ + if(ch=='E') + { + do{ + if(c==0) + { + System.out.println("Please enter an element. Also, if you want to stop inserting, press '-1' as input;"); + x=sc.nextInt(); + head=Insert(head,x, pos); + c++; + printList(head); + } + else + { + System.out.println(); + System.out.println("Enter element."); + x=sc.nextInt(); + if(x==-1) + break; + System.out.println("Enter position"); + pos=sc.nextInt(); + if(pos>c +1 || pos<1) + System.out.println("The position you entered is INCORRECT. Please re-enter."); + c++; + Insert(head, x, pos); + printList(head); + } + }while(x!=-1); + System.out.println("Do you wish to delete some elements. If yes, press 'D'. To exit, press 'F'"); + ch=sc.next().charAt(0); +} +if(ch=='D') +{ + do{ + System.out.println("Enter the position from where you want to delete element"); + pos=sc.nextInt(); + // if(pos==-1) + // break; + if(pos>c || pos<0) + {if(pos!=-1) + System.out.println("The position you entered is incorrect. Please re-enter");} + else + { + head=Delete(head, pos); + c--; + if(head!=null) + printList(head); + } + if(c==0) + { + System.out.println("All the elements have been deleted. If you like to add some, press 'E' or if you like to exit, press 'F'."); + ch=sc.next().charAt(0); + break; + } + }while(pos!=-1); + System.out.println("If you wish to enter elements, press 'E' else press 'F' to exit."); + ch=sc.next().charAt(0); +} + +}while(ch!='F'); + head= reverse(head); + printList(head); +} +} + + diff --git a/java_programs/Linked List/Delete head of doubly LL.java b/java_programs/Linked List/Delete head of doubly LL.java new file mode 100644 index 0000000..fe89b8a --- /dev/null +++ b/java_programs/Linked List/Delete head of doubly LL.java @@ -0,0 +1,71 @@ +import java.util.Scanner; +public class _20 { + static class node{ + int data; + node next,prev; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node Delete(node head, int pos) + { + node temp=null; + if(pos == 1) + { + temp=head.next; + temp.prev=null; + head.next=null; + return temp; + } + int c=1; + node curr=head; + boolean y=false; + while(curr.next!=null) + { + if(pos==c) + { + curr.prev.next=curr.next; + curr.next.prev=curr.prev; + y=true; + } + c++; + curr=curr.next; + } + if(y==false) + { + curr.prev.next=null; + curr.prev=null; + } + return head; + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + node pb=new node(100); + node head=new node(10); + node temp1=new node(20); + node temp2=new node(30); + node temp3=new node(40); + head.next=temp1; + temp1.prev=head; + temp1.next=temp2; + temp2.prev=temp1; + temp2.next=temp3; + temp3.prev=temp2; + printList(head); + System.out.println("Enter position of element you want to delete"); + int pos=sc.nextInt(); + head=Delete(head, pos); + printList(head); + } + } + + diff --git a/java_programs/Linked List/Delete last node of singly LL.java b/java_programs/Linked List/Delete last node of singly LL.java new file mode 100644 index 0000000..c1ea4b2 --- /dev/null +++ b/java_programs/Linked List/Delete last node of singly LL.java @@ -0,0 +1,37 @@ +public class _11 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void PrintList(node head) + { + while(head!=null) + { + System.out.println(head.data); + head=head.next; + } + } + static node DeleteLast(node head) + { + if(head==null) + System.exit(0); + node curr=head; + while(curr.next.next!=null) + curr=curr.next; + curr.next=null; + return head; + } + public static void main(String[] args) { + node head=new node(0); + head.next=new node(10); + head.next.next=new node(20); + head.next.next.next=new node(30); + PrintList(head); + head=DeleteLast(head); + PrintList(head); + } +} diff --git a/java_programs/Linked List/Find n-th node from end in LL.java b/java_programs/Linked List/Find n-th node from end in LL.java new file mode 100644 index 0000000..b8c762b --- /dev/null +++ b/java_programs/Linked List/Find n-th node from end in LL.java @@ -0,0 +1,63 @@ +import java.util.*; +public class _34 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + Scanner sc=new Scanner(System.in); + + node head=null; + System.out.println("Enter first element"); + int x=sc.nextInt(); + head=new node(x); + node curr=head; + int c=1; + System.out.println("If you do not wish to enter element, enter -1"); + do{ + System.out.println("\nEnter element"); + x=sc.nextInt(); + if(x==-1) + break; + c++; + node temp=new node(x); + curr.next=temp; + curr=curr.next; + printList(head); + }while(true); + System.out.println("Enter the position where you want to delete element from end"); + int pos=sc.nextInt(); + if(c-pos+1<=0) + System.out.println("The position you entered is incorrect"); + /* METHOD-1 + else + { + for(int i=0;im+1 || pos==0) + { + System.out.println("\nThe position you entered is not correct. Please re-enter."); + i--; + } + else + { + head=Insert(head, x, pos); + m=m+1; + } + PrintList(head); + } + } + } +} diff --git a/java_programs/Linked List/Middle of LL.java b/java_programs/Linked List/Middle of LL.java new file mode 100644 index 0000000..8c92ac8 --- /dev/null +++ b/java_programs/Linked List/Middle of LL.java @@ -0,0 +1,101 @@ +import java.util.*; +public class _33 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static int middle(node head) + { + node slow=head; + node fast=head; + if(head==null) + return -1; + // else if(head.next==null) + // return head.next.data; + else + { + while(fast!=null && fast.next!=null) + { + if(fast.next.next==null) + { + slow=slow.next; + break; + } + fast=fast.next.next; + slow=slow.next; + } + return slow.data; + } + } + static node InsertSort(node head, int x) + { + node temp=new node(x); + node curr=head; + if(head==null) + return temp; + // else if(head.next==null) + // { + // if(head.data<=x) + // { + // head.next=temp; + // return curr; + // } + // else + // { + // temp.next=head; + // return temp; + // } + // } + else + { + do{ + if(head.data>=x) + { + temp.next=head; + return temp; + } + if(head.next==null && head.data<=x) + { + head.next=temp; + return curr; + } + if(head.next.data>=x) + { + temp.next=head.next; + head.next=temp; + return curr; + } + + head=head.next; + }while(head!=null); + } + return curr; + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + System.out.println("how many elements you want ot print"); + Scanner sc=new Scanner(System.in); + node head=null; + int n=sc.nextInt(); + for(int i=0;im+1 || pos==0) + { + System.out.println("\nThe position you entered is not correct. Please re-enter."); + i--; + } + else + { + head=Insert(head, x, pos); + m=m+1; + } + PrintList(head); + } + } + System.out.println("If you want to search element, press 1."); + int t=sc.nextInt(); + if(t==1) + Search(head); + System.out.println(turr.data); + System.out.println("If you want to delete element, press 2."); + t=sc.nextInt(); + + if(t==2) + { + System.out.println("Enter the position where you want to delete the element."); + int pos=sc.nextInt(); + if(pos>m || pos<=0) + System.out.println("The position you entered is incorrect."); + else + { + + do{ + turr=Delete(turr, pos); + PrintList(turr); + m--; + System.out.println("Enter the position where you want to delete the element. To exit, press -1"); + pos=sc.nextInt(); + }while(m>1 && pos!=-1); + } } + } +} diff --git a/java_programs/Linked List/Sort insert in LL.java b/java_programs/Linked List/Sort insert in LL.java new file mode 100644 index 0000000..92714e5 --- /dev/null +++ b/java_programs/Linked List/Sort insert in LL.java @@ -0,0 +1,76 @@ +import java.util.*; +public class _32 { + static class node{ + int data; + node next; + node(int x) + { + data=x; + } + } + static node InsertSort(node head, int x) + { + node temp=new node(x); + node curr=head; + if(head==null) + return temp; + // else if(head.next==null) + // { + // if(head.data<=x) + // { + // head.next=temp; + // return curr; + // } + // else + // { + // temp.next=head; + // return temp; + // } + // } + else + { + do{ + if(head.data>=x) + { + temp.next=head; + return temp; + } + if(head.next==null && head.data<=x) + { + head.next=temp; + return curr; + } + if(head.next.data>=x) + { + temp.next=head.next; + head.next=temp; + return curr; + } + + head=head.next; + }while(head!=null); + } + return curr; + } + static void printList(node head) + { + while(head!=null) + { + System.out.print(head.data+" "); + head=head.next; + } + } + public static void main(String[] args) { + System.out.println("how many elements you want ot print"); + Scanner sc=new Scanner(System.in); + node head=null; + int n=sc.nextInt(); + for(int i=0;i