Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- using namespace std;
- class LinkedList
- {
- struct Node
- {
- int x;
- Node * next;
- };
- public:
- Node * head;
- LinkedList()
- {
- head = NULL;
- }
- void AddNode(int value)
- {
- Node *p = new Node;
- p->x = value;
- p->next = head;
- head = p;
- }
- void ReverseList()
- {
- Node *previous = NULL;
- Node *current = head;
- Node *next;
- while (current != NULL)
- {
- next = current->next;
- current->next = previous;
- previous = current;
- current = next;
- }
- head = previous;
- }
- void Print()
- {
- Node * p;
- p = head;
- while (p != NULL)
- {
- cout << p->x << endl;
- p = p->next;
- }
- }
- };
- int main()
- {
- LinkedList list;
- list.AddNode(1);
- list.AddNode(5);
- list.AddNode(10);
- list.AddNode(20);
- list.AddNode(40);
- list.Print();
- cout << " " << endl;
- list.ReverseList();
- list.Print();
- cin.get();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement