Cat------>Dog------>Horse----->Parrot
^P1 ^P2 ^P3 ^P4
Draw the resulting linked list after executing the following statements:
P4 = P2;
P3.info = P2.info
etc.
My question is, what does '.info' reference?
I've looked in the API for both node and linked list and haven't found anything.
Any ideas?
This would entirely depend on the specific implementation used in your assignment, but it sounds like info contains the data of the specific node in the linked list, i.e. P1.info is Cat.
Each node in a standard linked list has two pieces of information:
A reference the the next node
The data contained in the current node
I'm not sure if your instructor wants you to take into account that you would have to "clone" the node in order to have a separate object with the same data or if your instructor wants you to take it literally where setting one object equal to another object simply makes the first one a reference to the second one.
As spookyjon said, the info appears to be a public variable in the node class for the data (cat, dog, horse, parrot).
Related
I have this list : [5,3,5,5,3,7,7,5,3,2]
and I need to remove every time I have a sequence in the list which is the same as a queue given to me - for example the queue [exit-5,3-enter] my function returns
[5,3,5,7,7,2-->null] but I want it to be [5,7,7,2-->null].
I would also like to mention that when I am talking about a linked list I actually mean a list of nodes-
Node<Integer> lis
(it has a value and a pointer to the next node). I already did the function I mentioned but what I did doesn't work if the sequence appears at the start of the list-like Iv'e already mentioned because I don't know how to remove from the start of the list. I know how to remove a node I just reference to next Nodes and leave it with no reference but my function gets a copy of the main reference to the list
public static void removeAppear(Node<Integer> n,Queue<Integer> q){
so if I would just write :
n=n.getNext();
for example it won't actually delete the first node, and from the void main method I would still see the first node. If you need the function I did say so but I really just need to know how to delete a first node.Sorry if it's a basic question but i'm quite new to programming.
You can’t mutate a linked list to remove leading nodes, unless you stop thinking of your function as ‘changing the list’ and start thinking of it as ‘returning a new list which is different from the original’. Then it can return a reference to a node which was not originally the first node.
I'm studying Cracking the Coding Interview, and the code for constructing a Node class has a int data variable. What is the point of this variable? What does it refer to in the list?
The field
int data
is an example of what you hold inside a node, an element that you actually care for in the list.
Let's say you want to keep a list of integers in given order. The data field is your integer (for example profit of your company in some preconfigured order), while the Node object encapsulates it and also holds a reference to the next object (in single linked list)
This may be a too-specific question, but I'm hoping there's a more general solution to my problem.
I have a class. In this class is a tree-type structure with many parent/child nodes. Also in this class is an Array filled with references to each node in this tree-type structure.
The tree's purpose is for each node to know where to draw itself on screen (every node has relative positional information based on its parent's location).
The Array's purpose is the draw order, I simply draw whatever node is referenced at Array[0] first and so on. (So, the nodes aren't being drawn in the order they appear in the tree necessarily).
My problem is this. I want to clone this overall class that contains these two objects (tree with nodes and an Array that references said nodes). This seems simple.
I create a deep copy of the tree structure and the nodes it contains. Cool.
However, I don't know how to repopulate a new Array with references to these new nodes in this new tree. It seems like it would be simple but I can't figure out how to do it.
I tried to be specific enough to give enough information without being too confusing, hope you understand.
Thanks.
If you're able to change the node data structure, you could add a field for the node's array index. That way, once you've rebuilt your tree you can just walk through it and use the index field to repopulate the array. Not super elegant, but it gets the job done...
Or, to avoid adding a field to your node class, I suppose you could use a temporary hashtable that maps nodes to array indices. Walk through your source array to populate the hashtable, then once you've cloned the tree, walk through the tree, looking up the new nodes in the hashtable (which will work fine assuming you've implemented equals and hashCode properly) and populating the array from those.
In a Java application I need an structure to store, lets call them nodes, and the number of relationship with other nodes. For example, I would need to know that node A is related with B 3 times.
Thinking in a way to implement this I got to this possible solution: Have a hashmap using nodes as key and another hashmap as value. This hashmap would store nodes as key (node B in the example) and an integer as value representing the number of relationships.
What do you think about this? Is it a good approach?
If so, I have a question. Suppose tha I store strings and they come from a text file after apply String.split function. Now I store "hello" in the first hashmap but after processing the file, this string appears as a destiny node in the second hashmap. Would these strings have a reference to the same object or I'll have multiple copies of the same objects?
Regarding the first question, I would do something similar but different. Instead of creating a Hashmap inside a Hashmap I would create a new class Relationship that looks something like this:
public class NodeRelationship {
private Node relatedNode;
private int numOfRelations
// Constructor + getters and setters
}
And define your map like this: Map<Node, List<NodeRelationship>> This seems more readable to me (but maybe it's just me) and easier to later expend. For example if you iterate on the list and want to know the original node you can add a member parent to NodeRelationshio and so on.
Regarding the second question - it depends on how you create your objects and whether you create new objects or use existing ones. If you have a node hello that you put in your value Hashmap (or in the List in my solution) and you use the same object for creating a new key - so there's no duplication. If you don't have a way (or just don't search for) to know that the node was already created, and you create new node - then you'll have duplications of objects.
If indeed your each of your nodes is created from text string, you can maintain a new Map<String, Node> and in the process of reading the file you can maintain this map and check if an object exists before creating a new one. This is very low cost in performance and you can get rid of the map once the construction of the objects from text is done.
I got a problem here, implementing a Sorted Doubly Linked List of first and last names.
Add a field to each link that indicates the alphabetically next first name; the existing next link is used to indicate the alphabetically next last name. You will also need a second root link for the list - the existing root link indicates the alphabetically first last name, and you will need one indicating the alphabetically first first name. Note that you will still only have one link object for each name entered in the list.
Having done this, make any necessary changes to your insert, lookup, and delete methods so that both interleaved lists are maintained. Also update the runtime estimates as required to remain accurate.
Finally, add a second lookup method that takes in a first name and returns all full names including that first name, and a second display method that prints the list of names out alphabetically by first name. Make sure you give runtime estimates for these methods as well.
And I am at a complete loss as how to do this. I already created a single linked list, with a first and last name, but that's as far as I was able to get.
Any Help would be great :D
Thank you.
Create a Link Class that has two link fields (nextFirstName, nextLastName) and a field for the Name Object.
On insert, first search the Link object that comes before (LastName) this new one and insert it after that using the nextLastName field. Then do the same with the FirstName, using the nextFirstName field as the link.
?????
profit!
This does smell a lot like homework :)
Because you have already implemented a Singly Linked List, expanding to a doubly linked list shouldn't be too difficult. You already have references going forward (see picture below). Now you need to add references going backward as well. In addition, note the blue lines in the picture below. Add additional references for the spelling properties. So each node will have the variables:
private Node NextFirstName;
private Node PreviousFirstName;
private Node NextLastName;
private Node PreviousLastName;
You do NOT need backward-references! The task asks for something like 2 single-linked Lists, that use the same objects as entrys. Every object has two links: one to the next item in list A, and one to the next item in list B.