Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ////////////////////////////// nodedata.h //////////////////////////////////
- // Simple node data containing one integer and one character
- #ifndef LIST_H
- #define LIST_H
- #include <iostream>
- #include <fstream>
- using namespace std;
- template <typename T>
- //--------------------------- class NodeData ------------------------------
- class List {
- friend ostream& operator<< (ostream &output, const List<T> &a) {
- Node* current = a.head;
- while(current != NULL) {
- output << *current->data;
- current = current->next;
- }
- return output;
- }
- public:
- List();
- List(const List &);
- ~List();
- void buildList(ifstream& infile);
- bool insert(T*);
- bool isEmpty() const;
- bool remove(T&, T*);
- private:
- int size;
- struct Node { // the node in a linked list
- T* data; // pointer to actual data, operations in NodeData
- Node* next;
- };
- Node* head;
- };
- //-------------------------- Constructor ------------------------------------
- // Default constructor for class Array
- template <typename T>
- List<T>::List() {
- head = NULL;
- }
- //---------------------------- Copy -----------------------------------------
- // Copy constructor for class List
- template <typename T>
- List<T>::List(const List<T> &init) {
- Node current = init.head;
- head = current;
- Node current2 = head;
- if (current != NULL) {
- while(current -> next != NULL) {
- current2 -> next = current -> next;
- current = current -> next;
- current2 = current -> next;
- }
- }
- }
- template <typename T>
- List<T>::~List() {
- Node* current = head;
- Node* previous = current;
- while(current != NULL) {
- current = current -> next;
- delete previous->data;
- delete previous;
- previous = current;
- }
- head = NULL;
- cout << "SUCCESS!";
- }
- // continually insert new items into the list
- template <typename T>
- void List<T>::buildList(ifstream& infile) {
- T* ptr;
- bool successfulRead; // read good data
- bool success; // successfully insert
- for (;;) {
- ptr = new T;
- successfulRead = ptr->setData(infile); // fill the NodeData object
- if (infile.eof()) {
- delete ptr;
- break;
- }
- // insert good data into the list, otherwise ignore it
- if (successfulRead) {
- success = insert(ptr);
- }
- else {
- delete ptr;
- }
- if (!success) break;
- }
- }
- template <typename T>
- bool List<T>::insert(T* dataptr) {
- Node* ptr= new Node;
- if (ptr == NULL) return false; // out of memory, bail
- ptr->data = dataptr; // link the node to data
- // if the list is empty or if the node should be inserted before
- // the first node of the list
- if (isEmpty() || *ptr->data < *head->data) {
- ptr->next = head;
- head = ptr;
- }
- // then check the rest of the list until we find where it belongs
- else {
- Node* current = head->next; // to walk list
- Node* previous = head; // to walk list, lags behind
- // walk until end of the list or found position to insert
- while (current != NULL && *current->data < *ptr->data) {
- previous = current; // walk to next node
- current = current->next;
- }
- // insert new node, link it in
- ptr->next = current;
- previous->next = ptr;
- }
- return true;
- }
- template <typename T>
- bool List<T>::isEmpty() const {
- return head == NULL;
- }
- template <typename T>
- bool List<T>::remove(T& first, T* second) {
- if(isEmpty()) {
- return false;
- }
- Node* current = head;
- if(*current->data != first) {
- while(current->next != NULL || *current->next->data != first) {
- current = current->next;
- }
- }
- // else {
- // Node* temp = current;
- // head = head-> next;
- // }
- if(current->next == NULL) {
- return false;
- }
- Node* temp = current->next;
- if(temp->next != NULL) {
- current->next = temp->next;
- }
- second = temp->data;
- delete temp;
- return true;
- }
- #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement