My linked list is printing blank. Can someone explain what I am missing here.
class Linklist {
private Linklist first;
public int items;
public int itemLocation;
public int lastIndex = -1;
private final String[] list;
public Linklist nextlink;
//Link constructor
public Linklist(int totalItems) {
items = 0;
list = new String[totalItems];
}
public Linklist getNext()
{
return this.nextlink;
}
public void setNext(Linklist n)
{
nextlink = n;
}
public void insert (String item){
list[items] = item;
items++;
}
public void delete(String item){
int location = 0;
while(item.compareTo(list[location]) != 0)
location++;
list[location] = list[items -1];
items--;
}
public boolean doesExist (String item){
boolean search;
int location = 0;
boolean found = false;
search = (location < items);
while (search && !found)
{
if (item.compareTo(list[location])==0)
found = true;
else
{
location++;
search = (location<items);
}
}
return found;
}
public void printUnsortedlist(){
System.out.print("{" + list + "} ");
}
public void printList(){
Linklist currentLink = first;
System.out.print("List: ");
while(currentLink != null){
currentLink.printUnsortedlist();
currentLink = currentLink.getNext();
}
System.out.println("");
}
}
public class Unsortedlist{
/**
* #param args the command line arguments
*/
public static void main(String[] args) {
Linklist list = new Linklist(9);
list.insert("Sun");
list.insert("Mercury");
list.insert("Venus");
list.insert("Earth");
list.insert("Mars");
list.insert("Jupiter");
list.insert("Neptune");
list.insert("Saturn");
list.insert("Uranus");
list.printList();
list.delete("Sun");
if(list.doesExist("Earth"))
System.out.println("Earth is in the list");
else
System.out.println("Earth does not exist!");
list.printList();
}
}
This is my output:
List:
Earth is in the list
List:
BUILD SUCCESSFUL (total time: 0 seconds)
When I integrate a size() method for example:
public int size(){
int currentSize = 0;
Linklist current = head;
while(current != null){
currentSize = currentSize + 1;
current = current.getNext();
}
return currentSize;
}
I get this as the output:
{8} Earth is in the list
My linked list is there but can not figure out why it is printing blank.
For your code ,a few places make me more confused.
First,Why you already have a field "list" ,but then you also set a pointer to the nextlink,if you just want to print as a list ,you could only set two field String value and Linklist next,then you can print your list .
Second,I think the reason why your list is blank is that you set first to currentLink ,and first is null all the time.To solve this problem ,maybe you can change first to this,then your while code will run as you like it.
Just like this:
public void printList(){
Linklist currentLink = this;
System.out.print("List: ");
while(currentLink != null){
currentLink.printUnsortedlist();
currentLink = currentLink.getNext();
}
System.out.println("");
}
But still ,you need to improve your code at many places is you really want the code do a perfect job.Hope this help you out.
Related
I'm trying to do a Depth First Search of my graph, and something is slowing it down quite a lot and I'm not sure what.
Here is my Bag code:
import java.util.Iterator;
import java.util.NoSuchElementException;
public class Bag<Item> implements Iterable<Item> {
private Node<Item> first; // beginning of bag
private Node<Item> end;
private int n; // number of elements in bag
public int label;
public int edges;
public static class Node<Item> {
private Item item;
private Node<Item> next;
public int label;
public int edges;
}
public Bag() {
first = null; // empty bag initialized
end = null;
n = 0;
}
public void add(Item item) {
if (n==0) {
Node<Item> head = new Node<Item>(); // if bag is empty
first = head;
end = head;
head.item = item; // new node both first and end of bag
edges++;
n++;
}
else {
Node<Item> oldlast = end; // old last assigned to end of node
Node<Item> last = new Node<Item>();
last.item = item;
oldlast.next = last; // new node added after old last
end = last;
n++; // size increased
edges++;
}
}
public Iterator<Item> iterator() {
return new LinkedIterator(first); // returns an iterator that iterates over the items in this bag in arbitrary order
}
public class LinkedIterator implements Iterator<Item> {
private Node<Item> current;
public LinkedIterator(Node<Item> first) {
current = first; // iterator starts at head of bag
}
public boolean hasNext() { return current != null; }
public void remove() { throw new UnsupportedOperationException(); }
public Item next() {
if (!hasNext()) throw new NoSuchElementException(); // if there is next item, current is moved to next
Item item = current.item;
current = current.next;
return item; // item is returned
}
}
}
Here is my driver:
import java.util.ArrayList;
import java.util.Random;
public class Driver {
public static ArrayList<Integer> randomNum(int howMany) {
ArrayList<Integer> numbers = new ArrayList<Integer>(howMany);
Random randomGenerator = new Random();
while (numbers.size() < howMany) {
int rand_int = randomGenerator.nextInt(10000);
if (!numbers.contains(rand_int)) {
numbers.add(rand_int);
}
}
return numbers;
}
public static void main(String[] args) {
ArrayList<Integer> num = randomNum(100);
Graph G = new Graph(num);
System.out.println("The length of longest path for this sequence with graph is: " + G.dfsStart(num));
}
}
I send an ArrayList of random integers to my dfsStart method from the driver, which looks at all the different paths for each starting node in my graph. my DepthFirstSearch method calls the getAdjList for each starting node to find its neighbors using my Bag adj, and then works its way down each path before backtracking.
Here is my Graph code, containing my longest path method:
import java.util.ArrayList;
import java.util.NoSuchElementException;
public class Graph {
public final int V; // initializing variables and data structures
public Bag<Integer>[] adj;
public int longestPath;
public Graph(ArrayList<Integer> numbers) {
try {
longestPath = 0;
this.V = numbers.size();
adj = (Bag<Integer>[]) new Bag[V]; // bag initialized
for (int v = 0; v < V; v++) {
adj[v] = new Bag<Integer>();
}
for (int i = 0; i < V; i++) {
adj[i].label = numbers.get(i);
int j = (i + 1);
while (j < numbers.size()) {
if (numbers.get(i) < numbers.get(j)) {
addEdge(i, numbers.get(j));
}
j++;
}
}
}
catch (NoSuchElementException e) {
throw new IllegalArgumentException("invalid input format in Graph constructor", e);
}
}
public void addEdge(int index, int num) {
adj[index].add(num);
}
public int getIndex(int num) {
for (int i = 0; i < adj.length; i++) {
if (adj[i].label == num) {
return i;
}
}
return -1;
}
public Bag<Integer> getAdjList(int source) {
Bag<Integer> adjList = null;
for (Bag<Integer> list : adj) {
if (list.label == source) {
adjList = list;
break;
}
}
return adjList;
}
public int dfsStart(ArrayList<Integer> numbers) {
for (int i=0;i<numbers.size();i++) {
// Print all paths from current node
depthFirstSearch(numbers.get(i),new ArrayList<>(300));
}
return longestPath;
}
public void depthFirstSearch(int src, ArrayList<Integer> current) {
current.add(src);
Bag<Integer> srcAdj = getAdjList(src);
if (srcAdj.size() == 0) {
// Leaf node
// Print this path
longestPath = Math.max(longestPath, current.size());
}
for (int links : srcAdj) {
depthFirstSearch(links, current);
}
current.remove(current.size()-1);
}
}
I believe the suggestion below helped get rid of the error, but it is still unbelievably slow when trying to find the longest path in a graph of more than 150 vertices.
Even for a small dense graph there can be many unique paths from a src node. I tested for this input [1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25] there are 16777216 unique paths from all nodes. So you can expect OOM for bigger inputs. one way is to update the longestPath as soon as a path is found instead of adding it to the list.
Change this to later.
addtoCount(current.size());
to
longestPath = Math.max(longestPath, current.size());
Make sure longestPath is global and initialized to 0 before every test case.
Well, I do not know JAVA but that is an incredible lot of code for doing a simple thing such as depth first search.
In C++ it is done like this:
void cPathFinder::depthFirst(
int v)
{
// initialize visited flag for each node in graph
myPath.clear();
myPath.resize(nodeCount(), 0);
// start recursive search from starting node
depthRecurse(v, visitor);
}
void cPathFinder::depthRecurse(
int v )
{
// remember this node has been visited
myPath[v] = 1;
// look for new adjacent nodes
for (int w : adjacent(v))
if (!myPath[w])
{
// search from new node
depthRecurse(w);
}
}
I am using a Linked list/ Iterater combo to compute the Josephus problem, and need it to only print if there is only one element left in the list, but how would I use an if statement to check a condition that references to the size of the list rather than the number in the list with "ourList" being of type "LinkedList"?
Here is a piece of the code:
if(atEnd() == true){
//System.out.println("Value removed: " + current.dData);
previous.next = current.next;
current = ourList.getFirst();
//if(ourList == Only contains one element){ <--------------
//ourList.displayList();
// }
}
for reference heres my LinkedList + Iterator:
class Link{
public int dData;
public Link next;
public Link(int data){
dData = data;
}
public void displayLink(){
System.out.print(dData + " ");
}
}
class LinkList{
private Link first;
public LinkList(){
first = null;
}
public Link getFirst(){
return first;
}
public void setFirst(Link f){
first = f;
}
public boolean isEmpty(){
return first == null;
}
public ListIterator getIterator(){
return new ListIterator(this);
}
public void displayList(){
Link current = first;
while(current != null){
current.displayLink();
current = current.next;
}
System.out.println("");
}
}
class ListIterator{
private Link current;
private Link previous;
private LinkList ourList;
public ListIterator(LinkList list){
ourList = list;
reset();
}
public void reset(){
current = ourList.getFirst();
previous = null;
}
public boolean atEnd(){
return(current.next == null);
}
public boolean atEndPrevious(){
return(previous.next == null);
}
public Link getCurrent(){
return current;
}
public void nextLink(){
previous = current;
current = current.next;
}
public void insert(int value){
Link newLink = new Link(value);
if(ourList.isEmpty() == true){
ourList.setFirst(newLink);
current = newLink;
}else{
newLink.next = current.next;
current.next = newLink;
nextLink();
}
}
public void beginningPoint(int begin){
if(begin == 1){
current = ourList.getFirst();
previous = null;
// System.out.println("Current Location: " + current.dData);
}else if(begin != 1){
current = ourList.getFirst();
previous = null;
for(int i = 1; i < begin; i++){
nextLink();
// System.out.println("Current Location: " + current.dData);
}
}
}
Adding pseudocode, since it will help you understand the process, but learn by programming it yourself. Also note you could remove some else's through the behavior of exiting.
All of this functions through the iterator.
if at end
dont print and exit (0 elements)
else
get and save next element to variable
if at end
print the variable you saved (only 1 element)
else
dont print and exit (more than 1 element)
I am trying to make an application that will loop through a circular linked list. As it does so, it will use another linked list of index values, and it will use these values to delete from the circular linked list.
I have it set up now where it should fetch the index value to be deleted from my random linked list via runRandomList() method. It then uses the rotate() method to loop through the circular linked list and deletes the value from it. It will then add the deleted value to "deletedLinked list". Then, control should return back to runRandomList() method and it should feed the rotate() method the next value from the random linked list. The circular linked list should begin traversing where it left off. It should keep track of the count and node it is on. The count should reset to 0 when it reaches the first node, so it can properly keep track of which index it is on.
Unfortunately, this is not happening. I have been trying different things for the last few days as the code stands right now; it enters into a continuous loop. the issue appears to be in the rotate method.
This is the rotate method code. My thought was the counter would advance until it matches the index input. If it reaches the first node, the counter would reset to 0 and then start to increment again until it reaches the index value.
private void rotate(int x)
{
while(counter <= x)
{
if(p == names.first)
{
counter = 0;
}
p = p.next;
counter++;
}
deleteList.add((String) p.value);
names.remove(x);
}
This is my linked list class:
public class List<T>{
/*
helper class, creates nodes
*/
public class Node {
T value;
Node next;
/*
Inner class constructors
*/
public Node(T value, Node next)
{
this.value = value;
this.next = next;
}
private Node(T value)
{
this.value = value;
}
}
/*
Outer class constructor
*/
Node first;
Node last;
public int size()
{
return size(first);
}
private int size(Node list)
{
if(list == null)
return 0;
else if(list == last)
return 1;
else
{
int size = size(list.next) + 1;
return size;
}
}
public void add(T value)
{
first = add(value, first);
}
private Node add(T value, Node list)
{
if(list == null)
{
last = new Node(value);
return last;
}
else
list.next = add(value, list.next);
return list;
}
public void setCircularList()
{
last.next = first;
}
public void show()
{
Node e = first;
while (e != null)
{
System.out.println(e.value);
e = e.next;
}
}
#Override
public String toString()
{
StringBuilder strBuilder = new StringBuilder();
// Use p to walk down the linked list
Node p = first;
while (p != null)
{
strBuilder.append(p.value + "\n");
p = p.next;
}
return strBuilder.toString();
}
public boolean isEmpty()
{
boolean result = isEmpty(first);
return result;
}
private boolean isEmpty(Node first)
{
return first == null;
}
public class RemovalResult
{
Node node; // The node removed from the list
Node list; // The list remaining after the removal
RemovalResult(Node remNode, Node remList)
{
node = remNode;
list = remList;
}
}
/**
The remove method removes the element at an index.
#param index The index of the element to remove.
#return The element removed.
#exception IndexOutOfBoundsException When index is
out of bounds.
*/
public T remove(int index)
{
// Pass the job on to the recursive version
RemovalResult remRes = remove(index, first);
T element = remRes.node.value; // Element to return
first = remRes.list; // Remaining list
return element;
}
/**
The private remove method recursively removes
the node at the given index from a list.
#param index The position of the node to remove.
#param list The list from which to remove a node.
#return The result of removing the node from the list.
#exception IndexOutOfBoundsException When index is
out of bounds.
*/
private RemovalResult remove(int index, Node list)
{
if (index < 0 || index >= size())
{
String message = String.valueOf(index);
throw new IndexOutOfBoundsException(message);
}
if (index == 0)
{
// Remove the first node on list
RemovalResult remRes;
remRes = new RemovalResult(list, list.next);
list.next = null;
return remRes;
}
// Recursively remove the element at index-1 in the tail
RemovalResult remRes;
remRes = remove(index-1, list.next);
// Replace the tail with the results and return
// after modifying the list part of RemovalResult
list.next = remRes.list;
remRes.list = list;
return remRes;
}
}
This contains the main(), runRandomList(), and rotate() methods.
public class lottery {
private int suitors;
private List<String> names;
private List<Integer> random;
private List<String> deleteList = new List<>();
private int counter;
private Node p;
public lottery(int suitors, List<String> names, List<Integer> random)
{
this.suitors = suitors;
this.names = names;
this.random = random;
p = names.first;
}
public void start()
{
//Set names list to circular
names.setCircularList();
runRandomList(random);
}
public void runRandomList(List<Integer> random)
{
Node i = random.first;
while(i != null)
{
rotate((int) i.value, counter, p);
i = i.next;
}
}
public List getDeleteList()
{
return deleteList;
}
private void rotate(int x, int count, Node p)
{
Node i = p;
while(count <= x)
{
if(i == names.first)
{
count = 0;
}
i = i.next;
count++;
}
deleteList.add((String) i.value);
names.remove(x);
p = i;
counter = count;
}
public static void main(String[] args)
{
List<String> namesList = new List<>();
namesList.add("a");
namesList.add("b");
namesList.add("c");
namesList.add("d");
namesList.add("e");
namesList.add("f");
List<Integer> randomList = new List<>();
randomList.add(3);
randomList.add(1);
randomList.add(5);
randomList.add(4);
randomList.add(0);
lottery obj = new lottery(6, namesList, randomList);
obj.start();
System.out.println(obj.getDeleteList());
}
}
As I suspected it was the rotate method, this is the solution.
private void rotate(int x, int count)
{
while(count != x)
{
p = p.next;
count++;
if(count == x)
{
deleteList.add((String)p.value);
counter = x;
}
if(count >= suitors)
{
for (int j = 0; j < x ; j++)
{
p = p.next;
}
deleteList.add((String)p.value);
counter = x;
count = x;
}
}
}
I am trying to do an assignment for a class where I use the remove method of a String Bag class to return all the elements of a linked list, one at a time, then delete that element from the list. I have a start, but I can't figure out exactly how to do it. Can anyone help?
public String remove()
{
Random rand = new Random();
int randNum = rand.nextInt(numItems);
//generate random number
int count = 0;
String get;
currNode = firstNode;
//temporary node to get String from
while(count < randNum)
{
currNode = currNode.getLink();
count++;
}
//randomly select node to get String from
get = currNode.getInfo();
numItems--;
if(numItems == 0)
{
firstNode = null;
}
//decrement the number of items in the bag and make the first node
//null when it reaches 0
return get;
}
edit: Here is the application level:
public class StringBagTest
{
public static void main(String[] args)
{
LLStringBag bag = new LLStringBag();
bag.insert("Hat");
bag.insert("Shirt");
bag.insert("Pants");
bag.insert("Shoes");
//insert 4 strings into the list
while(!bag.isEmpty())
{
System.out.println(bag.remove());
}
//randomly removes all contents of list
}
}
If you want to remove randomly chosen element by index then it looks something like this:
public void removeRandomElement() {
int index = new Random().nextInt(size);
Node current = head;
Node prev = head;
for (int i = 0; i < index; i++) {
prev = current;
current = current.next;
}
prev.next = current.next;
current.next = null;
size--;
}
For singly linked list, where size is current size of the list, head — head node.
In other terms, you're doing something like this on the selected element :
Take a look at this link:
link
Also a full example:(making your own Links and Lists)
(The example below is a Linked list which have(links) it's link is a point for example A(50,3). You can transform it to be whatever you want...)
The Link
public class DoublePoint {
public double X;
public double Y;
public int LinkKey=0;
public DoublePoint nextLink; //keeps the nextLink
//Constructor
public DoublePoint(double Xpos,double Ypos,int key){
X=Xpos;
Y=Ypos;
LinkKey=key;
}
public void printLinkKey(){
System.out.println(LinkKey);
}
//Return Link key
public String returnLinkKey(){
return ""+LinkKey;
}
public void changeContent(double x,double y){
X=x;
Y=y;
}
public void ChangeLinkKey(int key){
LinkKey=key;
}
}
The List:
public class ListDoublePoints {
public DoublePoint first;
public int key;
public int totalLinks=0;
public ListDoublePoints(){
first=null;
key=0;
}
//Insert
public void insertLink(double x,double y){
DoublePoint newLink = new DoublePoint(x,y,key);
newLink.nextLink=first;
first=newLink;
key++;
totalLinks++;
}
//Find
public DoublePoint findLinkAt(int key){
DoublePoint current=first;
while(current.LinkKey!=key){
if(current.nextLink==null)
return null;
else
current=current.nextLink;
}
return current;
}
//Delete using Link key (similar with remove(int position) with ready java lists)
public String deleteLinkAt(int linkKey){
DoublePoint current =first;
DoublePoint previous=first;
while(current.LinkKey !=linkKey){
if(current.nextLink == null ){
return "boom";}
else
previous=current;
current=current.nextLink;
}
if(current==first)
first=first.nextLink;
else
previous.nextLink=current.nextLink;
--totalLinks;
return "ok";
}
//Return
public int LinksNumber(){
return totalLinks;
}
//Print
public void displayList(){
DoublePoint current=first;
while(current!=null){
current.displayLink();
current=current.nextLink;
}
}
public void displayTheNumberOfLinks(){
System.out.println(totalLinks);
}
}
*Let me know if you want something like this above or
just to work with the java ready lists..*
If you want to remove all elements of a linked list, you can use the built in clear() method.
If you don't want to use that method, you can just set the head node to null. The garbage collector will take care of the rest.
If you want a remove method that removes one thing at a time and you don't care what it removes, I suggest just removing the first element you find. If it's in a linked list, you can just assign a temp node to the head node, reassign the head node to the next node, and return the temp node.
Do you mean something like this???
Code
private LinkedList<String> list = new LinkedList<>();
private void fillList() {
for (int i = 0; i < 10; i++) {
list.add("Hello " + i);
}
}
private void removeAllRandomly() {
Random random = new Random();
while (!list.isEmpty()) {
int randomPosition = random.nextInt(list.size());
String s = list.remove(randomPosition);
System.out.println(String.format("Item on position: %s (%s) was removed", randomPosition, s));
}
}
Result
Item on position: 9 (Hello 9) was removed
Item on position: 1 (Hello 1) was removed
Item on position: 1 (Hello 2) was removed
Item on position: 2 (Hello 4) was removed
Item on position: 5 (Hello 8) was removed
Item on position: 0 (Hello 0) was removed
Item on position: 3 (Hello 7) was removed
Item on position: 1 (Hello 5) was removed
Item on position: 1 (Hello 6) was removed
Item on position: 0 (Hello 3) was removed
I'm trying to do a removeFirst() using lists that removes the first node of the list and returns the value removed.
Here's what I've got:
public E removeFirst() {
E value=first.val;
first=first.next;
size--;
return value;
}
public void addLast(E v) {
last = new Node(v,last);
size++;
}
Other classes:
class Cliente {
String nome;
int tchegada;
int np;
Cliente(String n, int tc, int p) {
nome = n;
tchegada = tc;
np = p;
}
}
The list:
class List<E> {
private int size;
private Node first;
private Node last;
public boolean isEmpty() {return (size == 0);}
public int size() {return size;}
// construtor de lista vazia
List() {
size = 0;
first = last = null;
}
// um no da lista
private class Node {
E val;
Node next;
Node(E v, Node n) {
val = v;
next = n;
}
}
(...)
Main:
class Prob106_v0 {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
// Read the flag
int flag = in.nextInt();
// Read the boxes to an array
int n = in.nextInt();
Caixa caixas[] = new Caixa[n];
for (int i=0; i<n; i++) {
int k = in.nextInt();
caixas[i] = new Caixa(k);
}
// Read the clients to a list of objects
int c = in.nextInt();
List<Cliente> clientes= new List<Cliente>();
for (int i=0; i<c; i++) {
String nome = in.next();
int tempo_chegada = in.nextInt();
int num_produtos = in.nextInt();
clientes.addLast(new Cliente(nome, tempo_chegada, num_produtos));
}
// Flag 0 - Remove each client and write the name of the one just removed
if(flag == 0) {
while(!clientes.isEmpty()) {
Cliente cli = clientes.removeFirst();
System.out.println(cli.nome);
}
} else {
// This doesn't matter, it's another part of the program I haven't finished.
Subtarefas.resolve(flag, n, caixas, clientes);
}
}
}
Resuming, I am supposed to remove the first element of the list 'clientes' and return what I just removed, in this case the client name (cliente.nome), until there is nothing to remove (null).
But I'm getting NullPointException
Exception in thread "main" java.lang.NullPointerException
at List.removeFirst(Prob106_v0.java:80)
at Prob106_v0.main(Prob106_v0.java:135)
EDIT: The RemoveFirst won't be used when the list is empty. But I found out when the size is 1, the exception generates. How can I fix this?
if your List has only one element this snippet:
public E removeFirst() {
E value=first.val;
first=first.next;
size--;
}
return value;
}
will result in NullPointException
public E removeFirst() {
E value=first.val;
if (size>=2)
{
first=first.next;
}
else if (size==1)
{
last=null;
}
size--;
return value;
}
You need to make sure there's something in your list, or else removeFirst() will blow up
The problem here is first=first.next;
If your list has only one element, there will be no next node, hence null.