Skip to content

Commit 9b2647a

Browse files
Add a separate implementation of linked list
1 parent c020230 commit 9b2647a

File tree

2 files changed

+12
-14
lines changed

2 files changed

+12
-14
lines changed

LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedList.java

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ public int get(int index) {
2525

2626
SinglyNode<Integer> current = head.next;
2727

28-
while (index >= 0) {
28+
while (index > 0) {
2929
current = current.next;
3030
index--;
3131
}
@@ -52,7 +52,7 @@ public void deleteAtIndex(int index) {
5252

5353
SinglyNode<Integer> current = head;
5454

55-
while (index >= 0) {
55+
while (index > 0) {
5656
current = current.next;
5757
index--;
5858
}
@@ -88,7 +88,8 @@ public void printList() {
8888
if (size == 0) {
8989
System.out.println("Empty Linked List");
9090
} else {
91-
SinglyNode<Integer> tempNode = head;
91+
System.out.println("Printing Linked List : ");
92+
SinglyNode<Integer> tempNode = head.next;
9293

9394
while (tempNode != null) {
9495
System.out.println(tempNode.value);

LinkedList/LinkedListImplementation/src/linkedlistimplementation/LinkedListImplementation.java

+8-11
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,8 @@
33
* To change this template file, choose Tools | Templates
44
* and open the template in the editor.
55
*/
6-
76
package linkedlistimplementation;
87

9-
108
/**
119
*
1210
* @author mac
@@ -18,8 +16,8 @@ public class LinkedListImplementation {
1816
*/
1917
public static void main(String[] args) {
2018
// TODO code application logic here
21-
22-
/*SinglyLinkedList<Integer> myLinkedList = new SinglyLinkedList<Integer>();
19+
20+
/*SinglyLinkedList<Integer> myLinkedList = new SinglyLinkedList<Integer>();
2321
myLinkedList.add(2);
2422
myLinkedList.add(4);
2523
myLinkedList.add(5);
@@ -74,17 +72,16 @@ public static void main(String[] args) {
7472
myLinkedList.removeAt(myLinkedList.size() -1);
7573
System.out.println("After removing value from last");
7674
myLinkedList.printList();*/
77-
78-
LinkedList myLinkedList = new LinkedList();
79-
myLinkedList.addAtHead(1);
75+
LinkedList myLinkedList = new LinkedList();
76+
myLinkedList.addAtHead(1);
8077
myLinkedList.addAtTail(3);
8178
myLinkedList.addAtIndex(1, 2); // linked list becomes 1->2->3
8279
myLinkedList.get(1); // return 2
83-
System.out.println(myLinkedList.get(1));;
80+
System.out.println(myLinkedList.get(1));;
8481
myLinkedList.deleteAtIndex(1); // now the linked list is 1->3
8582
myLinkedList.printList();
86-
System.out.println(myLinkedList.get(1));;
87-
83+
System.out.println(myLinkedList.get(1));;
84+
8885
}
89-
86+
9087
}

0 commit comments

Comments
 (0)