Started lab 8 Nqueen problem.

This commit is contained in:
RustyRaptor committed 2019-10-17 20:53:50 -06:00
1 parent a85932a67f
commit 52d3a5908e
6 files changed
+441

No files matched your search

+48
View File
@@ -0,0 +1,48 @@
import java.util.ArrayList;
public class ArraylistStack<E> implements StackInterface {
private ArrayList<E> data;
private int cnt;
public ArraylistStack() {
data = new ArrayList<E>();
cnt = -1;
}
@Override
public boolean push(Object o) {
cnt++;
return data.add((E) o);
}
@Override
public E pop() {
if (!isEmpty()) {
return data.get(cnt--);
}
return null;
}
@Override
public E top() {
if (!isEmpty()) {
return data.get(cnt);
}
return null;
}
@Override
public int size() {
return data.size();
}
@Override
public boolean isEmpty() {
return cnt <= -1;
}
public String toString() {
return data.toString();
}
}
+65
View File
@@ -0,0 +1,65 @@
import java.util.EmptyStackException;
public class LinkStack<E> implements StackInterface {
private SNode<E> top;
public SNode<E> getTop() {
return top;
}
public void setTop(SNode<E> top) {
this.top = top;
}
public LinkStack(){
setTop(new SNode<E>(null, null));
}
@Override
public boolean push(Object o) {
getTop().addNodeAfterThis((E)o);
return true;
}
@Override
public E pop() {
if (getTop().getLink() != null) {
SNode<E> popped =
new SNode<E>(this.getTop().getLink().getData(),
null);
setTop(getTop().getLink());
return popped.getData();
}
return null;
}
@Override
public E top() {
if (getTop().getLink() != null) {
return getTop().getLink().getData();
}
return null;
}
@Override
public int size() {
int count = 0;
SNode<E> cursor = getTop().getLink();
for (cursor = cursor; cursor != null; cursor = cursor.getLink()){
count++;
}
return count;
}
@Override
public boolean isEmpty() {
return size() <= 0;
}
public String toString(){
if (getTop().getLink() != null) {
return this.getTop().toString();
}
return "EMPTY LIST STACK";
}
}
+43
View File
@@ -0,0 +1,43 @@
public class NQueen {
public static void main(String[] args) {
}
public static ArraylistStack<Integer> solveNQueen(int N) {
ArraylistStack<Integer> board = new ArraylistStack<Integer>();
Integer rowid = 1;
Integer Qpos = 1;
while (board.size() < N){
while (Qpos <= N) {
boolean conflict = checkConflict(Qpos);
if (conflict) {
board.push(Qpos);
Qpos = 1;
rowid = board.size() + 1;
} else {
Qpos++;
}
}
while (!board.isEmpty() && Qpos >= N) {
if (board.pop() < N) {
board.push(++Qpos);
}else {
}
}
}
}
public static boolean checkConflict(Integer Qpos,
ArraylistStack<Integer> board){
int dist = 1;
while (!board.pop().equals(Qpos) && Math.abs(Qpos - board.pop()) != dist){
dist++;
}
return board.size() == 0;
}
public static void printBoard(ArraylistStack<Integer> board){
}
}
+126
View File
@@ -0,0 +1,126 @@
public class SNode<E> {
/**
* data: an integer stored
* link: the next intnode in the linked list
*/
private E data;
private SNode<E> link;
/**
* default constructor
*/
public SNode() {
this.data = null;
this.link = null;
}
public SNode(E _data, SNode<E> _link) {
this.data = _data;
this.link = _link;
}
// Getters and setters
public E getData() {
return data;
}
public void setData(E data) {
this.data = data;
}
public SNode<E> getLink() {
return link;
}
public void setLink(SNode<E> link) {
this.link = link;
}
/**
* adds a new SNode after the node it's called from
*
* @param newData the data you want to add (int)
*/
public void addNodeAfterThis(E newData) {
setLink(new SNode<E>(newData, this.link));
}
/**
* removes the SNode after the one it's called from
*/
public void removeNodeAfterThis() {
if (getLink() == null) {
System.out.println("The list is empty, Nothing to " +
"remove");
} else {
setLink(getLink().getLink());
}
}
/**
* gets the number of nodes after the one it's called from.
* @return the number of items in the List
*/
public int listLength() {
if (this.data == null) {
return 0;
}
int cnt = 1;
for (SNode<E> cursor = this.getLink(); cursor != null; cursor
= cursor.getLink()) {
cnt++;
}
return cnt-1;
}
/**
* adds a new integer to the end of the linked list.
*
* @param newData the new integer to be added.
*/
public void addToEnd(E newData) {
SNode cursor = this;
while(cursor.getLink() != null){ cursor = cursor.getLink(); }
cursor.addNodeAfterThis(newData);
}
/**
* Builds a string from all the nodes in the list.
*
* @return a string of the data separated by arrows.
*/
@Override
public String toString() {
StringBuilder linkStr = new StringBuilder();
SNode answer = null;
for (SNode<? extends E> cursor = getLink(); cursor != null; cursor =
cursor.getLink()) {
linkStr.append(cursor.getData());
if (cursor.getLink() != null) {
linkStr.append("->");
}
}
return "SNode{" +
"data=" + linkStr +
'}';
}
/**
* prints out the toString value of the Node.
*/
public void printData() {
System.out.println(this.toString());
}
}
+7
View File
@@ -0,0 +1,7 @@
interface StackInterface<E> {
boolean push(E e);
E pop();
E top();
int size();
boolean isEmpty();
}
+152
View File
@@ -0,0 +1,152 @@
public class StackTests {
public static void main(String[] args) {
System.out.println("=====TESTING LinkedList STACK=====");
announceTest("LIST PUSH");
testListPush();
announceTest("LIST POP");
testListPop();
announceTest("LIST TOP");
testListTop();
announceTest("LIST isEmpty");
testListIsEmpty();
System.out.println("=====LinkedList STACK TESTS " +
"COMPLETE!!!=====");
System.out.println("=====TESTING ArrayList STACK=====");
announceTest("ARR PUSH");
testArrPush();
announceTest("ARR POP");
testArrPop();
announceTest("ARR TOP");
testArrTop();
announceTest("ARR isEmpty");
testArrIsEmpty();
announceTest("ARR SIZE");
testArrSize();
System.out.println("=====ARRAYLIST STACK TESTS " +
"COMPLETE!!!=====");
}
static void announceTest(String name) {
for (int i = 0; i < 50; i++) {
System.out.print("=");
}
System.out.print("END OF TEST");
System.out.println();
System.out.println("TESTING " + name);
System.out.println();
}
static void testListPush(){
LinkStack<Integer> test = new LinkStack<Integer>();
test.push(1);
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test);
}
static void testListPop(){
LinkStack<Integer> test = new LinkStack<Integer>();
test.pop();
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
test.pop();
test.pop();
for (int i = 0; i < 10; i++) {
test.pop();
}
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test);
System.out.println(test.pop());
}
static void testListTop(){
LinkStack<Integer> test = new LinkStack<Integer>();
test.top();
System.out.println(test.top());
test.push(0);
System.out.println(test.top());
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test.top());
}
static void testListIsEmpty(){
LinkStack<Integer> test = new LinkStack<Integer>();
System.out.println(test.isEmpty());
System.out.println(test.isEmpty());
test.push(1);
System.out.println(test.isEmpty());
test.pop();
System.out.println(test.isEmpty());
}
static void testArrPush(){
ArraylistStack<Integer> test = new ArraylistStack<Integer>();
test.push(1);
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test);
}
static void testArrPop(){
ArraylistStack<Integer> test = new ArraylistStack<Integer>();
test.pop();
test.isEmpty();
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
test.pop();
test.pop();
for (int i = 0; i < 10; i++) {
test.pop();
}
System.out.println(test);
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test);
System.out.println(test.pop());
}
static void testArrTop(){
ArraylistStack<Integer> test = new ArraylistStack<Integer>();
test.top();
System.out.println(test.top());
test.push(0);
System.out.println(test.top());
for (int i = 0; i < 10; i++) {
test.push(i);
}
System.out.println(test.top());
}
static void testArrIsEmpty(){
ArraylistStack<Integer> test = new ArraylistStack<Integer>();
System.out.println(test.isEmpty());
System.out.println(test.isEmpty());
test.push(1);
System.out.println(test.isEmpty());
test.pop();
System.out.println(test.isEmpty());
}
static void testArrSize(){
ArraylistStack<Integer> test = new ArraylistStack<Integer>();
test.size();
test.push(0);
test.size();
test.push(0);
test.size();
test.pop();
test.size();
test.pop();
test.size();
}
}