Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Reversing the doubly linked list */
- #include <stdio.h>
- #include <stdlib.h>
- struct node {
- struct node *prev;
- int data;
- struct node *next;
- };
- struct node *addToEmpty(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, *tp;
- temp = malloc(sizeof(struct node));
- temp->prev = NULL;
- temp->data = data;
- temp->next = NULL;
- tp = head;
- while (tp->next != NULL)
- {
- tp = tp->next;
- }
- tp->next = temp;
- temp->prev = tp;
- return head;
- }
- struct node* reverse(struct node* head)
- {
- struct node* ptr1 = head;
- struct node* ptr2 = ptr1->next;
- ptr1->next = NULL;
- ptr1->prev = ptr2;
- while (ptr2 != NULL)
- {
- ptr2->prev = ptr2->next;
- ptr2->next = ptr1;
- ptr1 = ptr2;
- ptr2 = ptr2->prev;
- }
- head = ptr1;
- return head;
- }
- void print(struct node* head)
- {
- struct node* ptr = head;
- while (ptr != NULL)
- {
- printf("%d ", ptr->data);
- ptr = ptr->next;
- }
- printf("\n");
- }
- int main()
- {
- struct node *head = NULL;
- struct node *ptr;
- head = addToEmpty(head, 24);
- head = addAtEnd(head, 45);
- head = addAtEnd(head, 9);
- printf("Before Reversing the list: ");
- print(head);
- head = reverse(head);
- printf("After Reversing the list: ");
- print(head);
- return 0;
- }
- /*
- Output:
- Before Reversing the list : 24 45 9
- After Reversing the list : 9 45 24
- */
Add Comment
Please, Sign In to add comment