Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Linked List basics #258

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 31 additions & 0 deletions java_programs/Linked List/Circular LL Traversal.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
101 changes: 101 additions & 0 deletions java_programs/Linked List/Circular LL.java
Original file line number Diff line number Diff line change
@@ -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;i<n;i++)
{
if(i==0)
{
System.out.println("Enter first element");
int x=sc.nextInt();
head=Insert(head, 1, x);
printList(head);
m++;
}
else
{
System.out.println();
System.out.println("Enter element");
int x=sc.nextInt();
System.out.println("Enter position");
int pos=sc.nextInt();
if(pos>m+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);
}
}
40 changes: 40 additions & 0 deletions java_programs/Linked List/Delete first node of singly LL.java
Original file line number Diff line number Diff line change
@@ -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);
}
}
170 changes: 170 additions & 0 deletions java_programs/Linked List/Delete head of circular LL.java
Original file line number Diff line number Diff line change
@@ -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);
}
}


Loading