#include <stdio.h>
#include <stdlib.h>

struct node {
    int data;
    struct node* next;
};

struct node* createnode(struct node* head, int data) {
    struct node* temp = malloc(sizeof(struct node));
    temp->prev = NULL;
    temp->data = data;
    temp->next = NULL;
    head = temp;
    return head;
}

struct node* addAtEnd(struct node* head, int data) {
    struct node* temp = malloc(sizeof(struct node));
    temp->prev = NULL;
    temp->data = data;
    temp->next = NULL;

    if (head == NULL) {
        return temp;
    }

    struct node* tp = head;
    while (tp->next != NULL) {
        tp = tp->next;
    }
    tp->next = temp;
    temp->prev = tp;
    return head;
}

struct node* createList(struct node* head) {
    int n, i, data;
    printf("Enter the number of nodes: ");
    scanf("%d", &n);

    if (n == 0) return head;

    printf("Enter element for node 1: ");
    scanf("%d", &data);
    head = addToEmpty(head, data);

    for (i = 1; i < n; i++) {
        printf("Enter the element for node %d: ", i + 1);
        scanf("%d", &data);
        head = addAtEnd(head, data);
    }
    return head;
}
    
void display(struct node* head) {
    struct node* ptr = head;
    printf(" ");
    while (ptr != NULL) {
        printf("%d ", ptr->data);
        ptr = ptr->next;
    }
    printf("\n");
}

struct node* reverse(struct node* head) {
    struct node *temp = NULL, *current = head;
    while (current != NULL) {
        temp = current->prev;
        current->prev = current->next;
        current->next = temp;
        current = current->prev; 
    }
    if (temp != NULL) {
        head = temp->prev;
    }
    return head;
}

int main() {
    struct node* head = NULL;
    head = createList(head);

    printf("Original list: ");
    display(head);

    head = reverse(head);

    printf("Reversed list: ");
    display(head);

    return 0;
}