Advertisement
Guest User

Untitled

a guest
Apr 19th, 2019
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.76 KB | None | 0 0
  1. Node* createAnEmptyList() {
  2. Node* head = (Node*)malloc(sizeof(Node));
  3. //the head->val is never used
  4. head->next = NULL;
  5. //head->next will store the pointer to the first elem of the list
  6. return head;
  7. }
  8.  
  9. Node* createLinkedList(int n) {
  10.  
  11. Node* head = createAnEmptyList();
  12. Node* tail = head;
  13. for (int i = 0; i < n; i++) {
  14. head->next = (Node*)malloc(sizeof(Node));
  15. head = head->next;
  16. scanf("%d", &(head->value));
  17. head->next = NULL;
  18. }
  19. return tail;
  20. }
  21.  
  22. void pushBack(Node* head, int val) {
  23. while (head->next != NULL)
  24. head = head->next;
  25.  
  26. head->next = (Node*)malloc(sizeof(Node));
  27. head->next->value = val;
  28. head->next->next = NULL;
  29. }
  30.  
  31. void popFront(Node* head) {
  32. Node* tmp = head->next;
  33. head->next = head->next->next;
  34. free(tmp);
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement