Advertisement
Guest User

Untitled

a guest
Aug 29th, 2015
88
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.25 KB | None | 0 0
  1. #include <stdio.h>
  2. //Defining a structure of the node
  3. struct node {
  4. int data;
  5. struct node* next;
  6. };
  7.  
  8. void insert (struct node* rec, int x) {
  9. struct node* temp = (struct node*)malloc(sizeof(struct node));
  10. temp->data = x;
  11. temp->next = NULL;
  12. rec = temp; // head and rec is now pointing to the same node
  13. }
  14.  
  15. void print(struct node* rec){
  16. printf("%d", rec->data); //error occurs here
  17. puts("");
  18. }
  19.  
  20. main(){
  21. struct node *head = NULL; //head is currently pointing to NULL
  22. insert (head, 5); //Passing the head pointer and integer 5 to insert()
  23. print(head);
  24. }
  25.  
  26. /* returns the new head of the list */
  27. struct node *insert (struct node* current_head, int x) {
  28. struct node* temp = (struct node*)malloc(sizeof(struct node));
  29. temp->data = x;
  30. temp->next = current_head;
  31. return temp;
  32. }
  33.  
  34. head = insert(head, 5);
  35.  
  36. void insert(struct node **head, int x) {
  37. struct node* new_head = (struct node*)malloc(sizeof(struct node));
  38. new_head->data = x;
  39. new_head->next = *head;
  40.  
  41. *head = new_head;
  42. }
  43.  
  44. void insert (struct node** rec, int x) {
  45. struct node* temp = (struct node*)malloc(sizeof(struct node));
  46. temp->data = x;
  47. temp->next = NULL;
  48. *rec = temp; // head and rec is now pointing to the same node
  49. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement