Advertisement
Guest User

Untitled

a guest
Mar 19th, 2018
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  1. #include "stdafx.h"
  2. #include <iostream>
  3. #include <vector>
  4. #include <string>
  5. #include <cstdlib>
  6.  
  7. using namespace std;
  8.  
  9. class Node {
  10. public:
  11.     Node* next = nullptr;
  12.     float data;
  13. };
  14.  
  15. class CMyLinkedList {
  16. public:
  17.  
  18.     int length = 0;
  19.     Node* mHead = nullptr;
  20.  
  21.     ~CMyLinkedList() {
  22.         cout << "LIST DELETED";
  23.     }
  24.  
  25.  
  26.     void addToEnd(float data) {
  27.         Node* nodeToInsert = new Node();
  28.          nodeToInsert->data = data;
  29.        
  30.          
  31.  
  32.         if (mHead == nullptr) {
  33.             mHead = nodeToInsert;
  34.             length++;
  35.         }
  36.          
  37.         else {
  38.             Node *temp = mHead;
  39.             while (temp->next != nullptr) {
  40.                 temp = temp->next;
  41.             }
  42.            
  43.        
  44.             temp->next = nodeToInsert;
  45.            
  46.             length++;
  47.        
  48.         }
  49.  
  50.     }
  51.  
  52.  
  53.     float get(int pos) {
  54.  
  55.        
  56.         Node *temp = mHead;
  57.         int count = 0;
  58.          
  59.             while (count <= pos) {
  60.  
  61.                 if (count == pos) {
  62.                
  63.                     if (temp) {
  64.  
  65.                         cout << "your Node is : " << temp->data << endl;
  66.                         return temp->data;
  67.                     }
  68.  
  69.                     else {
  70.                         cout << "you are loser: " << endl;
  71.                         return 0;
  72.                     }
  73.                 }
  74.                 if (temp == nullptr) {
  75.                     cout << "you are loser: " << endl;
  76.                     return 0;
  77.                     }
  78.                     temp = temp->next;
  79.                     count++;
  80.                 }          
  81.     }
  82.    
  83.     void removeHead() {
  84.        
  85.        
  86.    
  87.         if (mHead == nullptr) {
  88.             cout<< "Head is absent" << endl;
  89.         }
  90.         else {
  91.             Node *temp = mHead;
  92.             mHead = mHead->next;
  93.             delete temp;
  94.             length--;
  95.             cout << "Head is removed" << endl;
  96.  
  97.         }
  98.  
  99.     }
  100.  
  101.  
  102.     void removeTail() {
  103.  
  104.        
  105.         cout << mHead << endl;
  106.         if (mHead == nullptr) {
  107.             cout << "there is no list here" << endl;
  108.         }
  109.         else {
  110.    
  111.             if (mHead->next) {
  112.                 removeHead();
  113.                
  114.             }
  115.             else {
  116.                 if (mHead->next->next) {
  117.                     delete mHead->next;
  118.                 }
  119.             }
  120.  
  121.             Node *temp = mHead;
  122.             while (temp->next) {
  123.                 temp = temp->next;            
  124.                 }
  125.  
  126.                 delete temp;
  127.                 length--;
  128.             }
  129.            
  130.            
  131.             cout << "tail is deleted" << endl;
  132.         }
  133.  
  134.    
  135.     void deletePosition(int pos) {
  136.  
  137.  
  138.         if (pos == 0) {
  139.             cout << "there is no list here" << endl;
  140.         }
  141.         else {
  142.  
  143.             Node * temp = mHead;
  144.             Node *temp1 = nullptr;
  145.             for (int i = 0; i < pos - 2; i++) {
  146.                 temp = temp->next;
  147.             }
  148.  
  149.             temp1 = temp->next;
  150.             temp->next = temp1->next;
  151.             length--;
  152.  
  153.         }
  154.  
  155.     }
  156.            
  157.  
  158.  
  159.     void print()  const {
  160.         int i = 1;
  161.         Node * temp = mHead;
  162.         while (temp != nullptr) {
  163.             std::cout << i << ": " << temp->data << std::endl;
  164.             temp = temp->next;
  165.             i++;
  166.         }
  167.     }
  168.         };
  169.  
  170.  
  171.    
  172.  
  173.  
  174.  
  175. int main(float argc, char const *argv[])
  176. {
  177.     CMyLinkedList* list = new CMyLinkedList();
  178.     float af = 11;
  179.  
  180.  
  181.     float ah = 22;
  182.  
  183.     float an = 33;
  184.  
  185.    
  186.  
  187.     list->addToEnd(af);
  188.     list->addToEnd(ah);
  189.     list->addToEnd(an);
  190.     //list->print();
  191.     list->removeTail();
  192.     //list->get(10);
  193.     //list->deletePosition(2);
  194.     list->print();
  195.     //list->removeHead();
  196.  
  197.  
  198.     //list->print();
  199.     cout << "List Length: " << list->length << std::endl;
  200.     delete list;
  201.  
  202.     int aaa;
  203.     cin >> aaa;
  204.     return 0;
  205. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement