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

Adding LinkedList #13

Closed
wants to merge 1 commit into from
Closed
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
160 changes: 160 additions & 0 deletions Data Structures/LinkedList/LinkedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
package LinkedList;

public class LinkedList {

private Node head;

private int size;

private Node tail;

public void add(int number) {
if(this.size==0) {
Node node = new Node();
node.setNumber(number);
node.setNext(null);
this.head=node;
this.tail=this.head;
this.size++;
return;
}
Node node = new Node();
node.setNumber(number);
node.setNext(null);
this.tail.setNext(node);
this.tail=node;
this.size++;
}


public void addFirst(int number) {
this.addFirst(this.head,number);
}

public void addFirst(Node head, int number) {
Node node = new Node();
node.setNumber(number);
node.setNext(head);
this.head=node;
}



public void display() {
this.display(this.head);
}


public void display(Node node) {
while(node.getNext()!=null) {
System.out.print(node.getNumber()+" ");
node=node.getNext();
}

}

public void addAt(int index, int number) throws Exception {
if(this.size<index || index<0 ) {
throw new Exception("Invalid Index");
}
else if(index==0) {
this.addFirst(number);
}
else if(index==this.size) {
this.add(number);
}else {

}

}

public Node getNodeAt(int index) throws Exception {

if(index==0) {
return this.head;
}
else if(index==this.size-1) {
return this.tail;
}
else if(index<0 || index>=this.size) {
throw new Exception("Invalid Arguments");
}

Node temp = this.head;
for(int i=0;i<index;i++) {
temp = temp.getNext();
}
return temp;
}

public void reverse() {
this.reverse(this.head);
}

private void reverse(Node head) {

}

public Node deleteNodeByKey(int key) {
return this.deleteNodeByKey(key,this.head);
}
private Node deleteNodeByKey(int key, Node head) {
if(head.getNumber()==key) {
this.head=null;
this.size--;
}

Node curr = head.getNext();
Node prev = head;
while(curr.getNumber()!=key) {
prev=curr;
curr = curr.getNext();
}
prev.setNext(curr.getNext());
return curr;
}


public Node deleteNodeByIndex(int index) throws Exception {
return this.deleteNodeByIndex(index,this.head);
}

private Node deleteNodeByIndex(int index, Node head) throws Exception {
if(index<0 || index>this.size-1) {
throw new Exception("Invalid Index");
}

if(this.size==1) {
Node rv = head;
this.head = null;
this.size--;
return rv;
}
else if(index==0) {
Node rv = this.head;
this.head=this.head.getNext();
this.size--;
return rv;
}

int i=0;
Node curr = head.getNext();
Node prev = head;
while(i!=index-1) {
prev = curr;
curr = curr.getNext();
i++;
}
prev.setNext(curr.getNext());
this.size--;
return curr;
}







}