Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdlib.h"
- #include "stdio.h"
- struct Node {
- int data;
- struct Node* next;
- };
- void Print(struct Node* p) {
- if (p == NULL) return;
- printf("%d", p->data);
- Print(p->next);
- }
- struct Node* Insert(struct Node* head,int data) { //insert in the end
- struct Node* tmp = malloc(sizeof(struct Node));
- if (head == NULL) {
- tmp->data = data;
- tmp->next = NULL;
- head = tmp;
- }
- else {
- struct Node* temp = head;
- tmp->next = NULL;
- while (temp->next != NULL) {
- temp = temp->next;
- }
- temp->next = tmp;
- tmp->data = data;
- }
- return head;
- }
- struct Node* head;
- void Reverse(struct Node* p) {
- if (p->next == NULL) {
- head = p;
- return;
- }
- Reverse(p->next);
- struct Node* q = p->next;
- q->next = p;
- p->next = NULL;
- }
- int main() {
- struct Node* head = NULL;
- head = Insert(head,2);
- head = Insert(head, 4);
- head = Insert(head, 6);
- head = Insert(head, 5);
- Print(head);
- Reverse(head);
- Print(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement