CircularList

master
Caleb Fontenot 2023-11-03 13:33:29 +07:00
parent d9e87bf2f9
commit 643bdc658f
5 changed files with 257 additions and 0 deletions

1
.gitignore vendored

@ -192,3 +192,4 @@
/Semester 3/Assignments/TestHashCode/build/
/Semester 3/Assignments/LinkedList/nbproject/private/
/Semester 3/Assignments/LinkedList/build/
/Semester 3/Assignments/MP6_CalebFontenot/target/

@ -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>edu.slcc.asdv.caleb</groupId>
<artifactId>MP6_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>edu.slcc.asdv.caleb.mp6_calebfontenot.MP6_CalebFontenot</exec.mainClass>
</properties>
</project>

@ -0,0 +1,139 @@
package edu.slcc.asdv.caleb.mp6_calebfontenot;
/**
*
* @author Markou
*/
public class CircularList<T extends Comparable<T>> {
Node<T> head;
public class Node< T extends Comparable<T>>
implements Comparable<Node<T>> {
private T t;
public void set(T t)
{
this.t = t;
}
public T get()
{
return t;
}
Node<T> next;
@Override
public int compareTo(Node<T> o)
{
return this.t.compareTo(o.get());
}
}
/**
*
* @param t
*/
public void add(Object t)
{
Node<T> temp = new Node();
temp.set((T) t);
if (head == null) {
head = temp;
head.next = head;
} else {
Node<T> temp2 = head;
do {
temp2 = temp2.next;
} while (temp2.next != head);
temp.next = head;
temp2.next = temp;
}
}
/**
*
* @return
*/
private int getSize()
{
if (head == null) {
return 0;
}
Node<T> temp = head;
int count = 0;
do {
temp = temp.next;
count++;
} while (temp != head);
return count;
}
////////////////////////////////////////////////////////
public void addAt(Object t, int pos)
{
if (pos < 0 || pos > getSize()) {
return;
}
Node<T> temp = new Node();
temp.set((T) t);
if (head == null) {
add(t);
} else if (pos == 0) {
} else {
}
}
//////////////////////////////////////////////////////////////
/**
* removes the
*/
public void remove()
{
}
//////////////////////////////////////////////////////////////
/**
* removes a specific object from the list
*/
public boolean remove(Object t)
{
return true;
}
//////////////////////////////////////////////////////////////
public void removeAt(int pos)
{
}
public void print()
{
if (head == null) {
return;
}
Node<T> temp = head;
do {
System.out.println(temp.get().toString());
temp = temp.next;
} while (temp != head);
}
public static void main(String... ar)
{
CircularList<Integer> list = new CircularList();
list.add("Hello");
list.add("World");
list.print();
}
}

@ -0,0 +1,87 @@
/*
* 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 DoubleLinkedList<T> {
Node<T> head;
Node<T> tail;
class Node<T> {
T t;
Node<T> left;
Node<T> right;
}
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.right = head;
head.left = newNode;
head = newNode;
} else if (pos == size()) {// add at the end
newNode.left = tail;
tail.right = newNode;
tail = newNode;
} else { //We're in the middle
Node<T> p = head;
for (int i = 0; i < pos - 1; ++i) {
p = p.right;
}
newNode.left = p;
newNode.right = p.right;
p.right.left = newNode;
}
}
public int size() {
Node<T> p = head;
int count = 0;
while (p != null) {
count++;
p = p.right;
}
return count;
}
@Override
public String toString()
{
String outputString = "DoubleLinkedList{" + "head=" + head + ", tail=" + tail +", size=" + this.size() + "}\n Contents= [";
Node<T> p = head;
int count = 0;
while (p != null) {
outputString += p.t.toString();
if (count -1 < size()) {
outputString += ", ";
} else {
outputString += "]";
}
p = p.right;
count++;
}
return outputString;
}
public static void main(String[] args)
{
DoubleLinkedList<Integer> list = new DoubleLinkedList<>();
list.addAt(10, 0);
list.addAt(20, 0);
list.addAt(30, 0);
list.addAt(40, 0);
list.addAt(40, 3);
list.addAt(25, 3);
System.out.println(list);
}
}

@ -0,0 +1,16 @@
/*
* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-default.txt to change this license
*/
package edu.slcc.asdv.caleb.mp6_calebfontenot;
/**
*
* @author caleb
*/
public class MP6_CalebFontenot {
public static void main(String[] args) {
System.out.println("Hello World!");
}
}