I'm having trouble with a project I have for my OOP class. I'm nearly finished, but still lack a toString method and a main method. Not really sure how to go about it and would appreciate any help.
I want my toString method to function as follows:
Returns a string representation of all the items stored in the list. A string representation of an empty list looks like head--><--tail
A string representation of a non-empty list looks like:
head-->134<-->-8<-->42<-->1<--tail
public class IntegerNode{
private IntegerNode next;
private IntegerNode prev;
private int data;
public IntegerNode(int data){
next = next;
prev = prev;
data = data;
}
public int getData(){
data = data;
return this.data;
}
public IntegerNode getNext(){
return next;
}
public IntegerNode getPrevious(){
return prev;
}
public void setNext(IntegerNode in){
prev = in;
}
public void setPrevious(IntegerNode in){
prev = in;
}
}
and here is what I have so far in my IntegerLinkedList class
public class IntegerLinkedList{
private IntegerNode head;
private IntegerNode tail;
public IntegerLinkedList(){
head = null;
tail = null;
}
public void addFirst(int x){
IntegerNode nH = new IntegerNode(x);
if (head == null) {
head = nH;
tail = nH;
}else{
head.setPrevious(nH);
nH.setNext(head);
head = nH;
}
}
public void addLast(int x){
IntegerNode t = new IntegerNode(x);
if (tail == null){
head = t;
tail = t;
}else{
tail.setNext(t);
t.setPrevious(tail);
tail = t;
}
}
public int peekFirst(){
return head.getData();
}
public int peekLast(){
return tail.getData();
}
public String toString(){
if (head == null && tail == null){
String empty = "head--><--tail";
return empty;
}else{
String h = "Head--> " + head;
String t = tail + " <--Tail";
String m = " <--> ";
// while(IntegerNode.getNext() != null)
//}
//return h + m + t;
}
}
public int pollFirst(){
int x = head.getData();
head = head.getNext();
head.setPrevious(null);
return x;
}
public int pollLast(){
int x = tail.getData();
tail = tail.getPrevious();
tail.setNext(null);
return x;
}
}
I'm thinking a while loop is the way to go here, but then again I'm not sure.
Here's how to write it:
#Override // <-- Annotate that you are overriding the toString() method
public String toString(){
if (head == null && tail == null){
String empty = "head--><--tail";
return empty;
}else{
StringBuilder sb = new StringBuilder();
sb.append("Head-->");
IntegerNode curr = head;
sb.append(curr.getData());
curr = curr.getNext();
while(curr != null) {
sb.append("<-->");
sb.append(curr.getData());
curr = curr.getNext();
}
sb.append("<--tail");
return sb.toString();
}
}
As an alternative, you can simplify the logic to not have an outer if else:
#Override // <-- Annotate that you are overriding the toString() method
public String toString(){
StringBuilder sb = new StringBuilder();
sb.append("Head-->");
IntegerNode curr = head;
if (curr == null)
{
sb.append("<--tail");
return sb.toString();
}
sb.append(curr.getData());
curr = curr.getNext();
while(curr != null) {
sb.append("<-->");
sb.append(curr.getData());
curr = curr.getNext();
}
sb.append("<--tail");
return sb.toString();
}
Yes, you have to use a loop, because you want to iterate over data of unknown length. Michael Markidis wrote the answer more quickly than me, use his solution, however I would suggest some improvements to your code.
String h = "Head--> " + head; won't work, because head is an object IntegerNode, and you want to access its data like this head.getData() (also why do you assign data = data; in this method? It should only do return)
If you want to assign data in constructor that have the same name as field, use have to use this keyword to make clear what do you want to assign. Also assigning null next and prev has no meaning, so this code
public IntegerNode(int data){
next = next;
prev = prev;
data = data;
}
should look like this
public IntegerNode(int data){
this.data = data;
}
or if you want to assign the previous and next node
public IntegerNode(int data, IntegerNode next, IntegerNode prev){
this.next = next;
this.prev = prev;
this.data = data;
}
If you are using Java 8+, StringJoiner makes it easy.
#Override
public String toString() {
StringJoiner joiner = new StringJoiner("<-->", "head-->", "<--tail");
for (IntegerNode node = this.head; node != null; node = node.getNext())
joiner.add(String.valueOf(node.getData()));
return joiner.toString();
}
If not using Java 8, a StringBuilder is the right way to go.
(Performs better than using String directly)
#Override
public String toString() {
StringBuilder buf = new StringBuilder("head-->");
boolean sep = false;
for (IntegerNode node = this.head; node != null; node = node.getNext()) {
if (sep)
buf.append("<-->");
buf.append(node.getData());
sep = true;
}
return buf.append("<--tail").toString();
}
In both cases, you use a basic for loop with a node variable to iterate through the list.
As for the rest of your code, you have some issues.
public IntegerNode(int data){
next = next;
prev = prev;
data = data;
}
Assigning next to next and prev to prev is meaningless.
Assigning the parameter to the field will only work if you qualify the field with this., otherwise you're assigning the parameter to itself (meaningless).
public IntegerNode(int data){
this.data = data;
}
public int getData(){
data = data;
return this.data;
}
Assigning data to data is meaningless.
public int getData(){
return this.data;
}
public void setNext(IntegerNode in){
prev = in;
}
Copy/paste error. You meant to assign to next.
public void setNext(IntegerNode in){
next = in;
}
public int pollFirst(){
int x = head.getData();
head = head.getNext();
head.setPrevious(null);
return x;
}
public int pollLast(){
int x = tail.getData();
tail = tail.getPrevious();
tail.setNext(null);
return x;
}
These methods will throw NullPointerException when you poll the last1 value from the list.
Add missing if statement.
public int pollFirst(){
int x = head.getData();
head = head.getNext();
if (head == null)
tail = null;
else
head.setPrevious(null);
return x;
}
public int pollLast(){
int x = tail.getData();
tail = tail.getPrevious();
if (tail == null)
head = null;
else
tail.setNext(null);
return x;
}
1) "last" refers to the "only remaining" value, not the "tail" value.
Related
I have created a class SLList where I take in a doubly linked list SLList where the input can be of any generic type. In order to aid myself in doing this, I've implemented sentinels to help me.
However, now, I need to find how to turn my Doubly Linked List into a string via method turntostring()
Here is my code so far...
public class SLList<T>{
private class IntNode {
private T data;
private IntNode previous;
private IntNode next;
public IntNode (T data, IntNode previous, IntNode next) {
this.data = data;
this.previous = previous;
this.next = next;
}
public IntNode () {
next = previous = this;
}
}
public int size = 0;
IntNode sentinel;
public SLList(){
sentinel = new IntNode();
}
public String turntostring() {
String S = "";
for (int x = 0; x < size; x++) {
S += x;
}
return S;
However, I'm struggling to understand how an I supposed to turn Doubly Linked List values into strings. My attempt so far has been yielding an error. Can anyone please help explain this to me?
Few things I will recommend
Improve your variable naming scheme, like you have used a sentinel
name which sounds illogical in this context.
The logic is also a bit incorrect, you haven't added an addNode method which can be problematic.
I have corrected the code and which I will mention here. Do let me know if you have any problems.
public class SLList<T> {
public int size = 0;
IntNode head, tail = null;
// DoubliLinkedList Element - IntNode
private class IntNode {
private T data;
private IntNode previous;
private IntNode next;
public IntNode(T data, IntNode previous, IntNode next) {
this.data = data;
this.previous = previous;
this.next = next;
}
public IntNode() {
next = previous = this;
}
public IntNode(T data) {
this.data = data;
}
}
public void addNode(T data) {
// Create a new node
IntNode newNode = new IntNode(data);
// If doubly list is empty
if (head == null) {
head = tail = newNode;
// head's previous will point to null
head.previous = null;
// tail's next will point to null, as it is the last node of the list
tail.next = null;
} else {
// newNode will be added after tail such that tail's next will point to newNode
tail.next = newNode;
// newNode's previous will point to tail
newNode.previous = tail;
// newNode will become new tail
tail = newNode;
// As it is last node, tail's next will point to null
tail.next = null;
}
}
// public SLList() {
// sentinel = new IntNode();
// }
public String turnTostring() {
// String S = "";
// for (int x = 0; x < size; x++) {
// S += x;
// }
// return S;
IntNode current = head;
if (head == null) {
return "Empty DLL";
}
String list = "null <==> ";
while (current != null) {
list += current.data + " <==> ";
current = current.next;
}
list += null;
return list;
}
public static void main(String[] args) {
SLList<Integer> dl1 = new SLList<Integer>();
dl1.addNode(3);
dl1.addNode(34);
dl1.addNode(36);
dl1.addNode(333);
String turnTostring = dl1.turnTostring();
System.out.println(turnTostring);
}
}
I am trying to return all node contents that match a given String input. What I am trying to do is essentially a very simple search engine, where the user is able to type in a String and the program returns all characteristically similar contents it can find in the linked list. The linked list itself is built from a file, formatted as
<<Game’s Name 0>>\t<<Game’s Console 0>>\n
<<Game’s Name 1>>\t<<Game’s Console 1>>\n
where the lines are delimited with a \n and the game and its corresponding console are delimited with a \t.
My current methodology follows searching the linked list with a while loop, assigning a temporary value to the head and reassigning it to it's link as it goes down the list. Once the loop finds contents within a node that matches the current input, it stops the loop and returns the data found in the node. I have yet to try if this could be done with a for loop, as the while loop more than likely would not know when to continue once it has found a match. I am also unsure if the while loop argument is the most efficient one to use, as my understanding of it is very minimal. I believe !temp.equals(query) is stating "temp does not equal query," but I have a feeling that this could be done in a more efficient manner.
This is what I have so far, I will provide the entire Generic linked list class for the sake of context, but the method I am questioning is the very last one, found at line 126.
My explicitly stated question is how can I search through a linked list's contents and return those contents through the console.
import java.io.FileNotFoundException;
import java.util.Scanner;
public class GenLL<T>
{
private class ListNode
{
T data;
ListNode link;
public ListNode(T aData, ListNode aLink)
{
data = aData;
link = aLink;
}
}
private ListNode head;
private ListNode current;
private ListNode previous;
private int size;
public GenLL()
{
head = current = previous = null;
this.size = 0;
}
public void add(T aData)
{
ListNode newNode = new ListNode(aData, null);
if (head == null)
{
head = current = newNode;
this.size = 1;
return;
}
ListNode temp = head;
while (temp.link != null)
{
temp = temp.link;
}
temp.link = newNode;
this.size++;
}
public void print()
{
ListNode temp = head;
while (temp != null)
{
System.out.println(temp.data);
temp = temp.link;
}
}
public void addAfterCurrent(T aData)
{
if (current == null)
return;
ListNode newNode = new ListNode(aData, current.link);
current.link = newNode;
this.size++;
}
public T getCurrent()
{
if(current == null)
return null;
return current.data;
}
public void setCurrent(T aData)
{
if(aData == null || current == null)
return;
current.data = aData;
}
public void gotoNext()
{
if(current == null)
return;
previous = current;
current = current.link;
}
public void reset()
{
current = head;
previous = null;
}
public boolean hasMore()
{
return current != null;
}
public void removeCurrent()
{
if (current == head)
{
head = head.link;
current = head;
}
else
{
previous.link = current.link;
current = current.link;
}
if (this.size > 0)
size--;
}
public int getSize()
{
return this.size;
}
public T getAt(int index)
{
if(index < 0 || index >= size)
return null;
ListNode temp = head;
for(int i=0;i<index;i++)
temp = temp.link;
return temp.data;
}
public void setAt(int index, T aData)
{
if(index < 0 || index >= size || aData == null)
return;
ListNode temp = head;
for (int i = 0; i < index; i++)
temp = temp.link;
temp.data = aData;
}
public T search() throws FileNotFoundException {
Scanner keyboard = new Scanner(System.in);
System.out.println("Search: ");
String query = keyboard.nextLine();
ListNode temp = head;
while(!temp.equals(query))
temp = temp.link;
return temp.data;
//plus some sort of print function to display the result in the console
}
}
You can apply regex for every node's content, if the data type is string apply it if it is of some other datatype convert it into string if possible, else throw some exceptions.
I'm struggling to construct a Linked List object for building strings. My class LString is meant to mimic a String or StringBuilderobject. Instead of arrays, it uses a linked list to form strings. I'm unsure of how to form the constructor though.
Here is my code so far:
public class LString {
// 2. Fields
node front;
//node tail;
int size;
// 1. Node class
private class node {
char data;
node next;
//constructors
//1. default
public node (){
}
//2. data
public node (char newData){
this.data = newData;
}
//3. data + next
public node (char newData, node newNext){
this.data = newData;
this.next = newNext;
}
}
// 3. Constructors
public LString(){
this.size = 0;
this.front = null;
}
public LString(String original) {
}
// 4. Methods
public int length() {
return this.size;
}
public int compareTo(LString anotherLString) {
return 0;
}
public boolean equals(Object other) {
if (other == null || !(other instanceof LString)) {
return false;
}
else {
LString otherLString = (LString)other;
return true;
}
}
public char charAt(int index) {
return 'a';
}
public void setCharAt(int index, char ch) {
ch = 'a';
}
public LString substring(int start, int end) {
return null;
}
public LString replace(int start, int end, LString lStr) {
return null;
}
//append
public void append (char data){
this.size++;
if (front == null){
front = new node(data);
return;
}
node curr = front;
while (curr.next != null){
curr = curr.next;
}
curr.next = new node(data);
}
//prepend
public void prepend (char data){
/*node temp = new node(data);
temp.next = front;
front = temp;*/
front = new node(data, front);
size++;
}
//delete
public void delete(int index){
//assume that index is valid
if (index == 0){
front = front.next;
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = curr.next.next;
}
size--;
}
//toString
public String toString(){
StringBuilder result = new StringBuilder();
result.append('[');
node curr = front;
while (curr != null){
result.append(curr.data);
if (curr.next != null){
result.append(',');
}
curr = curr.next;
}
result.append(']');
return result.toString();
}
//add (at an index)
public void add(int index, char data){
if (index == 0){
front = new node(data, front);
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = new node(data, curr.next);
}
}
}
Many of the methods are stubs, so the class will compile with another test file. I don't think I need to include it to find the issue though.
Thanks for the help.
You can build your LString constructor in different ways. One way I can think of is to accepting char[] and store it in your internal LinkedList. You can take a look at String constructors in here to get more ideas.
I'm having trouble developing a linked list class to mimic standard string and string builder classes in Java.
I'm trying to learn how to use and manipulate Linked Lists, and I want to make a class called LStringthat is a string object made from a linked list of characters, instead of arrays.
So far, this is how I understand to set up a Linked List class:
public class LString {
//Fields
node front;
//node tail;?
int size;
// Node class
private class node {
char data;
node next;
//constructors
//default
public node (){
}
//data
public node (char newData){
this.data = newData;
}
//data + next
public node (char newData, node newNext){
this.data = newData;
this.next = newNext;
}
// Constructors
public LString(){
this.size = 0;
this.front = null;
}
//Methods
//append
public void append (char data){
this.size++;
if (front == null){
front = new node(data);
return;
}
node curr = front;
while (curr.next != null){
curr = curr.next;
}
curr.next = new node(data);
}
//prepend
public void prepend (int data){
front = new node(data, front);
size++;
}
//delete
public void delete(int index){
//assume that index is valid
if (index == 0){
front = front.next;
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = curr.next.next;
}
size--;
}
//toString
public String toString(){
StringBuilder result = new StringBuilder();
result.append('[');
node curr = front;
while (curr != null){
result.append(curr.data);
if (curr.next != null){
result.append(',');
}
curr = curr.next;
}
result.append(']');
return result.toString();
}
//add (at an index)
public void add(int index, int data){
if (index == 0){
front = new node(data, front);
} else {
node curr = front;
for (int i = 0; i < index - 1; i++){
curr = curr.next;
}
curr.next = new node(data, curr.next);
}
}
}
I am receiving this error message:
LString.java:41: error: invalid method declaration; return type required
public LString(){
I have seen other solutions to this problem by adding something like this:
public static void main(String[] args) {
LString lstring = new LString();
}
but that didn't work for me. Any help is appreciated.
You need to close the bracket of node inner class. In the given code, public LString() function is defined inside node class so it should have return type.
I have a project where I have to write a bunch of sort methods and measure the time complexity for each, and output the results to an output text file. the program runs but i get some null pointer exceptions in bubblesort method. here is my code and error, if you can tell me how to fix my sort methods, that would be awesome!
linked list class:
public class LinkedList {
protected static class Node {
Comparable item;
Node prev, next;
public Node(Comparable newItem, Node prev, Node next) {
this.item = newItem;
this.prev = prev;
this.next = next;
}
public Node (Comparable newItem) {
this(newItem, null, null);
}
public Node() {
this(null, null, null);
}
public String toString() {
return String.valueOf(item);
}
}
private Node head;
private int size;
public int dataCompares, dataAssigns;
public int loopCompares, loopAssigns;
public int other;
public LinkedList() {
head = new Node(null, null, null);
head.prev = head;
head.next = head;
size = 0;
}
public boolean add(Comparable newItem) {
Node newNode = new Node(newItem);
Node curr;
if(isEmpty()) {
head.next = newNode;
head.prev = newNode;
newNode.next = head;
newNode.prev = head;
} else {
newNode.next = head;
newNode.prev = head.prev;
head.prev.next = newNode;
head.prev = newNode;
}
size++;
return false;
}
public boolean remove(Comparable item) {
if(!isEmpty()) {
Node prev = null;
Node curr = head;
while(curr!=null) {
if(curr.item.compareTo(item)==0) {
if(prev==null) {
head=curr.next;
} else {
prev.next = curr.next;
curr=curr.next;
}
size--;
return true;
}else{
prev=curr;
curr = curr.next;
}
}
}
return false;
}
public void removeAll() {
this.head.prev = null;
this.head.next = null;
size = 0;
}
public boolean isEmpty() {
return size == 0;
}
public boolean remove(Object item) {
return true;
}
public void insertSortNode() {
Node back = head;
if (size < 2)
return;
back = back.next; // SECOND entry in the list
while ( back != null ) { // I.e., end-of-list
Comparable value = back.item;
Node curr = head; // Start at the front
// Find insertion point for value;
while (curr != back && value.compareTo(curr.item) >= 0)
curr = curr.next;
// Propogate values upward, inserting the value from back
while (curr != back){
Comparable hold = curr.item;
curr.item = value;
value = hold;
curr = curr.next;
}
back.item = value; // Drop final value into place!
back = back.next; // Move sorted boundary up
}
} // end insertSort()
public void selSort() {
Node front = head;
// Nothing to do on an empty list
if ( front == null )
return;
while ( front.next != null ) { // skips a one-entry list
Node tiny = front;
Node curr = front.next;
Comparable temp = front.item; // start the swap
for ( ; curr != null ; curr = curr.next ) {
if ( tiny.item.compareTo(curr.item) > 0 )
tiny = curr;
}
front.item = tiny.item; // Finish the swap
tiny.item = temp;
front = front.next; // Advance to the next node
}
// The structure is unchanged, so the validity of tail is unchanged.
}
public void bubbleSort() {
Node Trav=head.next;
Node Trail=head.next;
Comparable temp;
if (Trav != null)
Trav = Trav.next;
while(Trav!=null) {
if (Trav.item.compareTo(Trail.item)<0) {
temp = Trail.item;
Trail.item=Trav.item;
Trav.item = temp;
}
Trail=Trav;
Trav=Trav.next;
}
}
public void insertSortArray() {
Node insert1, cur, tmp1;
Comparable temp;
for(insert1 = this.head.next.next; insert1!=this.head; insert1 = insert1.next) {
//++loopcompares; ++loopassigns;
for (cur = head.next; cur!=insert1; cur=cur.next) {
//++loopCompares; ++loopassigns;
//++datacompares;
if(insert1.item.compareTo(cur.item)<0) {
temp=insert1.item;
//++dataassign
tmp1=insert1;
//++other
while(tmp1!=cur.prev) {
//++loopcomares
tmp1.item=tmp1.prev.item;
tmp1=tmp1.prev;
//++dataassign+=2
}
//++loopcompares
cur.item = temp;
//++dataassign;
break;
}
}
//++loopcompares; ++loopassigns;
}
//++loopcompares; ++loopassigns
}
public void disp6sortsFile(boolean disp, String fileName, String header, String data) {
FileWriter fw = null;
PrintWriter pw = null;
try {
File file = new File(fileName);
fw = new FileWriter(file, true);
pw = new PrintWriter(fw, true);
} catch (IOException e) {
System.err.println("File open failed for " +fileName+ "\n" + e);
System.exit(-1);
}
if (disp) {
pw.print(header + "\n");
}
pw.print(data + "\n");
pw.close();
}
}
here is my error:
Exception in thread "main" java.lang.NullPointerException
at LinkedList.bubbleSort(LinkedList.java:149)
at LinkListTester.main(LinkListTester.java:51)
the linkedlisttester error is simply list1.bubbleSort(); so bubble sort is the problem.
Change:
public String toString() {
return this.item.toString();
}
to:
public String toString() {
return String.valueOf(item); // Handle null too.
}
For add return true. Might check that item is not null if so desired.
remove is written for a single linked list.
In remove the head has a null item, which might have caused the error. Also as we have a circular list with a dummy node for head, the termination should not test for null but head. Otherwise a not present item will loop infinitely.
public boolean remove(Comparable item) {
if(!isEmpty()) {
Node prev = null;
Node curr = head.next; // !
while(curr!=head) { // !
if(curr.item.compareTo(item)==0) {
if(prev==null) { // ! WRONG, but I will not correct home work ;)
head=curr.next;
} else {
prev.next = curr.next;
curr=curr.next;
}
size--;
return true;
}else{
prev=curr;
curr = curr.next;
}
}
}
return false;
}
swap is written for a single linked list.
And here I stopped reading, as I've come to the usages.
Second Edit:
All algorithmic functions, i.e. bubbleSort, have the following control flow:
while(Trav!=null) { ... Trav = Trav.next; }
But the data structure is defined cyclic, so eventually you arrive back at head and there the item is null.
The solution is to have for the first Node a prev null, and for the last Node a next null.
To make this clear, readable, you could substitute the Node head with:
Node first;
Node last;