Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //Reverse Iterative mode of a linked list
- #define _CRT_SECURE_NO_WARNINGS
- #include "stdio.h"
- #include "stdlib.h"
- struct Node {
- int data;
- struct Node* next;
- };
- struct Node* insert(struct Node* head, int data) { //insertion at beginning
- struct Node* list = malloc(sizeof(struct Node));
- list->data = data;
- if (list->next != NULL) {
- list->next = head;
- }
- head = list;
- return head;
- }
- void Print(struct Node* head) {
- struct Node* temp = head;
- while (temp->next != NULL) {
- printf("%d ", temp->data);
- temp = temp->next;
- }
- printf("%d ", temp->data);
- printf("\n");
- }
- struct Node* Reverse(struct Node* head) {
- struct Node *current, *prev, *next;
- current = head;
- prev = NULL;
- while (current != NULL) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- head = prev;
- return head;
- }
- int main() {
- struct Node* head = NULL;
- head = insert(head, 2);
- head = insert(head, 4);
- head = insert(head, 6);
- head = insert(head, 8);
- Print(head);
- head = Reverse(head);
- Print(head);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement