From 52d3a5908eb2f7f88bf1d92ef8f269fe04fff739 Mon Sep 17 00:00:00 2001 From: RustyRaptor Date: Thu, 17 Oct 2019 20:53:50 -0600 Subject: [PATCH] Started lab 8 Nqueen problem. --- src/ArraylistStack.java | 48 +++++++++++++ src/LinkStack.java | 65 +++++++++++++++++ src/NQueen.java | 43 ++++++++++++ src/SNode.java | 126 +++++++++++++++++++++++++++++++++ src/StackInterface.java | 7 ++ src/StackTests.java | 152 ++++++++++++++++++++++++++++++++++++++++ 6 files changed, 441 insertions(+) create mode 100644 src/ArraylistStack.java create mode 100644 src/LinkStack.java create mode 100644 src/NQueen.java create mode 100644 src/SNode.java create mode 100644 src/StackInterface.java create mode 100644 src/StackTests.java diff --git a/src/ArraylistStack.java b/src/ArraylistStack.java new file mode 100644 index 0000000..f7de9b2 --- /dev/null +++ b/src/ArraylistStack.java @@ -0,0 +1,48 @@ +import java.util.ArrayList; + +public class ArraylistStack implements StackInterface { + private ArrayList data; + private int cnt; + + public ArraylistStack() { + data = new ArrayList(); + 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(); + } +} diff --git a/src/LinkStack.java b/src/LinkStack.java new file mode 100644 index 0000000..14b6097 --- /dev/null +++ b/src/LinkStack.java @@ -0,0 +1,65 @@ +import java.util.EmptyStackException; + +public class LinkStack implements StackInterface { + private SNode top; + + public SNode getTop() { + return top; + } + + public void setTop(SNode top) { + this.top = top; + } + + public LinkStack(){ + setTop(new SNode(null, null)); + } + + @Override + public boolean push(Object o) { + getTop().addNodeAfterThis((E)o); + return true; + } + + @Override + public E pop() { + if (getTop().getLink() != null) { + SNode popped = + new SNode(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 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"; + } +} diff --git a/src/NQueen.java b/src/NQueen.java new file mode 100644 index 0000000..676bef3 --- /dev/null +++ b/src/NQueen.java @@ -0,0 +1,43 @@ +public class NQueen { + public static void main(String[] args) { + + } + public static ArraylistStack solveNQueen(int N) { + ArraylistStack board = new ArraylistStack(); + 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 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 board){ + + } +} diff --git a/src/SNode.java b/src/SNode.java new file mode 100644 index 0000000..3f1f99b --- /dev/null +++ b/src/SNode.java @@ -0,0 +1,126 @@ +public class SNode { + + /** + * data: an integer stored + * link: the next intnode in the linked list + */ + private E data; + private SNode link; + + + /** + * default constructor + */ + public SNode() { + this.data = null; + this.link = null; + } + + public SNode(E _data, SNode _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 getLink() { + return link; + } + + public void setLink(SNode 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(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 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 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()); + } + + +} diff --git a/src/StackInterface.java b/src/StackInterface.java new file mode 100644 index 0000000..315df16 --- /dev/null +++ b/src/StackInterface.java @@ -0,0 +1,7 @@ + interface StackInterface { + boolean push(E e); + E pop(); + E top(); + int size(); + boolean isEmpty(); +} diff --git a/src/StackTests.java b/src/StackTests.java new file mode 100644 index 0000000..2e58567 --- /dev/null +++ b/src/StackTests.java @@ -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 test = new LinkStack(); + 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 test = new LinkStack(); + 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 test = new LinkStack(); + 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 test = new LinkStack(); + 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 test = new ArraylistStack(); + 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 test = new ArraylistStack(); + 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 test = new ArraylistStack(); + 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 test = new ArraylistStack(); + 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 test = new ArraylistStack(); + test.size(); + test.push(0); + test.size(); + test.push(0); + test.size(); + test.pop(); + test.size(); + test.pop(); + test.size(); + } +}