sync
This commit is contained in:
14
Semester 3/Exams/ProgrammingExam2_CalebFontenot/pom.xml
Normal file
14
Semester 3/Exams/ProgrammingExam2_CalebFontenot/pom.xml
Normal file
@@ -0,0 +1,14 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<groupId>com.calebfontenot</groupId>
|
||||
<artifactId>ProgrammingExam2_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>20</maven.compiler.source>
|
||||
<maven.compiler.target>20</maven.compiler.target>
|
||||
<exec.mainClass>com.calebfontenot.programmingexam2_calebfontenot.ProgrammingExam2_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@@ -0,0 +1,144 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
*/
|
||||
|
||||
package com.calebfontenot.programmingexam2_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author caleb
|
||||
*/
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
* @param <T>
|
||||
*/
|
||||
public class ProgrammingExam2_CalebFontenot<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_CalebFontenot<Integer> list = new ProgrammingExam2_CalebFontenot<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