upload practice exam
This commit is contained in:
parent
5e48abd081
commit
aa46f43ea5
2
Semester 3/Assignments/MP2-chapter4_Java20_CalebFontenot/start.sh
Executable file
2
Semester 3/Assignments/MP2-chapter4_Java20_CalebFontenot/start.sh
Executable file
@ -0,0 +1,2 @@
|
||||
#!/bin/bash
|
||||
java --module-path /usr/lib --add-modules javafx.fxml,javafx.controls,javafx.media -XX:+UnlockExperimentalVMOptions -XX:+EagerJVMCI -XX:+EnableJVMCI -XX:+UseJVMCICompiler -XX:+UseJVMCINativeLibrary -Djava.awt.graphicsenv=net.java.openjdk.cacio.wayland.WaylandGraphicsEnvironment -jar dist/MP2-chapter4_Java20_CalebFontenot.jar
|
155
Semester 3/Exams/Projects/GenericMergeSortExam.java.html
Normal file
155
Semester 3/Exams/Projects/GenericMergeSortExam.java.html
Normal file
@ -0,0 +1,155 @@
|
||||
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
|
||||
<html>
|
||||
<head>
|
||||
<title>GenericMergeSortExam.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}
|
||||
.ST3 {font-family: monospace; font-weight: bold; font-style: italic}
|
||||
.ST4 {font-family: monospace; font-style: italic}
|
||||
.ST6 {color: #ce54b8; font-family: monospace; font-style: italic}
|
||||
.ST0 {color: #287bde}
|
||||
.ST5 {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\GenericMergeSortExam.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/Classes/Class.java</span><span class="comment"> to edit this template</span>
|
||||
<span class="comment"> */</span>
|
||||
<span class="literal">package</span> com.mycompany.programingexam2_calebfontenot;
|
||||
|
||||
<span class="literal">import</span> java.lang.reflect.Array;
|
||||
<span class="literal">import</span> java.util.Comparator;
|
||||
|
||||
<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">GenericMergeSortExam</span> {
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <E <span class="literal">extends</span> Comparable<E>> <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list) {
|
||||
<span class="ST4">mergeSort</span>(list,
|
||||
<span class="literal">new</span> Comparator<E>() {
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(E e1, E e2) {
|
||||
<span class="literal">int</span> compareResult = ((Comparable<E>) e1).compareTo(e2);
|
||||
<span class="literal">return</span> compareResult;
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <E> <span class="literal">void</span> <span class="ST3">mergeSort</span>(E[] list,
|
||||
Comparator<? <span class="literal">super</span> E> comparator) {
|
||||
<span class="literal">if</span> (list.<span class="ST5">length</span> > 1) {
|
||||
<span class="comment">// Merge sort the first half</span>
|
||||
E[] firstHalf = (E[]) <span class="literal">new</span> Object[list.<span class="ST5">length</span> / 2];
|
||||
<span class="comment">//copies 1st half of list into array firstHalf</span>
|
||||
System.<span class="ST4">arraycopy</span>(list, 0, firstHalf, 0, list.<span class="ST5">length</span> / 2);
|
||||
<span class="ST4">mergeSort</span>(firstHalf, comparator);
|
||||
<span class="comment">// Merge sort the second half</span>
|
||||
<span class="literal">int</span> secondHalfLength = list.<span class="ST5">length</span> - list.<span class="ST5">length</span> / 2;
|
||||
E[] secondHalf = (E[]) <span class="literal">new</span> Object[secondHalfLength];
|
||||
System.<span class="ST4">arraycopy</span>(list, list.<span class="ST5">length</span> / 2,
|
||||
secondHalf, 0, secondHalfLength);
|
||||
<span class="ST4">mergeSort</span>(secondHalf, comparator);
|
||||
<span class="comment">// Merge firstHalf with secondHalf</span>
|
||||
E[] temp = <span class="ST4">merge1</span>(firstHalf, secondHalf, comparator);
|
||||
System.<span class="ST4">arraycopy</span>(temp, 0, list, 0, temp.<span class="ST5">length</span>);
|
||||
}
|
||||
}
|
||||
|
||||
<span class="comment">/**</span>
|
||||
<span class="comment"> * </span><span class="ST1">Merges</span> <span class="ST1">the</span> <span class="ST1">two</span> <span class="ST1">lists</span> <span class="ST1">using</span> <span class="ST1">a</span> <span class="ST1">Comparator</span><span class="ST1">.</span>
|
||||
<span class="comment"> *</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> <E> <span class="comment">The</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">the</span> <span class="comment">methods</span> <span class="comment">accepts</span><span class="comment">.</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> list1 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> list2 <span class="comment">a</span> <span class="comment">list</span> <span class="comment">of</span> <span class="comment">generic</span> <span class="comment">type</span> <span class="comment">E</span>
|
||||
<span class="comment"> * </span><span class="ST1">@param</span> comparator <span class="comment">Comparator</span>
|
||||
<span class="comment"> * </span><span class="ST1">@return</span> <span class="comment">a</span> <span class="comment">sorted</span> <span class="comment">new</span> <span class="comment">list</span> <span class="comment">made</span> <span class="comment">of</span> <span class="comment">the</span> <span class="comment">merged</span> <span class="comment">list1</span> <span class="comment">and</span> <span class="comment">list2</span><span class="comment">.</span>
|
||||
<span class="comment">*/</span>
|
||||
<span class="literal">private</span> <span class="literal">static</span> <E> E[]
|
||||
<span class="ST3">merge1</span>(E[] list1, E[] list2, Comparator<? <span class="literal">super</span> E> comparator) {
|
||||
<span class="literal">int</span> returnArraySize = list1.<span class="ST5">length</span> + list2.<span class="ST5">length</span>;
|
||||
E[] returnArray = (E[]) <span class="literal">new</span> Object[returnArraySize];
|
||||
<span class="literal">int</span> list1Iterator = 0, list2Iterator = 0, returnArrayIterator = 0, iterationsRemaining = 0;
|
||||
<span class="literal">for</span> (; returnArrayIterator < returnArraySize; ++returnArrayIterator) {
|
||||
iterationsRemaining = (returnArraySize - returnArrayIterator);
|
||||
<span class="literal">if</span> (comparator.compare(list1[list1Iterator], list2[list2Iterator]) <= 0) {
|
||||
returnArray[returnArrayIterator] = list1[list1Iterator];
|
||||
<span class="literal">if</span> (iterationsRemaining > 1)
|
||||
returnArray[returnArrayIterator + 1] = list2[list2Iterator];
|
||||
<span class="literal">if</span> ((list1.<span class="ST5">length</span> - 1) < (list1Iterator)) {
|
||||
++list1Iterator;
|
||||
} <span class="literal">else</span> {
|
||||
++list2Iterator;
|
||||
}
|
||||
++returnArrayIterator;
|
||||
} <span class="literal">else</span> {
|
||||
returnArray[returnArrayIterator] = list2[list2Iterator];
|
||||
<span class="literal">if</span> (iterationsRemaining > 1)
|
||||
returnArray[returnArrayIterator + 1] = list1[list1Iterator];
|
||||
<span class="literal">if</span> ((list2.<span class="ST5">length</span> - 1) < (list2Iterator)) {
|
||||
++list2Iterator;
|
||||
} <span class="literal">else</span> {
|
||||
++list1Iterator;
|
||||
}
|
||||
++returnArrayIterator;
|
||||
|
||||
}
|
||||
<span class="comment">/*</span>
|
||||
<span class="comment"> if (comparator.compare(list1[list1Iterator], list2[list2Iterator]) == 1) {</span>
|
||||
<span class="comment"> returnArray[returnArrayIterator] = list2[list2Iterator];</span>
|
||||
<span class="comment"> ++list2Iterator;</span>
|
||||
<span class="comment"> ++returnArrayIterator;</span>
|
||||
<span class="comment"> } else {</span>
|
||||
<span class="comment"> returnArray[returnArrayIterator] = list1[list1Iterator];</span>
|
||||
<span class="comment"> ++list1Iterator;</span>
|
||||
<span class="comment"> ++returnArrayIterator;</span>
|
||||
|
||||
<span class="comment"> }</span>
|
||||
<span class="comment"> */</span>
|
||||
}
|
||||
<span class="literal">return</span> returnArray;
|
||||
}
|
||||
|
||||
<span class="literal">public</span> <span class="literal">static</span> <span class="literal">void</span> <span class="ST3">main</span>(String[] args) {
|
||||
Integer[] list
|
||||
= {
|
||||
2, 3, 2, 5, 6, 1, -2, 3, 14, 12
|
||||
};
|
||||
<span class="ST4">mergeSort</span>(list);
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < list.<span class="ST5">length</span>; i++) {
|
||||
System.<span class="ST6">out</span>.print(list[i] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
System.<span class="ST6">out</span>.println();
|
||||
String[] list1
|
||||
= {
|
||||
<span class="string">"</span><span class="string">ABC</span><span class="string">"</span>, <span class="string">"</span><span class="string">abc</span><span class="string">"</span>, <span class="string">"</span><span class="string">abm</span><span class="string">"</span>, <span class="string">"</span><span class="string">Anf</span><span class="string">"</span>, <span class="string">"</span><span class="string">Good</span><span class="string">"</span>, <span class="string">"</span><span class="string">Bad</span><span class="string">"</span>, <span class="string">"</span><span class="string">nice</span><span class="string">"</span>
|
||||
};
|
||||
<span class="ST4">mergeSort</span>(list1, <span class="literal">new</span> Comparator<String>() {
|
||||
@Override
|
||||
<span class="literal">public</span> <span class="literal">int</span> <span class="ST2">compare</span>(String s1, String s2) {
|
||||
<span class="literal">return</span> s1.compareToIgnoreCase(s2);
|
||||
}
|
||||
});
|
||||
<span class="literal">for</span> (<span class="literal">int</span> i = 0; i < list1.<span class="ST5">length</span>; i++) {
|
||||
System.<span class="ST6">out</span>.print(list1[i] + <span class="string">"</span> <span class="string">"</span>);
|
||||
}
|
||||
System.<span class="ST6">out</span>.println(<span class="string">"</span><span class="string">"</span>);
|
||||
}
|
||||
}
|
||||
|
||||
</pre></body>
|
||||
</html>
|
@ -0,0 +1,165 @@
|
||||
<!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>
|
@ -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.mycompany.programmingquiz1_calebfontenot</groupId>
|
||||
<artifactId>ProgrammingQuiz1_CalebFontenot</artifactId>
|
||||
<version>1.0-SNAPSHOT</version>
|
||||
<packaging>jar</packaging>
|
||||
<properties>
|
||||
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||
<maven.compiler.source>11</maven.compiler.source>
|
||||
<maven.compiler.target>11</maven.compiler.target>
|
||||
<exec.mainClass>com.mycompany.programmingquiz1_calebfontenot.ProgrammingQuiz1_CalebFontenot</exec.mainClass>
|
||||
</properties>
|
||||
</project>
|
@ -0,0 +1,101 @@
|
||||
/*
|
||||
* 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 com.mycompany.programmingquiz1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
* @param <T>
|
||||
*/
|
||||
public class DoubleLinkedListExam<T> {
|
||||
|
||||
Node<T> head;
|
||||
Node<T> tail;
|
||||
|
||||
class Node<T> {
|
||||
|
||||
T t;
|
||||
Node<T> l;
|
||||
Node<T> r;
|
||||
}
|
||||
|
||||
public T removeAt(int pos) {
|
||||
return null;
|
||||
}
|
||||
|
||||
public void clear() {
|
||||
head = tail = null;
|
||||
}
|
||||
|
||||
public void addAt(T t, int pos) {
|
||||
System.out.println("Linked List size: " + this.size());
|
||||
if (pos == 0) { // add first element
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.t = t;
|
||||
head = tail = newNode;
|
||||
return;
|
||||
}
|
||||
if (pos == this.size()) { // add at end
|
||||
Node<T> tmp = tail;
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.t = t;
|
||||
tmp.r = newNode;
|
||||
newNode.l = tmp;
|
||||
newNode.r = null;
|
||||
tail = newNode;
|
||||
return;
|
||||
}
|
||||
if (pos < this.size()) { // add in middle
|
||||
Node<T> pointer = head;
|
||||
for (int i = 0; i < this.size() - 1; ++i) {
|
||||
pointer = pointer.r;
|
||||
}
|
||||
Node<T> newNode = new Node<T>();
|
||||
newNode.t = t;
|
||||
newNode.l = pointer;
|
||||
newNode.r = pointer.l;
|
||||
pointer.r = newNode;
|
||||
pointer.r.l = newNode;
|
||||
return;
|
||||
}
|
||||
if (pos < 0 || this.size() < pos) {
|
||||
throw new IndexOutOfBoundsException("Given index is outside the range of acceptible values.");
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
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) {
|
||||
DoubleLinkedListExam<Integer> list = new DoubleLinkedListExam<>();
|
||||
list.addAt(10, 0);
|
||||
System.out.println(list);
|
||||
list.addAt(30, 1);
|
||||
list.addAt(20, 1);
|
||||
list.addAt(5, 0);
|
||||
|
||||
System.out.println(list);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,41 @@
|
||||
/*
|
||||
* 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 com.mycompany.programmingquiz1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class InsertionSort {
|
||||
public static void insertionSort(String[] list)
|
||||
{
|
||||
int i = 1;
|
||||
while (i < list.length)
|
||||
{
|
||||
if (list[i - 1].compareTo(list[i]) >= 0) {
|
||||
String tmp = list[i - 1];
|
||||
list[i - 1] = list[i];
|
||||
list[i] = tmp;
|
||||
}
|
||||
++i;
|
||||
//1. Loop that opens the hole by shifting to the right
|
||||
|
||||
//2. End of loop of shifting ---Insert the current at the opened hole list[j+1]
|
||||
|
||||
}
|
||||
}
|
||||
public static void printArray(String[] array) {
|
||||
for (String current: array) {
|
||||
System.out.print(current + " ");
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
public static void main(String[] args) {
|
||||
String[] array = {"Zedfrey", "Hello", "Glorious", "World"};
|
||||
printArray(array);
|
||||
insertionSort(array);
|
||||
printArray(array);
|
||||
}
|
||||
}
|
@ -0,0 +1,17 @@
|
||||
/*
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
|
||||
* Click nbfs://nbhost/SystemFileSystem/Templates/Project/Maven2/JavaApp/src/main/java/${packagePath}/${mainClassName}.java to edit this template
|
||||
*/
|
||||
|
||||
package com.mycompany.programmingquiz1_calebfontenot;
|
||||
|
||||
/**
|
||||
*
|
||||
* @author ar114
|
||||
*/
|
||||
public class ProgrammingQuiz1_CalebFontenot {
|
||||
|
||||
public static void main(String[] args) {
|
||||
System.out.println("Hello World!");
|
||||
}
|
||||
}
|
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
@ -0,0 +1,5 @@
|
||||
#Generated by Maven
|
||||
#Sun Dec 03 12:52:32 CST 2023
|
||||
artifactId=ProgrammingQuiz1_CalebFontenot
|
||||
groupId=com.mycompany.programmingquiz1_calebfontenot
|
||||
version=1.0-SNAPSHOT
|
@ -0,0 +1,4 @@
|
||||
com/mycompany/programmingquiz1_calebfontenot/InsertionSort.class
|
||||
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.class
|
||||
com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.class
|
||||
com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam$Node.class
|
@ -0,0 +1,3 @@
|
||||
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/ProgrammingQuiz1_CalebFontenot.java
|
||||
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/InsertionSort.java
|
||||
/home/caleb/ASDV-Java/Semester 3/Exams/Projects/ProgrammingQuiz1_CalebFontenot/src/main/java/com/mycompany/programmingquiz1_calebfontenot/DoubleLinkedListExam.java
|
Loading…
Reference in New Issue
Block a user