Adding multiple data attributes to one node
I need a way to add more than one data ( such as name, id, age) into a single node in a linked list in C++. Instead of having the data value being only a name or a number.
1 answer
-
answered 2022-05-04 13:41
Blue
I think you wish to group your data, there are many ways to do that. The easiest is if you create a stucture:
struct MyData { int id; std::string name; int age; }; MyData data; data.id = 1; data.name = "John"; data.age = 23; std::list<MyData> list; list.push_back(data); ... std::list<MyData>::const_iterator itr = list.begin(); int age = itr->age;
Would that help?
do you know?
how many words do you know
See also questions close to this topic
-
C++ increment with macro
I have the following code and I want to use increment with macro:
#include <iostream> #define ABS(x) ((x) < 0 ? -(x) : (x)) int main(int argc, char** argv) { int x = 5; const int result = ABS(x++); std::cout << "R: " << result << std::endl; std::cout << "X: " << x << std::endl; return EXIT_SUCCESS; }
But output will be incorrect:
R: 6 X: 7
Is it possible to somehow use macros with an increment, or should this be abandoned altogether?
-
Can anyone pls tell me whats wrong with my code ? im stuck for the last 3 hours ,this question is bipartite graph in c++
idk why im getting error ,can someone help ? im trying to prove if a graph is bipartite or not in c++
bool isBipartite(vector<int> graph[],int V) { vector<int> vis(V,0); vector<int> color(V,-1); color[0]=1; queue <int> q; q.push(0); while (!q.empty()) { int temp = q.front(); q.pop(); for (int i=0;i<V;i++) { if (!vis[i] && color[i] == -1) "if there is an edge, and colour is not assigned" { color[i] = 1 - color[temp]; q.push(i); vis[i]=1; } else if (!vis[i] && color[i] == color[temp] "if there is an edge and both vertices have same colours" { vis[i]=1; return 0; // graph is not bipartite } } } return 1; }
it gives output "no" for whatever i enter
-
How to assign two or more values to a QMap Variable in Qt
I am getting confused of how to store the values assigned from 3 different functions and storing them in a single map variable
QMap<QString,QString> TrainMap = nullptr; if(......) ( TrainMap = PrevDayTrainMap(); TrainMap = NextDayTrainMap(); TrainMap = CurrentDayTrainMap(); }
The PrevDayTrainMap,NextDayTrainMap & CurrentDayTrainMap returns a set of values with Date and the TrainIdName.I need to store all the values from prevday,currentday and nextday in the TrainMap but it stores only the currentday values to the TrainMap as it is assigned at the last.I am not sure what to do so that it doesn't overwrite.If I should merge what is the way to do it?
-
Linked List Deletion function causing program to crash *wrong pointers?*
In my program when a username is following another one and also has posted a "tweet" which is a string in an attempt to delete the followed username it breaks the program. I believe this is the code snippet in question. If anyone could help me identify the issue I would be thankful as I am learning the usage of linked lists for scale able projects. Thanks
void deleteAccount(accountNodePtr *startPtr, accountNodePtr *curAcPtr, tweetsNodePtr *startTwtPtr){ accountNodePtr acLoopPtr; accountNodePtr tempCur = *curAcPtr; followNodePtr followingPtr = tempCur->followingPtr; followNodePtr tempPtr; followNodePtr tempPtr2; tweetsNodePtr iterTwtPtr; iterTwtPtr = *startTwtPtr; *below here in question* tempCur = *curAcPtr; while (tempCur->followersPtr!=NULL){ acLoopPtr = *startPtr; while (strcmp(acLoopPtr->username, tempCur->followersPtr->username)!=0){ acLoopPtr=acLoopPtr->nextPtr; } if (strcmp(acLoopPtr->followingPtr->username, tempCur->username)==0){ tempPtr=acLoopPtr->followingPtr->nextPtr; free(acLoopPtr->followingPtr); acLoopPtr->followingPtr=tempPtr; }else{ tempPtr=acLoopPtr->followingPtr; while(strcmp(tempPtr->nextPtr->username, tempCur->username)!=0){ tempPtr=tempPtr->nextPtr; } tempPtr2=tempPtr->nextPtr->nextPtr; free(tempPtr->nextPtr); tempPtr->nextPtr=tempPtr2; } tempPtr = tempCur->followersPtr->nextPtr; free(tempCur->followersPtr); tempCur->followersPtr=tempPtr; }
This is the structure
typedef struct followsNode { char username[MAX_USERNAME]; struct followsNode *nextPtr; } followNode; typedef struct accountsNode { char username[MAX_USERNAME]; struct followsNode *followersPtr; struct followsNode *followingPtr; struct accountsNode *nextPtr; } accountNode; typedef followNode *followNodePtr; typedef accountNode *accountNodePtr;
-
ANTLR function to retrieve text from a single node in a parse tree
I want to retrieve text from a parse tree's node but when i use getText(), it retrieves all the text from that node's children. I have searched but i don't find any ANTLR function that can retrieve only the node's text or a function to delete a node's children (so that i can use getText() on that node once i have deleted his children). Any help is welcomed.
-
Blender Viewer Nodes showing different values for the same vectors depending on the geometry attached to them?
In Blender (3.1.2) I tried to connect the "corresponding" points of two curve-circles with the same center (at world-origin) with cones, like the beams of a symbolic sun with the inner circle being the circumference of the sun-disc and the outer circle being the circumference of the tip of the beams. This is just an exercise to later do the same with curves of different shape, for instance between an inner circle and an outer ellipse.
Therefore I wanted to calculate the length of the vectors between corresponding inner and outer points to scale the cones accordingly. However, I found that my simple vector math don't work as I expected.
I created a simplified scenario without the cones to demonstrate the issue I'm facing. The upper and lower half of the Geometry-Nodes tree attached as screenshot #1 are identical. The upper part is for the outer circle with radius 2, the lower part for the inner circle with radius 1. Both circles have only 4 points for simplicity (so they appear as rhombs).
In the 5 boxes I do the following:
Create the circle-curves, convert them to points and capture the point positions.
Subtract the positions of corresponding points to get the relative vectors pointing from the outer to the inner circle's points. The result should consist of a list of the 4 vectors [(1,0,0)(0,1,0)(-1,0,0)(0,-1,0)] with all having length 1 due to the radius difference of 1.
Put small Ico-Spheres to the points of both circles by Instance-to-Points nodes. Here is where also the cones would be instantiated, but on just the inner circle.
Group-output the geometry of the circles and ico-spheres.
Here comes my issue, displayed by 4 Viewer-Nodes:
a) The value-inputs of the two viewer-nodes in the middle are attached to the same vector array calculated in step 2. So I would expect their output to be identical in the corresponding spread-sheets, independent of the geometry the viewer nodes are attached to.
b) The geometry-sockets of the top two viewer-nodes are attached to the ico-sphere instances on the outer circle, with the top one showing the positions of their points captured in step 1.
c) The geometry-sockets of the bottom two viewer-nodes are attached to the ico-sphere instances on the inner circle, with the one on the very bottom showing the positions of their points also captured in step 1.
My expectation would have been the following outputs:
a) From the top-most viewer node (points on outer circle): (2,0,0)(0,2,0)(-2,0,0)(0,-2,0)
b) From the bottom-most (points on inner circle): (1,0,0)(0,1,0)(-1,0,0)(0,-1,0)
c) From the two at the center (attached to the same vector-subtract node): (1,0,0)(0,1,0)(-1,0,0)(0,-1,0)
However, the outputs of the center viewer-nodes differ depending on which geometry is attached to them (see screenshot #2). This leads to also different length, while all 4 vectors should be of length 1 in my opinion.
Output of the 4 viewer-nodes with unexpected different values by the center nodes
Where is my fallacy of thinking, and how can I actually calculate the distance between corresponding points on the circles for later scaling of cones between them?
For completnes here screenshot #3 with my entire Blender screen:
Overview screen with geometry output
Thank's in advance!
-
I'm having a problem creating a linked list
#include<stdio.h> #include<stdlib.h> void insert_front(struct node* head, int block_number); void insert_rear(struct node* head, int block_number); void print_list(struct node* head); struct node { int block_number; struct node* next; }; int main(void) { struct node* list = NULL; insert_front(list, 10); insert_rear(list, 20); insert_front(list, 30); insert_rear(list, 40); print_list(list); return 0; } void insert_front(struct node* head, int block_number) { struct node* p = malloc(sizeof(struct node)); p->block_number = block_number; p->next = head; head = p; return head; } void insert_rear(struct node* head, int block_number) { struct node* p = malloc(sizeof(struct node)); p->block_number = block_number; p->next = NULL; if (head == NULL) { head = p; } else { struct node* q = head; while (q->next != NULL) { q = q->next; } q->next = p; } } void print_list(struct node* head) { struct node* p = head; while (p != NULL) { printf("--> %d ", p->block_number); p = p->next; } printf("\n"); }
When I ran it, there was no result at all.
Now, in the insert_front function p->block_number = block_number, a message appears saying that the NULL pointer 'p' is being dereferenced... (The same thing appears in the insert_rear function.)
Could it be that I am declaring the pointer wrong?
-
How __iter__ works for printing linked list
I'm trying to learn linked lists and I'm stuck at the point where the instructor wrote down the
__iter__
method.Although I particularly don't understand the part where it says
while node
,node = node.next
andprint(node.value for node in singlyLinkedList)
I would be very grateful if you could explain__iter__
and print parts in detail.# step 1: defining class for creating head and tail pointers class SLinkedList: def __init__(self) -> None: self.head = None self.tail = None # we cannot print this bcoz this is a custom data structure. # so to be able to print this, we need to define # a helper function def __iter__(self): node = self.head while node: yield node node = node.next # step 2: class for creating node class Node: def __init__(self, value=None) -> None: self.value = value self.next = None # step 3: assign value (create nodes) singlyLinkedList = SLinkedList() node1 = Node(1) node2 = Node(2) # step 4: create link between nodes and head & tail singlyLinkedList.head = node1 singlyLinkedList.head.next = node2 singlyLinkedList.tail = node2 print(node.value for node in singlyLinkedList)
-
Circular linked list not working after appending 3rd node
It's Showing output for 2 nodes but after adding 3rd node it doesn't showing any output, what i'm doing anything wrong?? This is the append method that i have written all the other program have no errors i have tested all the things.
void append(nd element) { nd temp = head; nd temp1 = null; while(temp != null) { temp1 = temp; temp = temp.nxt; } element.nxt = head; temp1.nxt = element; } void printCLL() { nd temp = head; do { System.out.print(temp.data+" --> "); temp = temp.nxt; } while(temp != head); } public static void main(String ar[]) { CLList clobj = new CLList(); nd nn1 = new nd(10); clobj.head = nn1; //working nd nn2 = new nd(20); clobj.append(nn2); //working nd nn3 = new nd(30); clobj.append(nn3); //not working nd nn4 = new nd(40); clobj.append(nn4); //not working nd nn5 = new nd(25); clobj.insertAfter(nn1, nn5); //this method also working if node added System.out.println("Printing Circular LinkedList : "); clobj.printCLL(); }