Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- //Defining a structure of the node
- struct node {
- int data;
- struct node* next;
- };
- void insert (struct node* rec, int x) {
- struct node* temp = (struct node*)malloc(sizeof(struct node));
- temp->data = x;
- temp->next = NULL;
- rec = temp; // head and rec is now pointing to the same node
- }
- void print(struct node* rec){
- printf("%d", rec->data); //error occurs here
- puts("");
- }
- main(){
- struct node *head = NULL; //head is currently pointing to NULL
- insert (head, 5); //Passing the head pointer and integer 5 to insert()
- print(head);
- }
- /* returns the new head of the list */
- struct node *insert (struct node* current_head, int x) {
- struct node* temp = (struct node*)malloc(sizeof(struct node));
- temp->data = x;
- temp->next = current_head;
- return temp;
- }
- head = insert(head, 5);
- void insert(struct node **head, int x) {
- struct node* new_head = (struct node*)malloc(sizeof(struct node));
- new_head->data = x;
- new_head->next = *head;
- *head = new_head;
- }
- void insert (struct node** rec, int x) {
- struct node* temp = (struct node*)malloc(sizeof(struct node));
- temp->data = x;
- temp->next = NULL;
- *rec = temp; // head and rec is now pointing to the same node
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement