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

Added SinglyLinkedList.java and CircularLinkedList.java #43

Open
wants to merge 2 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
103 changes: 103 additions & 0 deletions com/williamfiset/datastructures/linkedlist/CircularLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
class CircularLinkedList{
class Node{
int data;
Node next;
public Node(int data){
this.data = data;
}
}
Node last = null;
int length = 0;
public void print(){
Node p;
if(last == null){
System.out.println("List is empty");
return;
}
p = last.next;
do{
System.out.println(p.data);
p = p.next;
}while(p != last.next);
return;
}
public void insert(int data){
Node p;
Node tmp = new Node(data);
if(last == null){
last = tmp;
last.next = tmp;
length++;
return;
}
tmp.next = last.next;
last.next = tmp;
last = tmp;
length++;
return;
}
public void insertStart(int data){
Node tmp = new Node(data);
if(last == null){
last = tmp;
last.next = tmp;
length++;
return;
}
tmp.next = last.next;
last.next = tmp;
length++;
return;
}
public void delete(int data){
Node tmp,p;
if(last == null){
System.out.println("List is empty");
return;
}
if(last.next == last){
if( last.data == data){
last.next = null;
last = null;
length--;
return;
}
else{
System.out.println("Element not found");
return;
}
}
if(last.next.data == data){
last.next = last.next.next;
length--;
return;
}
p = last.next;
while(p.next != last.next){
if(p.next.data == data ){
tmp = p.next;
p.next = tmp.next;
if(p.next == last){
last = p;
}
length--;
return;
}
p = p.next;
}
}
}

public class Main{
public static void main(String args[]){
CircularLinkedList c = new CircularLinkedList();
c.insert(1);
c.insert(2);
c.insert(3);
c.insert(4);
c.insert(5);
c.delete(5);
c.print();

}
}
115 changes: 115 additions & 0 deletions com/williamfiset/datastructures/linkedlist/SinglyLinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
class Node{
int data;
Node next;
}
class LinkedList{
Node head;
public void insert(int data){
Node p = new Node();
p.data = data;
p.next = null;
if(head == null){
head = p;
}
else{
Node n = head;
while(n.next != null){
n = n.next;
}
n.next = p;
}
}

public void print(){
Node p = head;
if(head == null){
System.out.println("List is empty");
}
while(p!= null){
System.out.println(p.data+" ");
p = p.next;
}
}
public void insertStart(int data){
Node p = new Node();
p.data= data;
p.next = head;
head = p;

}
public void insertPos(int pos,int data){
Node p = new Node();
Node tmp = new Node();
int count = 0;
p = head;
if(pos == 0){
tmp.data = data;
tmp.next = head;
head = tmp;
return;
}
while(p!= null){
if(count == pos-1){
tmp.data = data;
tmp.next = p.next;
p.next = tmp;
return;
}
p = p.next;
count++;
}
}
public void delete(int data){
Node p = new Node();
Node tmp = new Node();
p = head;
if(head == null){
System.out.println("List is empty");
return;
}
if(head.data == data){
head = head.next;
return;
}
while(p.next!= null){
if(p.next.data == data){
tmp = p.next;
p.next = tmp.next;
return;
}
p = p.next;
}
System.out.println("Element not found");
return;
}
public void reverse(){
Node p = new Node();
Node n = new Node();
Node curr = new Node();
curr = head;
p = null;
while(curr != null){
n = curr.next;
curr.next = p;
p = curr;
curr = n;
}
head = p;
return;
}

}


public class Main{
public static void main(String args[]){
LinkedList linkedlist = new LinkedList();
linkedlist.insert(1);
linkedlist.insert(2);
linkedlist.insert(3);
linkedlist.insert(4);
linkedlist.insert(5);
linkedlist.reverse();
linkedlist.print();
}
}