Recursively Search a linked List - C++ Data Structure

C++ examples for Data Structure:Linked List

Description

Recursively Search a linked List

Demo Code

#include <iostream>

template <typename NODETYPE>
class List;/*w ww  .  j a  v  a2s.  c om*/

template <typename NODETYPE>
class ListNode {
    friend class List<NODETYPE>;  // make List a friend

 public:
    explicit ListNode(const NODETYPE &);

    NODETYPE getData() const;

    ListNode<NODETYPE>* next() const;
    ListNode<NODETYPE>* prev() const;

 private:
    NODETYPE data;
    ListNode<NODETYPE> *nextPtr;
    ListNode<NODETYPE> *prevPtr;
};

template <typename NODETYPE>
ListNode<NODETYPE>::ListNode(const NODETYPE &info) :
    data(info), nextPtr(0), prevPtr(0) {}

template <typename NODETYPE>
NODETYPE ListNode<NODETYPE>::getData() const {
    return data;
}


template <typename NODETYPE>
ListNode<NODETYPE>* ListNode<NODETYPE>::next() const {
    return nextPtr;
}

template <typename NODETYPE>
ListNode<NODETYPE>* ListNode<NODETYPE>::prev() const {
    return prevPtr;
}


template <typename NODETYPE>
class List {
 public:
    List();
    ~List();

    void insertAtFront(const NODETYPE &);
    void insertAtBack(const NODETYPE &);
    bool removeFromFront(NODETYPE &);
    bool removeFromBack(NODETYPE &);
    void concatenate(List<NODETYPE> &);
    bool isEmpty() const;
    void print() const;
    void printListBackwards() const;
    int size() const;

    ListNode<NODETYPE>* begin() const { return firstPtr; }
    ListNode<NODETYPE>* end() const { return lastPtr; }
    ListNode<NODETYPE>* searchList(const NODETYPE&) const;

 private:
    ListNode<NODETYPE> *firstPtr;  // pointer to first node
    ListNode<NODETYPE> *lastPtr;   // pointer to last node

    int sz;

    // utility function to allocate new node
    ListNode<NODETYPE> *getNewNode(const NODETYPE &);
    void sortHelper();
};

template <typename NODETYPE>
List<NODETYPE>::List() : firstPtr(0), lastPtr(0), sz(0) {}

template <typename NODETYPE>
List<NODETYPE>::~List() {
    if (!isEmpty()) {
        ListNode<NODETYPE> *currentPtr = firstPtr;

        // delete remaining nodes
        while (currentPtr != 0) {
            ListNode<NODETYPE> *tempPtr = currentPtr;
            currentPtr = currentPtr->nextPtr;
            delete tempPtr;
        }
    }
}

template <typename NODETYPE>
void List<NODETYPE>::insertAtFront(const NODETYPE &value) {
    ListNode<NODETYPE> *newPtr = getNewNode(value);

    if (isEmpty()) {
        firstPtr = lastPtr = newPtr;  // new list only has one node
    } else {
        newPtr->nextPtr = firstPtr;  // point new node to previous list node
        firstPtr->prevPtr = newPtr;  // point back to new node.
        firstPtr = newPtr;
    }
    ++sz;
}

template <typename NODETYPE>
void List<NODETYPE>::insertAtBack(const NODETYPE &value) {
    ListNode<NODETYPE> *newPtr = getNewNode(value);

    if (isEmpty()) {
        firstPtr = lastPtr = newPtr;  // new list has only one node
    } else {
        lastPtr->nextPtr = newPtr;  // point forward to new node
        newPtr->prevPtr = lastPtr;  // point back to previous node
        lastPtr = newPtr;
    }
    ++sz;
}

template <typename NODETYPE>
bool List<NODETYPE>::removeFromFront(NODETYPE &value) {
    if (isEmpty()) {  // list is empty
        return false;
    } else {
        ListNode<NODETYPE> *tempPtr = firstPtr;

        if (firstPtr == lastPtr)
            firstPtr = lastPtr = 0;  // no nodes remain after removal
        else
            firstPtr = firstPtr->nextPtr;  // point to previous 2nd node

        value = tempPtr->data;
        delete tempPtr;

        --sz;

        return true;
    }
}

template <typename NODETYPE>
bool List<NODETYPE>::removeFromBack(NODETYPE &value) {
    if (isEmpty()) {  // list is empty
        return false;
    } else {
        ListNode<NODETYPE> *tempPtr = lastPtr;

        if (firstPtr == lastPtr) {   // list has one element
            firstPtr = lastPtr = 0;  // no nodes remain after removal
        } else {
            ListNode<NODETYPE> *currentPtr = firstPtr;

            // locate second to last element
            while (currentPtr->nextPtr != lastPtr)
                currentPtr = currentPtr->nextPtr;  // move to next node

            lastPtr = currentPtr;     // remove last node
            currentPtr->nextPtr = 0;  // this is now the last node
        }

        value = tempPtr->data;
        delete tempPtr;

        --sz;
        return true;
    }
}

template <typename NODETYPE>
void List<NODETYPE>::concatenate(List<NODETYPE> &listSecond) {
    ListNode<NODETYPE> *currentPtr = listSecond.firstPtr;

    while (currentPtr != 0) {
        insertAtBack(currentPtr->getData());
        currentPtr = currentPtr->nextPtr;
    }
}

template <typename NODETYPE>
bool List<NODETYPE>::isEmpty() const {
    return firstPtr == 0;
}

template <typename NODETYPE>
ListNode<NODETYPE> *List<NODETYPE>::getNewNode(const NODETYPE &value) {
    return new ListNode<NODETYPE>(value);
}

template <typename NODETYPE>
void List<NODETYPE>::print() const {
    if (isEmpty()) {
        std::cout << "The list is empty\n\n";
        return;
    }

    static ListNode<NODETYPE> *currentPtr = begin();

    std::cout << currentPtr->getData() << ' ';
    currentPtr = currentPtr->nextPtr;

    if (currentPtr == nullptr) {
        std::cout << std::endl;
        return;
    }

    print();
}

template <typename NODETYPE>
void List<NODETYPE>::printListBackwards() const {
    if (isEmpty()) {
        std::cout << "The list is empty\n\n";
        return;
    }

    static ListNode<NODETYPE> *currentPtr = end();

    std::cout << currentPtr->getData() << ' ';
    currentPtr = currentPtr->prevPtr;

    if (currentPtr == nullptr) {
        std::cout << std::endl;
        return;
    }

    printListBackwards();
}

template <typename NODETYPE>
int List<NODETYPE>::size() const {
    return sz;
}

template <typename NODETYPE>
ListNode<NODETYPE>* List<NODETYPE>::searchList(const NODETYPE& val) const {
    static ListNode<NODETYPE> *currentPtr = begin();

    if (currentPtr == nullptr || currentPtr->getData() == val) {
        return currentPtr;
    }

    currentPtr = currentPtr->nextPtr;

    searchList(val);
}

int main(int argc, char* argv[]) {
    static const int LIMIT = 20;

    List<int> intList;
    int value = 0;

    printf("Enter %d ints: \n", LIMIT);
    for (int i = 0; i < LIMIT; ++i) {
        std::cin >> value;

        intList.insertAtBack(value);
    }

    std::cin.clear();
    std::cin.ignore(INT8_MAX, '\n');

    std::cout << "Enter value to find in list: ";
    std::cin >> value;

    ListNode<int>* res = intList.searchList(value);

    printf("%d%sfound\n", value, ((res == nullptr) ? " not " : " "));

    return 0;
}

Related Tutorials