Here's the question you clicked on:
Rdx
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?
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".
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).
#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); }
this was the code i got it thanks ppl :)