Advertisement
Guest User

Übung 1 b

a guest
Jan 23rd, 2020
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<stdlib.h>
  3.  
  4. struct Node
  5. {
  6. int data;
  7. struct Node* next;
  8. };
  9.  
  10. static void reverse(struct Node** head_ref)
  11. {
  12. struct Node* prev = NULL;
  13. struct Node* current = *head_ref;
  14. struct Node* next;
  15. while (current != NULL)
  16. {
  17. next = current->next;
  18. current->next = prev;
  19. prev = current;
  20. current = next;
  21. }
  22. *head_ref = prev;
  23. }
  24.  
  25. void push(struct Node** head_ref, int new_data)
  26. {
  27. struct Node* new_node =
  28. (struct Node*) malloc(sizeof(struct Node));
  29.  
  30. new_node->data = new_data;
  31.  
  32. new_node->next = (*head_ref);
  33.  
  34. (*head_ref) = new_node;
  35. }
  36.  
  37. void printList(struct Node *head)
  38. {
  39. struct Node *temp = head;
  40. while(temp != NULL)
  41. {
  42. printf("%d ", temp->data);
  43. temp = temp->next;
  44. }
  45. }
  46.  
  47. int main()
  48. {
  49. struct Node* head = NULL;
  50.  
  51. push(&head, 12);
  52. push(&head, 13);
  53. push(&head, 15);
  54. push(&head, 14);
  55.  
  56. printf("Given linked list\n");
  57. printList(head);
  58. reverse(&head);
  59. printf("\nReversed Linked list \n");
  60. printList(head);
  61. getchar();
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement