Advertisement
uopspop

Untitled

May 9th, 2016
134
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 KB | None | 0 0
  1. #include <iostream>
  2. using namespace std;
  3.  
  4. class ChainNode{
  5.     friend class Chain;
  6.     public:
  7.         ChainNode(int element, ChainNode* next);
  8.     private:
  9.         int data;
  10.         ChainNode* link;   
  11. };
  12.  
  13. class Chain{
  14.     public:
  15.         Chain(): first(NULL){}  //empty constructor
  16.         void insertAtBack(const int& value);
  17.         void insertAtFront(const int& value);
  18.         void deletefromBack();
  19.         void deletefromFront();
  20.         void print();
  21.         ~Chain();
  22.     private:
  23.         ChainNode* first;
  24. };
  25.  
  26.  
  27. int main(){
  28.     Chain myChain;
  29.     myChain.insertAtBack(50);
  30.     myChain.insertAtBack(60);  
  31.  
  32.     myChain.insertAtFront(10);
  33.     myChain.insertAtFront(20);
  34.     myChain.insertAtFront(30);
  35.     myChain.print();
  36.  
  37.     myChain.deletefromBack();
  38.     myChain.deletefromBack();
  39.     myChain.deletefromBack();
  40.     myChain.deletefromBack();
  41.     myChain.deletefromBack();
  42.     myChain.deletefromBack();  
  43.  
  44.     myChain.print();
  45.  
  46.     myChain.insertAtBack(50);
  47.     myChain.insertAtBack(60);  
  48.  
  49.     myChain.insertAtFront(10);
  50.     myChain.insertAtFront(20);
  51.     myChain.insertAtFront(30);
  52.     myChain.print();
  53.  
  54.     myChain.deletefromFront();
  55.     myChain.print();
  56.     myChain.deletefromFront();
  57.     myChain.print();
  58.     myChain.deletefromFront();
  59.     myChain.print();
  60.     myChain.deletefromFront();
  61.     myChain.print();
  62.     myChain.deletefromFront();
  63.     myChain.print();
  64.     myChain.deletefromFront();
  65.     myChain.print();
  66. }
  67.  
  68.  
  69. ChainNode::ChainNode(int element, ChainNode* next): data(0), link(NULL)
  70. {
  71.     data = element;
  72.     link = next;
  73. }  
  74.  
  75. void Chain::insertAtBack(const int& value){
  76.     ChainNode* tmpPtr = new ChainNode(value,0);
  77.     if(first){
  78.         ChainNode* last = first;
  79.         while(last->link != NULL)
  80.             last = last->link;
  81.         last->link = tmpPtr;
  82.     }else{
  83.         first  = tmpPtr;
  84.     }
  85. }
  86. void Chain::insertAtFront(const int& value){
  87.     ChainNode* tmpPtr = new ChainNode(value,0);
  88.     if(first){
  89.         tmpPtr->link = first;
  90.         first = tmpPtr;
  91.     }else{
  92.         first  = tmpPtr;
  93.     }
  94. }
  95. void Chain::deletefromBack(){
  96.     int value;
  97.     // get the mry location of the last node
  98.     if (first){            
  99.         ChainNode* last = first;
  100.         while(last->link != NULL){
  101.             last = last->link;
  102.         }// end while
  103.    
  104.    
  105.         if(first == last){
  106.             value = last->data;
  107.             delete last;
  108.             first = NULL;
  109.             cout << value << " is removed" << " and nothing remained"<< endl;
  110.         }else{
  111.             ChainNode* previous = first;
  112.             while(previous->link != last){
  113.                 previous = previous->link;             
  114.             }
  115.                            
  116.             previous->link = NULL;
  117.             value = last->data;
  118.             delete last;
  119.             cout << value << " is removed" << endl;
  120.         }// end inner if
  121.     }else{
  122.         cout << "remove unsuccessful: no nodes remained" << endl;
  123.     }// end if
  124. }
  125. void Chain::deletefromFront(){
  126.     int value;
  127.     // get the mry location of the last node
  128.    
  129.     if(first){             
  130.         ChainNode* tmpPtr = first;
  131.         first = first->link;
  132.         value = tmpPtr->data;
  133.         delete tmpPtr;
  134.         cout << value << " is removed" << endl;
  135.     }else{
  136.         cout << "remove unsuccessful: no nodes remained" << endl;
  137.     }
  138. }      
  139. void Chain::print(){
  140.     if(first != NULL){
  141.         ChainNode* tmpPtr = first;
  142.         while(tmpPtr != NULL){
  143.             cout << tmpPtr->data << " ";
  144.             tmpPtr = tmpPtr->link;
  145.         }// end while          
  146.         cout << endl;
  147.     }else{
  148.         cout << "nothing" << endl;
  149.     }// end if
  150. }
  151. Chain::~Chain(){
  152.    
  153.     ChainNode* tmpPtr = first;
  154.     while(tmpPtr != NULL){
  155.         first = first->link;
  156.         delete tmpPtr;
  157.         tmpPtr = first;
  158.     }// end while
  159. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement