Reset author name to chosen name ✨
This commit is contained in:
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.chloefontenot.programmingexam2_chloefontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author chloe
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
* @param <T>
|
||||
*/
|
||||
public class ProgrammingExam2_ChloeFontenot<T> {
|
||||
|
||||
Node<T> head;
|
||||
Node<T> tail;
|
||||
|
||||
class Node<T> {
|
||||
|
||||
T t;
|
||||
Node<T> l;
|
||||
Node<T> r;
|
||||
}
|
||||
|
||||
public T removeAt(int pos) {
|
||||
if (pos < 0 || pos > size() - 1) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
|
||||
//list is empty
|
||||
if (size() == 0) {
|
||||
throw new RuntimeException("The list empty.");
|
||||
}
|
||||
T returnT = null;
|
||||
|
||||
if (pos == 0)//remove at 0
|
||||
{
|
||||
Node<T> pointer = head.r;
|
||||
returnT = (T) head.t;
|
||||
pointer.l = null;
|
||||
head = pointer;
|
||||
} else if (pos == (size() - 1))//remove at end
|
||||
{
|
||||
Node<T> pointer = tail.l.l;
|
||||
returnT = (T) tail.t;
|
||||
pointer.r = null;
|
||||
tail = pointer;
|
||||
} else//remove in the middle
|
||||
{
|
||||
// Iterate to the element position
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < pos - 1; ++i) {
|
||||
pointer = pointer.r;
|
||||
}
|
||||
returnT = (T) pointer.r.t;
|
||||
pointer.r = pointer.r.r;
|
||||
pointer.l = pointer.r;
|
||||
|
||||
|
||||
}
|
||||
|
||||
return returnT;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
}
|
||||
|
||||
public void addAt(T t, int pos) {
|
||||
if (pos < 0 || pos > size()) {
|
||||
throw new IndexOutOfBoundsException();
|
||||
}
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.t = t;
|
||||
if (head == null)//list is empty
|
||||
{
|
||||
head = tail = newNode;
|
||||
} else if (pos == 0)//add at the front
|
||||
{
|
||||
newNode.r = head;
|
||||
head.l = newNode;
|
||||
head = newNode;
|
||||
} else if (pos == size())//add at the end
|
||||
{
|
||||
newNode.l = tail;
|
||||
tail.r = newNode;
|
||||
tail = newNode;
|
||||
} else//middle
|
||||
{
|
||||
Node<T> p = head;
|
||||
for (int i = 0; i < pos - 1; ++i) {
|
||||
p = p.r;
|
||||
}
|
||||
newNode.l = p;
|
||||
newNode.r = p.r;
|
||||
p.r.l = newNode;
|
||||
p.r = newNode;
|
||||
}
|
||||
}
|
||||
|
||||
public int size() {
|
||||
Node<T> p = head;
|
||||
int count = 0;
|
||||
while (p != null) {
|
||||
count++;
|
||||
p = p.r;
|
||||
}
|
||||
return count;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
String s = "";
|
||||
Node<T> p = head;
|
||||
while (p != null) {
|
||||
s += p.t.toString() + " ";
|
||||
p = p.r;
|
||||
}
|
||||
return s;
|
||||
}
|
||||
|
||||
public static void main(String[] args) {
|
||||
ProgrammingExam2_ChloeFontenot<Integer> list = new ProgrammingExam2_ChloeFontenot<Integer>();
|
||||
|
||||
list.addAt(20, 0);
|
||||
list.addAt(10, 0);
|
||||
list.addAt(40, 2);
|
||||
list.addAt(30, 2);
|
||||
list.addAt(50, 4);
|
||||
System.out.println(list);
|
||||
System.out.println(list.removeAt(0));
|
||||
System.out.println(list);
|
||||
System.out.println(list.removeAt(2));
|
||||
System.out.println(list);
|
||||
|
||||
System.out.println(list.removeAt(2));
|
||||
System.out.println(list.toString());
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user