fork download
  1. #include <iostream>
  2. using namespace std;
  3. struct node
  4. {
  5. int data;
  6. struct node *next;
  7. };
  8.  
  9. void push(struct node** head_ref, int new_data)
  10. {
  11. struct node* new_node = (struct node*) malloc(sizeof(struct node));
  12. new_node->data = new_data;
  13. new_node->next = (*head_ref);
  14. (*head_ref) = new_node;
  15. return;
  16. }
  17.  
  18. void printList(struct node *node)
  19. {
  20. while (node != NULL)
  21. {
  22. printf(" %d ", node->data);
  23. node = node->next;
  24. }
  25. return;
  26. }
  27.  
  28. int main() {
  29. struct node* head = NULL;
  30.  
  31. push(&head, 7);
  32. push(&head, 1);
  33. push(&head, 3);
  34. push(&head, 2);
  35. push(&head, 8);
  36. printList(head);
  37. return 0;
  38. }
Success #stdin #stdout 0s 16048KB
stdin
Standard input is empty
stdout
 8  2  3  1  7