0% found this document useful (0 votes)
0 views

Study Guide

The document contains Java code for a singly linked list (SLL) implementation, including methods for reversing the list, appending two lists, zipping two lists, dropping and taking elements, cloning the list, and adding elements at specific indices. It also includes error handling for invalid indices and checks for duplicate values within the list. The code demonstrates various operations that can be performed on a linked list data structure.

Uploaded by

alanhujr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
0 views

Study Guide

The document contains Java code for a singly linked list (SLL) implementation, including methods for reversing the list, appending two lists, zipping two lists, dropping and taking elements, cloning the list, and adding elements at specific indices. It also includes error handling for invalid indices and checks for duplicate values within the list. The code demonstrates various operations that can be performed on a linked list data structure.

Uploaded by

alanhujr
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

void reverse(){

for(int i= 0; i<size; i++){


int rear = size - i - 1; //to trace the linked list from back
if(i < rear){
E temp = getAt(i);
set(i, getAt(rear));
set(rear, temp);
}else
break;
}
}
//create a new list for reverse()
void reverse(){
SLL<E> newList = new SLL<E>();
Node<E> current = head;
int newListIndex = size - 1;
while(current!=null)
{
newList.add(current.data, newListIndex);
newListIndex --;
current = current.next;
}
this = newList;
}
//Exercise 6 - append two lists
SLL<E> append(SLL<E> l2){
Node<E> l2Node = l2.head;
while(l2Node!=null){
this.add(l2Node.data, size);
l2Node = l2Node.next;
}
}
//Exercise 13 -zipL
public SLL<E> zipL(SLL<E> l2){
if(this.isEmpty())
return l2;
if(l2.isEmpty())
return this;
SLL<E> newList = new SLL<E>(this.size+l2.size);
Node<E> l1Node = this.head;
Node<E> l2Node = l2.head;
int newListIndex = 0;
while(l1Node!=null && l2Node!=null){
if(l1Node.data < l2Node.data)
{
newList.add(l1Node.data, newListIndex);
l1Node = l1Node.next;
}else{
newList.add(l2Node.data, newListIndex);
l2Node = l2Node.next;
}
newListIndex++;
}
//This will work because we get out of the loop because at least one of them is null
//If there are remaining elements in one of the list,add them till reaching end of the list
while(l1Node!=null){
newList.add(l1Node.data, newListIndex);
newListIndex++;
l1Node = l1Node.next;
}
while(l2Node!=null){
newList.add(l2Node.data, newListIndex);
newListIndex++;
l2Node = l2Node.next;
}
return newList;
}
public void drop(int count) {
if (count<0) {
throw new IllegalArgumentException("drop: negative count");
}
if (count==0 || head==null) {
return;
}
// count>0 and the list is not empty
int i=0;
while (i<count && head!=null) {
removeFirst();
i++;}}
public void take(int count) {
if (count<0) {
throw new IllegalArgumentException("take: negative argument");
}
if (count==0 || head==null) {
head=null;
size=0;
return;
}
// count>0 and the list is not empty
Node<E> current = head;
int i = 1;
while (current.next!=null && i<count) {
current=current.next;
i++;
}
current.next=null;
size = i;
}
public SingleLinkedList<E> clone3() {
SingleLinkedList<E> result = new SingleLinkedList<>();

Node<E> current = head;


Node<E> newCurrent = new Node<>(null);
Node<E> dummy = newCurrent;

while (current!=null) {
newCurrent.next = new Node<>(current.data);
current=current.next;
newCurrent=newCurrent.next;
}

result.head = dummy.next;
result.size = size;
return result;
}
public void add2(int index, E item) {
if (index < 0) {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
if (index == 0) {
head = new Node(item, head);
size++;
} else {
int i = index;
Node<E> current = head;
while (current != null && --i > 0) {
current = current.next;
}
if (current != null && i == 0) {
current.next = new Node(item, current.next);
size++;
} else {
throw new IndexOutOfBoundsException(Integer.toString(index));
}
}
}
public void add (int index, E item) {
if (index < 0 || index > size) {
throw new
IndexOutOfBoundsException(Integer.toString
(index));
}
if (index == 0) {
addFirst(item);
} else {
Node<E> node = getNode(index-1);
addAfter(node, item);
}
}

private Node<E> getNode(int index) {


Node<E> node = head;
for (int i=0; i<index && node != null; i++) {
node = node.next;
}
return node;
}
public E get (int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString(index)
}
Node<E> node = getNode(index);
return node.data;
}
public E set (int index, E anEntry) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException(Integer.toString
4 }
Node<E> node = getNode(index);
E result = node.data;
node.data = newValue;
return result;
}

Boolean hasDuplicates(){
If (head == null){
Return False}
Node current = head;
While node(current!= null){
Node inner = current.next;
While(inner != null){
if (current.data.equals(inner.data){
Return True
}
Inner = inner.next;
{
Current = current.next;
}
}

You might also like