Quantcast

A community for students. Sign up today!

Here's the question you clicked on:

55 members online
  • 0 replying
  • 0 viewing

Rdx

  • 8 months ago

I was studying linked list. I am not able to understand what is node*, if it is a pointer why not write *node? How does it work?

  • This Question is Closed
  1. eSpeX
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 1

    To start with, some contextual code would be helpful. Using the notation 'node*' implies that you are defining a pointer to an object of type 'node', for example, you create a node object, then you want to declare a pointer to the new object like: node* a_node; The other way is when you wish to dereference that object, in your example the object is named "node".

  2. theEric
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 1

    Here's some info on pointers: http://www.cplusplus.com/doc/tutorial/pointers/ The idea of nodes is that they contain some information AND a "link" to another node that has its information and a link to another node. Well, the last node in a list won't have a link, because there's nothing else to go to. Anyway, these "links" as I like to think of them, are actually one-way links. And that makes them harder to think about. It's more like a node that has a one way road to get to the next node. That is what I mean by a link. A great way to do that is with a pointer. A pointer is just some numbers to the computer that tells it where to find something. So, in your node, you have a piece of data that tells the computer how to get to the node. If you want to make a pointer to a node, you'll see that asterisk. Like, `node * name_of_node_pointer;` like `node * nodep;` That's why you see `node*`. To get the value at that pointer, then, you use `* nodep`, which you can use as if it was that node, I think. Now you have a node, that can be as simple as just one piece of data and pointer to another noded. You can link a bunch of nodes together, and then you'll have a linked list. Just, in a linked list, you have to keep track of the first one! Later you might move on to doubly linked lists. Then you will have to pointers. One pointer can be for the next node, and another for the previous node. That might work better for some algorithms, but you can really do a lot using a singly linked list (that has just one link).

  3. Rdx
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 0

    #include <stdio.h> #include <stdlib.h> typedef struct node { int val; struct node * next; } node_t; void print_list(node_t * head) { node_t * current = head; while (current != NULL) { printf("%d\n", current->val); current = current->next; } } int pop(node_t ** head) { int retval = -1; node_t * next_node = NULL; if (*head == NULL) { return -1; } next_node = (*head)->next; retval = (*head)->val; free(*head); *head = next_node; return retval; } int remove_by_value(node_t ** head, int val) { /* TODO: fill in your code here */ } int main() { node_t * test_list = malloc(sizeof(node_t)); test_list->val = 1; test_list->next = malloc(sizeof(node_t)); test_list->next->val = 2; test_list->next->next = malloc(sizeof(node_t)); test_list->next->next->val = 3; test_list->next->next->next = malloc(sizeof(node_t)); test_list->next->next->next->val = 4; test_list->next->next->next->next = NULL; remove_by_value(&test_list, 3); print_list(test_list); }

  4. Rdx
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 0

    this was the code i got it thanks ppl :)

  5. Rdx
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 0

    thanks @eSpeX and @theEric

  6. theEric
    • 8 months ago
    Best Response
    You've already chosen the best response.
    Medals 1

    You're welcome! :)

  7. Not the answer you are looking for?
    Search for more explanations.

    • Attachments:

Ask your own question

Ask a Question
Find more explanations on OpenStudy

Your question is ready. Sign up for free to start getting answers.

spraguer (Moderator)
5 → View Detailed Profile

is replying to Can someone tell me what button the professor is hitting...

23

  • Teamwork 19 Teammate
  • Problem Solving 19 Hero
  • You have blocked this person.
  • ✔ You're a fan Checking fan status...

Thanks for being so helpful in mathematics. If you are getting quality help, make sure you spread the word about OpenStudy.

This is the testimonial you wrote.
You haven't written a testimonial for Owlfred.