toString why you be so stubborn

master
Caleb Fontenot 2023-11-06 13:26:35 +07:00
parent 643bdc658f
commit 9cc7e1d0df
4 changed files with 80 additions and 3 deletions

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package linkedlist;
/**
*
* @author caleb
*/
public class EmptyListException extends RuntimeException{
public EmptyListException() {}
public EmptyListException(String msg) {
super(msg);
}
}

@ -103,10 +103,13 @@ public class MyList<E> {
}
public E removeAt(int index) {
if (size() == 0) {
throw new EmptyListException("The list is empty.");
}
if (index < 0 || index >= size())
throw new IndexOutOfBoundsException();
if (size() == 0)
throw new RuntimeException("Empty List");
throw new EmptyListException("Empty List");
E removedE = null;
if (index == 0) {
// remove the front

@ -18,6 +18,44 @@ public class DoubleLinkedList<T> {
Node<T> right;
}
public T removeAt(int pos)
{
if (pos < 0 || pos > size()-1)
{
throw new IndexOutOfBoundsException();
}
//list is empty
if ( size() == 0 )
throw new EmptyListException( "The list empty.");
//remove at 0
if ( pos == 0)
{
if ( size() == 1)
head = tail = null;
else
head = head.right;
}
else if (pos== size() - 1) { //remove at end
tail = tail.left;
tail.right = null;
}
else { //remove in the middle
Node<T> p = head;
for (int i = 0; i < pos; ++i) {
p = p.right;
p.left.right = p.right;
p.right.left = p.right;
p.right.left = p.left;
}
}
return null;
}
public void addAt(T t, int pos) {
if (pos < 0 || pos > size()) {
throw new IndexOutOfBoundsException();
@ -60,15 +98,17 @@ public class DoubleLinkedList<T> {
String outputString = "DoubleLinkedList{" + "head=" + head + ", tail=" + tail +", size=" + this.size() + "}\n Contents= [";
Node<T> p = head;
int count = 0;
int size = size();
System.out.println(size);
while (p != null) {
outputString += p.t.toString();
if (count -1 < size()) {
count++;
if (count < size) {
outputString += ", ";
} else {
outputString += "]";
}
p = p.right;
count++;
}
return outputString;
}
@ -82,6 +122,8 @@ public class DoubleLinkedList<T> {
list.addAt(40, 0);
list.addAt(40, 3);
list.addAt(25, 3);
list.removeAt(3);
list.removeAt(1);
System.out.println(list);
}
}

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit this template
*/
package edu.slcc.asdv.caleb.mp6_calebfontenot;
/**
*
* @author caleb
*/
public class EmptyListException extends RuntimeException{
public EmptyListException() {}
public EmptyListException(String msg) {
super(msg);
}
}