I am writing a method that takes a function that takes a root of a linked list and the number of vertices in a graph and organizes the linked list into an array of linked list based on the first vertex of an edge struct, for example, in a struct where an node Edge has firVertex = 1, sndVertex = 2, and weight = 2, it would be sorted into the first element of an array. and another node Edge that has another firVertex = 1 would be appended onto the previous one, and etc. My code compiles but it's not exactly doing what I want. Please help.
You have many issues in your code.
While assigning the newNode to the elements of array list it should be
arrayList[j]= newNode; //not 'i' since i=numberVertices always....
When you are finding a firVertex matching the j, you are creating a temporary pointer which points to the current node. Then you are doing this:
newNode -> next = NULL;
It actually changes the next of the node in the given list to NULL, thus making the current->next as NULL and your code won't process the whole list. It should be instead as follows:
Edge* newNode = new Edge(current);//create a copy of the 'current' node..
newNode -> next = NULL;//now make the 'next' of this node NULL..
The above code prevents the list from any modifications. I also assume there is a constructor for Edge, which takes another Edge pointer as argument.
As you specified in the question, if you have two Edge nodes with same firVertex, you wanted to append them on the same list. But your code merely over-writes the previous node.
To prevent that, it can be written as follows:
Edge* newNode = new Edge(current);
newNode -> next = NULL;
Edge* tmp = arrayList[j];
if(tmp==NULL)//then this is the first node..
{
arrayList[j] = new Edge(current);
arrayList[j]->next = NULL;
}
else
{
while(tmp->next!=NULL)//go to the end of the list...
tmp = tmp->next;
tmp->next = new Edge(current);//create a copy of the 'current' node...
tmp->next->next=NULL;//make the next of this new node NULL...
}
Hope this helps...and I strongly feel that you need to learn more about pointers and then start coding.
Related
I need to create a function that takes a normal singly-linked list and creates another list in reverse order of the first list, the first element in the new list will be the last element in the original list and so on.
My function for some reason only ever returns the same number over and over for the entirety of the new list. Therefore, if the last number in my original list is e.g. '50', the new list will be made up entirely of '50's.
This is my code, what am I doing wrong? If anyone wants me to post the whole program for more clarity or context give me a shout.
void invert() {
node *list1=top,*newlist,*temp,*prev;
while (list1->next!=NULL) {
list1=list1->next;
}
newlist=new node;
newlist->num=list1->num;
newlist->next=NULL;
if (top2==NULL) {
top2=newlist;
}
else {
for (temp=top2;temp!=NULL;temp=temp->next) {
prev=temp;
}
prev->next=newlist;
}
list1->next=NULL;
}
Your code cannot be correct as it creates only a single new node and then modifies the next link in existing nodes. From your requirements, you have to create a new list which means cloning all nodes and linking them in the reverse order.
Following the suggestion of #user4581301, I came up with the following:
node* invert(node* list)
{
node* inverted = NULL;
// run through original in order
for (node* p = list; p != NULL; p = p->next)
{
// clone the node
node* newNode = new node();
newNode->num = p->num;
// and link it so that the predecessor in the original list
// (which has been processed in the previous iteration) is
// linked as next node
newNode->next = inverted;
inverted = newNode;
}
return inverted;
}
i want this function sortPair to take 2 Node pointers and return a pointer to a list of the 2 elements sorted alphabetically. The code below is what I have so far. If someone could let me know where I went wrong, that would be great.
struct Node{
string val;
Node* next;
};
Node* sortPair (Node* p1, Node* p2){
//Assert that neither pointer is null
assert(p1!=NULL);
assert(p2!=NULL);
Node* head=NULL;
Node* current=NULL;
Node* last = NULL;
current = new Node();
if(p1-> val >p2-> val) //If p1->val comes before p2->val in the alphabet
{
current->val = p1->val;
head = current;
last = current;
current = new Node();
current -> val = p2->val;
last = current;
last ->next = NULL;
}
else
{
current->val = p2->val;
head = current;
last = current;
current = new Node();
current -> val = p1->val;
last = current;
last ->next = NULL;
}
return head;
}
A linked list is just a series of nodes that are linked by each element having a pointer to the next one.
From your code, it seems like you do not want to make a list of the two nodes, but rather insert a node into a list that already exists.
If all you want to do is to make a linked list of the two nodes that are there, then set the one with the lower or higher value, depending on how you sort them, to point at the other one. For example, if you are sorting from smallest to biggest, set the smallest node's next pointer to point to the bigger one and return the pointer of the smallest one.
If you want to add one of the nodes into a list of nodes, then you must loop through the list until you find one that is larger or smaller than the node you want to insert. I recommend using a while loop.
If you want to merge two lists, then you must make a new loop that inserts each element of one list into the other list.
There is no need to make a new node for any of this, just use the ones you have and change the next pointers.
Hope this helps.
I'm having a problem figuring out how to set my ptr's for prev and next when I add a node with at least one existing node. Adding the first node is easy, just setting the ptrs to the front. I need help viewing this mentally, also this program is a queue so each node gets added to the back of the list.
if (Empty())
{
front = new qnode;
front->next=front;
front->prev=front;
front->data = item;
}
else if (front->prev=front)
{
front->prev = new qnode;
front->prev->next= front;
front->next=front->prev;
front->prev->data = item;
}
else
{
}
What i have now still not getting it
else
{
front->prev= new qnode;
front->prev->data= item;
front->prev->next=front;
front->prev=front->prev->prev;
}
I hope this image helps a bit
I have created pictures for 1 item 2 items and 3 items
the pointers just point to the actual object meaning the black rectangle is the whole object if front being blue and prev being brown(those are just there as references)
I really hope this helps linked list can get really tricky and drawing always helps me.
so to add the item at the front of the list you have some code like this:
//ok first I'll define some variables for you
//last === the last node in the list
//head === the first node in the list
//node === the new node you are adding;
qnode node = new qnode;
node.data = data; //whatever data you are holding
node->next = last; //last element in the list since it is circular;
node->prev = head; //you want the new node to point the the first node since it's getting added before that;
head->next = node; //you want the head of the node to point to the new node not the last item
last->prev = node; //last node now should point to the new node you just added not the head;
I am not sure why you need an else if, as I think the case with one element is not different from that with multiple elements. In any case you have a syntax error there - you wanted to write == instead of =.
More to the point, what you want to do is:
Create a new qnode
Set the data of the new node to the item
Set the next of the new node to the front
Set the prev of the new node to front->prev
Set the prev of the front to the new node.
You may check for yourself on a piece of paper how this works.
In a circular doubly linked list, you have 2 pointers, one to the head and one to the tail (last element in the list). If a element has a next, the next element's prev should be the element pointing at it through it's next element.
Your head pointer should never move unless the head element is removed. Your tail will move along always ensuring that the element it points to has head as it's next element.
Hopefully this helps
Since you always add at the end, the case of a queue with a single element is identical with the case of a list with multiple elements. Take a paper and a pen, make some sketches and it will come very easy to you.
I'm trying to sort a linked-list. I have a Node called head and it's pointing to the next node, etc.
But when I try to sort the Nodes by the value they carry, I get the sort working because I see it printing out the stuff in the if-statement, but I'm not getting back the linked-list. Where did I go wrong?
Node* head;
void sortlist(){
Node * runner = head;
Node * runner2;
for(runner = head; runner->next != NULL; runner = runner->next){
for(runner2 = runner->next; runner2->next != NULL; runner2 = runner2->next){
if(runner->freq < runner2->freq){
cout<< runner->freq<< " is LT "<<runner2->freq<< endl;
Node * temp = runner;
runner = runner2;
runner2 = temp;
}
}
}
head = runner;
}
I'm only getting back the first Node.
In order to swap two elements in a linked list, consider what you need to change. For example, to get from
Head -> First -> Second -> (NULL)
to
Head -> Second -> First -> (NULL)
you need to update: Head.next, First.next and Second.next. You don't change any of those things when trying to swap nodes, so it can't possibly do what you expect.
Just swapping the values (ie, swap(runner->freq, runner2->freq)) would be much simpler.
you will stop when runner->next == NULL;, which is supposed to be the last element. And then you set head = runner;, which means the head will always be the last element after this routine. Furthermore, I do not trust this swapping.
It seems you vaguely want to do an insertion sort. If you want to do a simple sorting on linked lists, I suggest you to use selection sort: You create another empty list l2, and each time you remove the minimum element from your first list, and add it as the head of l2. The code for the second list is simple:
void prepend(Node* node, Node** list){
//null checks if you want
node->next = *list;
*list=node->next;
}
I have a linked list contains 3 nodes like the image shown:
There is a head pointer and temp1 pointer point to the front of the list, and tail point points at the end of the list.
I want to remove all the nodes, and change it back to its original initial form ( tail = NULL, head = first_node , but the first node doesn't have any value in the data and next field).
Because I want to start putting up some new values in it. To remove all those data, is this code going to remove nodes inside this linked list and left with the first node with no values in data and next field?
This code is in C++:
while(temp1!=tail)
{
temp1 = temp1->next;
if(temp1->next == tail)
{
tail=temp1;
temp1 = temp1->next;
free(temp1);
}
}
But then, does this mean only the last node will be deleted? are there any way to delete all the nodes except the first one?
To delete all nodes except the first node, you can try below code.
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
This will delete all nodes except first one. But the data with the first node will remain as it is.
Disclaimer: I assume it's only for learning purposes and in real-world scenario you would use std::list<> or similar container.
For single-linked list, you can just drop all this burden an let the stdlib manage the pointers:
class Node {
std::unique_ptr<Node> next;
};
You can safely use .reset() method to make operations on the list:
Given current_ptr, the pointer that was managed by *this, performs the following actions, in this order:
Saves a copy of the current pointer old_ptr = current_ptr
Overwrites the current pointer with the argument current_ptr = ptr
If the old pointer was non-empty, deletes the previously managed object if(old_ptr != nullptr) get_deleter()(old_ptr).
From http://en.cppreference.com/w/cpp/memory/unique_ptr/reset.
And that's pretty much what you would do when deleting. I believe you can also use unique_ptr::swap(), to easily manipulate your nodes.
Instead of free, C++ uses delete function.
Check the link to have deep knowledge about all kind of operations(including recursive or iterative delete) on linked lists.
temp1 = head->next;
while(temp1!=NULL) // as I am considering tail->next = NULL
{
head->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = head->next;
}
The logic for this would be more correct if it is this way.
After the statement
free(temp1);
Add the condition
if (head -> next != NULL)
temp1 = head->next;
Since after deleting the last node there is no point in reassigning the address of head pointer to temp1.