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

  • SEARCH
  • Home
  • 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 6968081
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T16:24:19+00:00 2026-05-27T16:24:19+00:00

typedef struct vertex{ int num; struct vertex *next; } Vertex; Vertex *adj[1]; void buildList(){

  • 0
typedef struct vertex{
    int num;
    struct vertex *next;
} Vertex;

Vertex *adj[1];

void buildList(){
    Vertex *v=NULL;
    Vertex *t=NULL;

    v = malloc(1*sizeof(*v));
    v->num = 1;
    adj[0] = v;  //a NODE with value 1
    t = v;

    v = malloc(1*sizeof(*v));
    v->num = 1;
    t->next = v; // and ANOTHER NODE but it should be the SAME NODE with the above one
    t = v;


    //v = malloc(1*sizeof(*v));
    //v->num = 1;
    //t->next = adj[0]; // causes infinite loop...
    //t = v;
}

The expected output is a node with value 1 having itself in its adjacency list, an output like 1 -> 1.

Here my problem is it looks like I have two different nodes. When I made a change on one of them the other doesn’t change, acting like an another node.

For instance after building the list if I change the value of the node I should get an output like 3 -> 3. But I get 3 -> 1. The change on the node doesn’t affect the other one. When I try to point adj[0] to t->next however I get an infinite loop…

  • 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-05-27T16:24:20+00:00Added an answer on May 27, 2026 at 4:24 pm

    It’s not entirely clear to me what you want. If you want a Vertex pointing to itself, it’s simply

    void buildList(){
        adj[0] = malloc(1*sizeof(*adj[0])); // allocate memory for one Vertex
        if (adj[0] == NULL){
            perror("Allocation of Vertex failed\n");
            exit(EXIT_FAILURE);
        }
        // adj[0] contains the adress of a Vertex
        // set the num of the Vertex
        adj[0]->num = 1;
        // set the next pointer of the Vertex to its address
        adj[0]->next = adj[0];
    }
    

    Can you clarify in what way this is not what you want?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

typedef struct node { struct node *leftChild, *rightChild; int value; } bst; void insert(bst*
typedef struct Value{ int id; char type; char a; } Value; void fooV(Value v){
//CHILD typedef struct Child{ int id; }Child; Child* newChild(){ Child *aChild = malloc(sizeof(Child)); aChild->id
I have these structs: typedef struct _Frag{ struct _Frag *next; char *seq; int x1;
typedef struct dict_pair { void *key; void *value; struct dict_pair *head; struct dict_pair *tail;
typedef struct _stResult { std::string x; int y; struct _stResult *next; } strResult; In
typedef struct _protocol1 { int type; CGPoint pos; } Protocol1; -(void)sendData { NSError *error;
typedef struct hash_table_data { int key; int data; struct hash_table_data* next; struct hash_table_data* prev;
typedef struct _DListNode { struct _DListNode* prev; struct _DListNode* next; void* data; }DListNode; in
typedef struct { nat id; char *data; } element_struct; typedef element_struct * element; void

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.