Advertisement
Vladpepe

Untitled

Oct 25th, 2018
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.99 KB | None | 0 0
  1. #define _CRT_SECURE_NO_WARNINGS
  2. #include "stdlib.h"
  3. #include "stdio.h"
  4.  
  5. struct Node {
  6.     int data;
  7.     struct Node* next;
  8. };
  9.  
  10. void Print(struct Node* p) {
  11.     if (p == NULL) return;
  12.     printf("%d", p->data);
  13.     Print(p->next);
  14. }
  15.  
  16.  
  17. struct Node* Insert(struct Node* head,int data) {       //insert in the end
  18.     struct Node* tmp = malloc(sizeof(struct Node));
  19.     if (head == NULL) {
  20.         tmp->data = data;
  21.         tmp->next = NULL;
  22.         head = tmp;
  23.     }
  24.     else {
  25.         struct Node* temp = head;
  26.         tmp->next = NULL;
  27.         while (temp->next != NULL) {
  28.             temp = temp->next;
  29.         }
  30.         temp->next = tmp;
  31.         tmp->data = data;
  32.     }
  33.     return head;
  34. }
  35. struct Node* head;
  36. void Reverse(struct Node* p) {
  37.    
  38.     if (p->next == NULL) {
  39.         head = p;
  40.         return;
  41.     }
  42.     Reverse(p->next);
  43.     struct Node* q = p->next;
  44.     q->next = p;
  45.     p->next = NULL;
  46. }
  47.  
  48. int main() {
  49.     struct Node* head = NULL;
  50.     head = Insert(head,2);
  51.     head = Insert(head, 4);
  52.     head = Insert(head, 6);
  53.     head = Insert(head, 5);
  54.     Print(head);
  55.     Reverse(head);
  56.     Print(head);
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement