Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8706005
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:30:21+00:00 2026-06-13T03:30:21+00:00

I have created two structs: typedef struct mainNode { int theNode; int visited; struct

  • 0

I have created two structs:

typedef struct mainNode {
    int theNode;
    int visited;
    struct mainNode *next;
} node_rec, *node_ptr;

and

typedef struct neighborNode {
    struct neighborNode *next;
} neighbor_node_rec, *neighbor_node_ptr;

which I need to insert into a list:

void insert(node_ptr head, int theNodeVal, int neighborNodeVal, int weight) {

//if list is empty
if (head == NULL) {
    head = (node_ptr) malloc(sizeof (node_rec)); //create head of list
    head->theNode = theNodeVal; //set head value to node value
    head->next = NULL; //point to null       
}

//while list is not pointing no null
while (head != NULL) {
    //if node IS NOT equal to node value   
    if (head->theNode != theNodeVal) {
        head->theNode = theNodeVal; //set head value (new node) to node value
        head->next = tail; //connect to next node
        tail->next = NULL; //point to null
    }
    else
    {          
        //if node IS equal to node value (the node already exists)
        tail->next = head // head is the new tail
        head->neighbor = n; //point at the neighbor of head (new tail)
    }
}
}

I’m trying to see if the logic I implemented is correct. That is the reason I commented every line. You can refer to the link at the top of the page for a visual.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T03:30:23+00:00Added an answer on June 13, 2026 at 3:30 am

    Replace the if (head->theNode = theNodeVal) { with if (head->theNode == theNodeVal) {. Would it help?

    This is how I would implement the graph for this algorithm:

    #include <limits.h>
    #include <string.h>
    #include <malloc.h>
    #include <stdio.h>
    
    struct Vertex {
        struct Vertex *next; // The reference to the next vertex in the global list of vertices
        int nodeId;
        int distance;
        struct Vertex *previous; // The reference to trace back the shortest path;
        struct Edge *neighbor; // The reference to the list of edges of this 
    };
    
    struct Edge {
        struct Edge *next;   // The reference to the next edge of this vertex;
        struct Vertex *node; // The reference to the vertex at other end of this edge;
        int weight;
    };
    
    
    struct Vertex *head = NULL;
    
    struct Vertex *newVertex(int nodeId) {
        struct Vertex *p = malloc(sizeof(struct Vertex));
        memset(p,0,sizeof(struct Vertex));
        p->nodeId = nodeId;
        p->distance=INT_MAX;
        p->next = head;
        head = p;
        return p;
    }
    
    void addEdge(struct Vertex *v1, struct Vertex *v2, int weight) {
        struct Edge *e = malloc(sizeof(struct Edge));
        e->next = v1->neighbor;
        v1->neighbor = e;
        e->node = v2;
        e->weight = weight;
    }
    
    void insert(int node1, int node2, int weight) {
        struct Vertex *current = head, *p1 = NULL, *p2 = NULL;
    
        while (current != NULL) {
            if (current->nodeId == node1) p1 = current;
            if (current->nodeId == node2) p2 = current;
            current = current->next;
        }
    
        if (p1 == NULL) p1 = newVertex(node1);
        if (p2 == NULL) p2 = newVertex(node2);
    
        addEdge(p1,p2,weight);
        addEdge(p2,p1,weight);
    }
    
    void main(int argc, char **argv) {
        int node1, node2, weight;
        while (EOF != scanf("%d %d %d \n", &node1, &node2, &weight)) {
            insert(node1, node2, weight);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have three structs, one of which inherits from the other two: typedef struct
I have two classes test1 and test2: struct test1 { int r; test1() {}
I have two structs containing some fields: struct MyNodeData, and struct MyEdgeData. When I
I have two vectors of predicates: typedef std::function<bool(int)> pred; vector<pred> v1; vector<pred> v2; I
I have created two imageViews promatically as shown below: public void createImageViews(Integer count){ ImageView[]
I have created two WindowSurface (WPF), and I want to navigate betwen them. I've
I have created two tables & inserted values as shown below . Table 1
I have created two Pages Page1.xaml and Page2.xaml in windows8 metro apps. I have
I have created two DbContexts, one is for application configuration, the second is for
I have created two folders layout and layout-land with two xml files, one for

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.