Advertisement
Guest User

Untitled

a guest
Nov 1st, 2014
183
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.30 KB | None | 0 0
  1. #include <iostream>
  2.  
  3. using namespace std;
  4.  
  5. class LinkedList
  6. {
  7.     struct Node
  8.     {
  9.         int x;
  10.         Node * next;
  11.     };
  12.  
  13.     public:
  14.         Node * head;
  15.  
  16.         LinkedList()
  17.         {
  18.             head = NULL;
  19.         }
  20.  
  21.  
  22.         void AddNode(int value)
  23.         {
  24.             Node *p = new Node;
  25.             p->x = value;
  26.             p->next = head;
  27.  
  28.             head = p;
  29.         }
  30.  
  31.         void ReverseList()
  32.         {
  33.             Node *previous = NULL;
  34.             Node *current = head;
  35.             Node *next;
  36.  
  37.             while (current != NULL)
  38.             {
  39.                 next = current->next;
  40.                 current->next = previous;
  41.                 previous = current;
  42.                 current = next;
  43.             }
  44.  
  45.             head = previous;
  46.  
  47.         }
  48.  
  49.         void Print()
  50.         {
  51.             Node * p;
  52.             p = head;
  53.             while (p != NULL)
  54.             {
  55.                 cout << p->x << endl;
  56.                 p = p->next;
  57.             }
  58.         }
  59. };
  60.  
  61. int main()
  62. {
  63.     LinkedList list;
  64.     list.AddNode(1);
  65.     list.AddNode(5);
  66.     list.AddNode(10);
  67.     list.AddNode(20);
  68.     list.AddNode(40);
  69.     list.Print();
  70.  
  71.     cout << " " << endl;
  72.  
  73.     list.ReverseList();
  74.     list.Print();
  75.  
  76.     cin.get();
  77.  
  78.     return 0;
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement