166 lines
8.7 KiB
HTML
166 lines
8.7 KiB
HTML
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
|
<html>
|
|
<head>
|
|
<title>ProgramingExam2_CalebFontenot.java</title>
|
|
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
|
|
<style type="text/css">
|
|
<!--
|
|
body {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
pre {color: #000000; background-color: #ffffff; font-family: monospace}
|
|
table {color: #404040; background-color: #e9e8e2; font-family: monospace}
|
|
.ST1 {color: #969696; font-family: monospace; font-weight: bold}
|
|
.ST2 {font-family: monospace; font-weight: bold}
|
|
.comment {color: #969696}
|
|
.ST4 {font-family: monospace; font-weight: bold; font-style: italic}
|
|
.ST5 {color: #ce54b8; font-family: monospace; font-style: italic}
|
|
.ST0 {color: #287bde}
|
|
.ST3 {color: #ce54b8}
|
|
.string {color: #1e9347}
|
|
.literal {color: #336bdd}
|
|
-->
|
|
</style>
|
|
</head>
|
|
<body>
|
|
<table width="100%"><tr><td align="center">C:\Users\ar114\Documents\NetBeansProjects\ProgramingExam2_CalebFontenot\src\main\java\com\mycompany\programingexam2_calebfontenot\ProgramingExam2_CalebFontenot.java</td></tr></table>
|
|
<pre>
|
|
<span class="comment">/*</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt</span><span class="comment"> to change this license</span>
|
|
<span class="comment"> * Click </span><span class="ST0">nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/$</span><span class="comment">{packagePath}/${mainClassName}.java to edit this template</span>
|
|
<span class="comment"> */</span>
|
|
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
|
|
|
|
<span class="comment">/**</span>
|
|
<span class="comment"> *</span>
|
|
<span class="comment"> * </span><span class="ST1">@author</span> <span class="comment">ar114</span>
|
|
<span class="comment">*/</span>
|
|
<span class="literal">public</span> <span class="literal">class</span> <span class="ST2">ProgramingExam2_CalebFontenot</span><T> {
|
|
|
|
Node<T> <span class="ST3">head</span>;
|
|
Node<T> <span class="ST3">tail</span>;
|
|
|
|
<span class="literal">class</span> <span class="ST2">Node</span><T> {
|
|
|
|
T <span class="ST3">t</span>;
|
|
Node<T> <span class="ST3">l</span>;
|
|
Node<T> <span class="ST3">r</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> T <span class="ST2">removeAt</span>(<span class="literal">int</span> pos) {
|
|
<span class="literal">if</span> (pos < 0 || pos > size() - 1) {
|
|
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
|
|
}
|
|
|
|
<span class="comment">//list is empty</span>
|
|
<span class="literal">if</span> (size() == 0) {
|
|
<span class="literal">throw</span> <span class="literal">new</span> RuntimeException(<span class="string">"</span><span class="string">The list empty.</span><span class="string">"</span>);
|
|
}
|
|
T returnT = <span class="literal">null</span>;
|
|
|
|
<span class="literal">if</span> (pos == 0)<span class="comment">//remove at 0</span>
|
|
{
|
|
Node<T> pointer = <span class="ST3">head</span>.<span class="ST3">r</span>;
|
|
returnT = (T) <span class="ST3">head</span>.<span class="ST3">t</span>;
|
|
pointer.<span class="ST3">l</span> = <span class="literal">null</span>;
|
|
<span class="ST3">head</span> = pointer;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (pos == (size() - 1))<span class="comment">//remove at end</span>
|
|
{
|
|
Node<T> pointer = <span class="ST3">tail</span>.<span class="ST3">l</span>.<span class="ST3">l</span>;
|
|
returnT = (T) <span class="ST3">tail</span>.<span class="ST3">t</span>;
|
|
pointer.<span class="ST3">r</span> = <span class="literal">null</span>;
|
|
<span class="ST3">tail</span> = pointer;
|
|
} <span class="literal">else</span><span class="comment">//remove in the middle</span>
|
|
{
|
|
<span class="comment">// Iterate to the element position</span>
|
|
Node<T> pointer = <span class="ST3">head</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < pos - 1; ++i) {
|
|
pointer = pointer.<span class="ST3">r</span>;
|
|
}
|
|
pointer.<span class="ST3">r</span> = pointer.<span class="ST3">r</span>.<span class="ST3">r</span>;
|
|
pointer.<span class="ST3">l</span> = pointer.<span class="ST3">r</span>;
|
|
returnT = (T) pointer.<span class="ST3">t</span>;
|
|
|
|
}
|
|
|
|
<span class="literal">return</span> returnT;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">clear</span>() {
|
|
<span class="ST3">head</span> = <span class="ST3">tail</span> = <span class="literal">null</span>;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">void</span> <span class="ST2">addAt</span>(T t, <span class="literal">int</span> pos) {
|
|
<span class="literal">if</span> (pos < 0 || pos > size()) {
|
|
<span class="literal">throw</span> <span class="literal">new</span> IndexOutOfBoundsException();
|
|
}
|
|
Node<T> newNode = <span class="literal">new</span> Node<T>();
|
|
newNode.<span class="ST3">t</span> = t;
|
|
<span class="literal">if</span> (<span class="ST3">head</span> == <span class="literal">null</span>)<span class="comment">//list is empty</span>
|
|
{
|
|
<span class="ST3">head</span> = <span class="ST3">tail</span> = newNode;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (pos == 0)<span class="comment">//add at the front</span>
|
|
{
|
|
newNode.<span class="ST3">r</span> = <span class="ST3">head</span>;
|
|
<span class="ST3">head</span>.<span class="ST3">l</span> = newNode;
|
|
<span class="ST3">head</span> = newNode;
|
|
} <span class="literal">else</span> <span class="literal">if</span> (pos == size())<span class="comment">//add at the end</span>
|
|
{
|
|
newNode.<span class="ST3">l</span> = <span class="ST3">tail</span>;
|
|
<span class="ST3">tail</span>.<span class="ST3">r</span> = newNode;
|
|
<span class="ST3">tail</span> = newNode;
|
|
} <span class="literal">else</span><span class="comment">//middle</span>
|
|
{
|
|
Node<T> p = <span class="ST3">head</span>;
|
|
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < pos - 1; ++i) {
|
|
p = p.<span class="ST3">r</span>;
|
|
}
|
|
newNode.<span class="ST3">l</span> = p;
|
|
newNode.<span class="ST3">r</span> = p.<span class="ST3">r</span>;
|
|
p.<span class="ST3">r</span>.<span class="ST3">l</span> = newNode;
|
|
p.<span class="ST3">r</span> = newNode;
|
|
}
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">size</span>() {
|
|
Node<T> p = <span class="ST3">head</span>;
|
|
<span class="literal">int</span> count = 0;
|
|
<span class="literal">while</span> (p != <span class="literal">null</span>) {
|
|
count++;
|
|
p = p.<span class="ST3">r</span>;
|
|
}
|
|
<span class="literal">return</span> count;
|
|
}
|
|
|
|
@Override
|
|
<span class="literal">public</span> String <span class="ST2">toString</span>() {
|
|
String s = <span class="string">""</span>;
|
|
Node<T> p = <span class="ST3">head</span>;
|
|
<span class="literal">while</span> (p != <span class="literal">null</span>) {
|
|
s += p.<span class="ST3">t</span>.toString() + <span class="string">"</span> <span class="string">"</span>;
|
|
p = p.<span class="ST3">r</span>;
|
|
}
|
|
<span class="literal">return</span> s;
|
|
}
|
|
|
|
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST4">main</span>(String[] args) {
|
|
ProgramingExam2_CalebFontenot<Integer> list = <span class="literal">new</span> ProgramingExam2_CalebFontenot<Integer>();
|
|
|
|
list.addAt(20, 0);
|
|
list.addAt(10, 0);
|
|
list.addAt(40, 2);
|
|
list.addAt(30, 2);
|
|
list.addAt(50, 4);
|
|
System.<span class="ST5">out</span>.println(list);
|
|
System.<span class="ST5">out</span>.println(list.removeAt(0));
|
|
System.<span class="ST5">out</span>.println(list);
|
|
System.<span class="ST5">out</span>.println(list.removeAt(2));
|
|
System.<span class="ST5">out</span>.println(list);
|
|
|
|
System.<span class="ST5">out</span>.println(list.removeAt(2));
|
|
System.<span class="ST5">out</span>.println(list.toString());
|
|
}
|
|
|
|
}
|
|
|
|
</pre></body>
|
|
</html>
|