0% found this document useful (0 votes)
22 views4 pages

EXERCISE LL Insert

Uploaded by

Feben Getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
22 views4 pages

EXERCISE LL Insert

Uploaded by

Feben Getachew
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd

// THIS GOES IN YOUR MAIN CLASS TO TEST YOUR CODE:

// -----------------------------------------------

package datastructures.linkedlist;

public class Main {

public static void main(String[] args) {

LinkedList myLinkedList = new LinkedList(1);


myLinkedList.append(3);

System.out.println("LL before insert():");


myLinkedList.printList();

myLinkedList.insert(1, 2);

System.out.println("\nLL after insert(2) in middle:");


myLinkedList.printList();

myLinkedList.insert(0, 0);

System.out.println("\nLL after insert(0) at beginning:");


myLinkedList.printList();

myLinkedList.insert(4, 4);

System.out.println("\nLL after insert(4) at end:");


myLinkedList.printList();

/*
EXPECTED OUTPUT:
----------------
LL before insert():
1
3

LL after insert(2) in middle:


1
2
3

LL after insert(0) at beginning:


0
1
2
3

LL after insert(4) at end:


0
1
2
3
4

*/
}

// THIS CODE GOES IN YOUR LINKEDLIST CLASS:


// ----------------------------------------

package datastructures.linkedlist;

public class LinkedList {

private Node head;


private Node tail;
private int length;

class Node {
int value;
Node next;

Node(int value) {
this.value = value;
}
}

public LinkedList(int value) {


Node newNode = new Node(value);
head = newNode;
tail = newNode;
length = 1;
}

public void printList() {


Node temp = head;
while (temp != null) {
System.out.println(temp.value);
temp = temp.next;
}
}

public void getHead() {


if (head == null) {
System.out.println("Head: null");
} else {
System.out.println("Head: " + head.value);
}
}

public void getTail() {


if (head == null) {
System.out.println("Tail: null");
} else {
System.out.println("Tail: " + tail.value);
}
}
public void getLength() {
System.out.println("Length: " + length);
}

public void append(int value) {


Node newNode = new Node(value);
if (length == 0) {
head = newNode;
tail = newNode;
} else {
tail.next = newNode;
tail = newNode;
}
length++;
}

public Node removeLast() {


if (length == 0) return null;
Node temp = head;
Node pre = head;
while(temp.next != null) {
pre = temp;
temp = temp.next;
}
tail = pre;
tail.next = null;
length--;
if (length == 0) {
head = null;
tail = null;
}
return temp;
}

public void prepend(int value) {


Node newNode = new Node(value);
if (length == 0) {
head = newNode;
tail = newNode;
} else {
newNode.next = head;
head = newNode;
}
length++;
}

public Node removeFirst() {


if (length == 0) return null;
Node temp = head;
head = head.next;
temp.next = null;
length--;
if (length == 0) {
tail = null;
}
return temp;
}

public Node get(int index) {


if (index < 0 || index >= length) return null;
Node temp = head;
for(int i = 0; i < index; i++) {
temp = temp.next;
}
return temp;
}

public boolean set(int index, int value) {


Node temp = get(index);
if (temp != null) {
temp.value = value;
return true;
}
return false;
}

// WRITE INSERT METHOD HERE //


// //
// //
// //
// //
//////////////////////////////

You might also like