Advertisement
Guest User

Untitled

a guest
May 22nd, 2017
43
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.82 KB | None | 0 0
  1. //////////////////////////////  nodedata.h  //////////////////////////////////
  2. // Simple node data containing one integer and one character
  3. #ifndef LIST_H
  4. #define LIST_H
  5.  
  6. #include <iostream>
  7. #include <fstream>
  8. using namespace std;
  9.  
  10. template <typename T>
  11. //---------------------------  class NodeData  ------------------------------
  12. class List {                                       
  13.     friend ostream& operator<< (ostream &output, const List<T> &a) {
  14.         Node* current = a.head;
  15.         while(current != NULL) {
  16.             output << *current->data;
  17.             current = current->next;
  18.         }
  19.         return output;
  20.     }
  21.  
  22.  
  23. public:
  24.     List();
  25.     List(const List &);
  26.     ~List();
  27.    
  28.     void buildList(ifstream& infile);
  29.     bool insert(T*);
  30.     bool isEmpty() const;
  31.     bool remove(T&, T*);
  32.  
  33. private:
  34.     int size;
  35.     struct Node {                 // the node in a linked list
  36.         T* data;            // pointer to actual data, operations in NodeData
  37.         Node* next;
  38.     };
  39.     Node* head;
  40. };
  41.  
  42. //-------------------------- Constructor ------------------------------------
  43. // Default constructor for class Array
  44. template <typename T>
  45. List<T>::List() {
  46.     head = NULL;
  47. }
  48.  
  49. //---------------------------- Copy -----------------------------------------
  50. // Copy constructor for class List
  51. template <typename T>
  52. List<T>::List(const List<T> &init) {
  53.     Node current = init.head;
  54.     head = current;
  55.     Node current2 = head;
  56.     if (current != NULL) {
  57.         while(current -> next != NULL) {
  58.             current2 -> next = current -> next;
  59.             current = current -> next;
  60.             current2 = current -> next;
  61.         }
  62.     }
  63. }
  64.  
  65. template <typename T>
  66. List<T>::~List() {
  67.     Node* current = head;
  68.     Node* previous = current;
  69.     while(current != NULL) {
  70.         current = current -> next;
  71.         delete previous->data;
  72.         delete previous;
  73.         previous = current;
  74.     }
  75.     head = NULL;
  76.     cout << "SUCCESS!";
  77. }
  78.  
  79.  
  80. // continually insert new items into the list
  81. template <typename T>
  82. void List<T>::buildList(ifstream& infile) {
  83.     T* ptr;
  84.     bool successfulRead;                                     // read good data
  85.     bool success;                                             // successfully insert
  86.     for (;;) {
  87.         ptr = new T;
  88.         successfulRead = ptr->setData(infile);       // fill the NodeData object
  89.         if (infile.eof()) {
  90.             delete ptr;
  91.             break;
  92.         }
  93.  
  94.         // insert good data into the list, otherwise ignore it
  95.         if (successfulRead) {
  96.             success = insert(ptr);
  97.         }
  98.         else {
  99.             delete ptr;
  100.         }
  101.         if (!success) break;
  102.     }
  103. }
  104.  
  105.  
  106.  
  107.  
  108. template <typename T>
  109. bool List<T>::insert(T* dataptr) {                       
  110.  
  111.     Node* ptr= new Node;
  112.     if (ptr == NULL) return false;                    // out of memory, bail
  113.     ptr->data = dataptr;                                    // link the node to data
  114.  
  115.     // if the list is empty or if the node should be inserted before
  116.     // the first node of the list
  117.     if (isEmpty() || *ptr->data < *head->data) {
  118.         ptr->next = head;                                  
  119.         head = ptr;
  120.     }
  121.      
  122.     // then check the rest of the list until we find where it belongs
  123.     else {
  124.         Node* current = head->next;          // to walk list
  125.         Node* previous = head;                  // to walk list, lags behind
  126.  
  127.         // walk until end of the list or found position to insert
  128.         while (current != NULL && *current->data < *ptr->data) {
  129.                 previous = current;                     // walk to next node
  130.                 current = current->next;
  131.         }
  132.  
  133.         // insert new node, link it in
  134.         ptr->next = current;
  135.         previous->next = ptr;
  136.     }
  137.     return true;
  138. }
  139.  
  140. template <typename T>
  141. bool List<T>::isEmpty() const {
  142.     return head == NULL;
  143. }
  144.  
  145. template <typename T>
  146. bool List<T>::remove(T& first, T* second) {
  147.     if(isEmpty()) {
  148.         return false;
  149.     }
  150.     Node* current = head;
  151.     if(*current->data != first) {
  152.         while(current->next != NULL || *current->next->data != first) {
  153.             current = current->next;
  154.         }
  155.     }
  156. // else {
  157. //      Node* temp = current;
  158. //      head = head-> next;
  159.        
  160. //  }
  161.     if(current->next == NULL) {
  162.         return false;
  163.     }
  164.     Node* temp = current->next;
  165.     if(temp->next != NULL) {
  166.         current->next = temp->next;
  167.     }
  168.     second = temp->data;
  169.     delete temp;
  170.     return true;
  171. }
  172.  
  173. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement