Advertisement
SabirSazzad

Single Linklist Reverse Travle

Feb 26th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. struct Node
  5. {
  6.    int data;
  7.    Node *point;
  8. };
  9.  
  10. Node *head;
  11. Node* getnewnode(int value)
  12. {
  13.     Node *newnode = new Node();
  14.     newnode -> data = value;
  15.     newnode -> point = NULL;
  16.     return newnode;
  17. }
  18.  
  19. void insertatanyindex(int n, int value)
  20. {
  21.     Node *newnode= getnewnode(value);
  22.     if(n==1)
  23.     {
  24.         newnode->point = head;
  25.         head = newnode;
  26.         return;
  27.     }
  28.     Node *temp = head;
  29.     for(int i=1; i<n-1; i++)
  30.     {
  31.         temp = temp->point;
  32.     }
  33.     newnode->point = temp->point;
  34.     temp->point = newnode;
  35. }
  36.  
  37. void print()
  38. {
  39.     Node *temp = head;
  40.     while(temp->point != NULL)
  41.     {
  42.         cout << temp->data << " ";
  43.         temp = temp->point;
  44.     }
  45.     cout << temp->data << endl;
  46. }
  47.  
  48. //void ReversePrint(Node *Head)
  49. //{
  50. //    if(Head==NULL)
  51. //    {
  52. //        return;
  53. //    }
  54. //    ReversePrint(Head->point);
  55. //    cout << Head->data << " ";
  56. //}
  57.  
  58. void ReversePrint(Node *Head)
  59. {
  60.     if(Head->point !=NULL)
  61.         ReversePrint(Head->point);
  62.     cout << Head->data << " ";
  63. }
  64.  
  65. int main()
  66. {
  67.     int data,limit,i;
  68.     cout << "Input size of link list: ";
  69.     cin >> limit;
  70.     for(i=1; i<=limit; i++)
  71.     {
  72.         cout << "Input data: ";
  73.         cin >> data;
  74.         insertatanyindex(i,data);
  75.  
  76.     }
  77.     cout << "\nForward Print...."<<endl;
  78.     print();
  79.     cout << "\nReverse Print...."<<endl;
  80.     ReversePrint(head);
  81.     cout << "\n"<<endl;
  82.  
  83.     return 0;
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement