Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // LL_.cpp : This file contains the 'main' function. Program execution begins and ends there.
- //
- #include <iostream>
- struct Node
- {
- int data;
- struct Node* next;
- };
- using namespace std;
- void add(int data, struct Node ** head)
- {
- struct Node* p = new Node;
- p->data = data;
- p->next = *head;
- *head = p;
- }
- void display(struct Node** head)
- {
- struct Node* p = *head;
- while(p)
- {
- cout << p->data << " ";
- p = p->next;
- }
- cout << endl;
- }
- void reverse(struct Node**head)
- {
- struct Node* prev = NULL;
- struct Node* current = *head;
- struct Node* next = NULL;
- while (current != NULL) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- *head = prev;
- }
- void reverse2(struct Node** head)
- {
- struct Node* lastHead;
- int k = 2;
- k--; // fix
- int n = 4;
- lastHead = *head;
- while (k--)
- lastHead = lastHead->next;
- cout << lastHead->data << endl;
- struct Node* prev = NULL;
- struct Node* current = lastHead->next;
- struct Node * lastTemp = lastHead->next;
- struct Node* next = NULL;
- while (current != NULL && n--) {
- next = current->next;
- current->next = prev;
- prev = current;
- current = next;
- }
- lastHead->next = prev;
- struct Node* p = *head;
- while (p->next)
- p = p->next;
- p->next = next;
- }
- int main()
- {
- struct Node* first = NULL;
- add(8, &first);
- add(9, &first);
- add(10, &first);
- add(20, &first);
- add(30, &first);
- add(40, &first);
- add(50, &first);
- add(60, &first);
- display(&first);
- reverse2(&first);
- display(&first);
- }
- // working
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement