Skip to content

Update Linked List classes #33

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

Open
wants to merge 4 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

This file was deleted.

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package com.codefortomorrow.advanced.chapter16.practice;

/**
* @author ArmeetJatyani
* March 2021
*
* Implement a simple LinkedList
* You will need to create a LinkedListNode class, which represents each item/node in the linked list
* Assume that the elements in the linked list are all of type int
* Required functionality...
* - head(): get head node of list
* - tail(): get tail node of list
* - add(): add element to the end of the list
* - insert(): insert an element at a given index position
* - push(): add element to the beginning of the list
* - pop(): remove head of list and return the node that was removed
* - clear(): remove all elements in the list
* - toString(): return a String representation of the list
*
* Create a driver class called TestLinkedList. In the main method, create an instance
* of LinkedList and test all of its methods at least once. (Print the list as needed
* to prove the methods worked.)
*/

public class TestSinglyLinkedList {

public static void main(String[] args) {
// write your code here

}
}

class Node {
// write your code here
}

class LinkedListNode extends Node {
// write your code here
}

class SinglyLinkedList {
// write your code here
}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -8,30 +8,79 @@
* You will need to create a LinkedListNode class, which represents each item/node in the linked list
* Assume that the elements in the linked list are all of type int
* Required functionality...
* - head(): get head of list
* - tail(): get tail of list
* - add(): add to tail of list
* - push(): push to head of list
* - pop(): remove head of list
* - head(): get head node of list
* - tail(): get tail node of list
* - add(): add element to the end of the list
* - insert(): insert an element at a given index position
* - push(): add element to the beginning of the list
* - pop(): remove head of list and return the node that was removed
* - clear(): remove all elements in the list
* - toString(): return a String representation of the list
*
* Create a driver class called TestLinkedList. In the main method, create an instance
* of LinkedList and test all of its methods at least once. (Print the list as needed
* to prove the methods worked.)
*/

public class LinkedList {
public class TestSinglyLinkedList {
public static void main(String[] args) {
// test default constructor
SinglyLinkedList list = new SinglyLinkedList();
System.out.println("Empty linked list: " + list);

// test 1 arg constructor
list = new SinglyLinkedList(10);
System.out.println("Linked list with 1 element: " + list);

// test add method
list.add(5);
list.add(7);
list.add(12);
list.add(2);
System.out.println("Test add(): " + list);

// test insert method
list.insert(13, 0);
System.out.println("Test insert(): " + list);
list.insert(1, 6);
System.out.println("Test insert(): " + list);
list.insert(4, 3);
System.out.println("Test insert(): " + list);

// test head and tail methods
System.out.println("Head: " + list.head().value());
System.out.println("Tail: " + list.tail().value());

// test push
list.push(21);
System.out.println("Test push(): " + list);

// test pop
System.out.println("Popped element: " + list.pop().value());
System.out.println("Test pop(): " + list);

// test clear
list.clear();
System.out.println("Test clear(): " + list);
}
}

class SinglyLinkedList {

private LinkedListNode head;

/**
* default constructor
*/
public LinkedList() {
public SinglyLinkedList() {
head = null;
}

/**
* constructor with first value
* @param value first element in the linked list
*/
public LinkedList(int value) {
public SinglyLinkedList(int value) {
// create first node
head = new LinkedListNode(value, null);
}
Expand Down Expand Up @@ -73,15 +122,50 @@ public void add(int value) {
}

/**
* push (add element to head of linkedlist)
* Inserts a value at a given position in the linked list
* @param value value to insert
* @param index where to insert the value
*/
public void insert(int value, int index) {
// Verify valid index number
if (index < 0) System.out.print("Invalid position");

// Create new node and keep track of current node
LinkedListNode new_node = new LinkedListNode(value, null);
LinkedListNode current = head;

// If index is 0, set new node as head
if (index == 0) {
new_node.setNext(head);
head = new_node;
} else {
while (index-- >= 0) {
if (index == 0) {
// Add new node after current position
new_node.setNext(current.next());
current.setNext(new_node);
break;
}
// Check if index is out of bounds (reaches end)
if (current.next() == null) {
System.out.println("Out of bounds");
break;
}
current = current.next();
}
}
}

/**
* Adds element to head of the linked list
*/
public void push(int value) {
LinkedListNode newHead = new LinkedListNode(value, head);
head = newHead;
}

/**
* pop (remove and return head of linkedlist)
* Removes and returns the head of the linked list
* @return the node that was removed
*/
public LinkedListNode pop() {
Expand All @@ -90,12 +174,23 @@ public LinkedListNode pop() {
return popped;
}

/**
* Removes all elements in the list
*/
public void clear() {
head = null;
}

/**
* Returns a String version of the LinkedList
* @return a String version of the LinkedList
*/
@Override
public String toString() {
if (head == null) {
return "[]";
}

String list = "[";
LinkedListNode current = head;
if (current == null) return null;
Expand Down