Sunday, August 9, 2015

Linked Lists

Linked lists are the backbone of just about any information system.  They're used everywhere.

These days we don't often roll our own linked lists.  There are much better ways of implementing lists in high-level languages such as Java, C# and C++.  We use vectors, maps and lists.

Nevertheless, you need to learn about linked lists.  My first implementation was done in 65C02 assembly on the Apple IIc.

I provide here an outline of a linked list class.  Your job, should you choose to accept, is to complete the implementation.

//
//  LinkedList.cpp
//  LinkedList
//
//  Created by David Means on 8/5/15.
//  Copyright (c) 2015 David Means. All rights reserved.
//  All rights reserved.
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//      * Redistributions of source code must retain the above copyright
//        notice, this list of conditions and the following disclaimer.
//      * Redistributions in binary form must reproduce the above copyright
//        notice, this list of conditions and the following disclaimer in the
//        documentation and/or other materials provided with the distribution.
//      * Neither the name of the <organization> nor the
//        names of its contributors may be used to endorse or promote products
//        derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
//  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
//  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL DAVID MEANS BE LIABLE FOR ANY
//  DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
//  (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
//  LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
//  ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
//  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//

#include <iostream>
#include "LinkedList.h"
using namespace std;

LinkedList::LinkedList() : head(0), nodeCount(0), tail(0)
{
    tail = head = new Node();
    cout << "-- Added Node[" << nodeCount << "] -- " << endl;
    Print(head);
}

LinkedList::~LinkedList()
{
    while ( tail )
    {
        Delete(tail);
    }
    tail = head = 0;
}


Node* LinkedList::Find(const string& s)
{
    Node* search = head->getNext();
    
    cout << "-- Search[" << s << "] --" << endl;
    
    for (; search; search = search->getNext())
    {
        if ( search->getData() == 0 ) // May be the head
            continue;
        
        if ( search->getData()->compare(s) == 0 )
        {
            cout << "\tSearch success" << endl << endl;
            return search;
        }
    }
    cout << "\tSearch failed" << endl << endl;
    return (Node*)0;
}


void LinkedList::Print() const
{
    const Node* node = head;
    cout << endl << "--- Print List ---" << endl << endl;
    do
    {
        Print(node);
    } while ( (node = node->getNext()));
    
}

void LinkedList::Print(const Node* n) const
{
    if ( n == 0 )
    {
        cout << "-- List is Empty --" << endl
             << "-- Head follows:" << endl;
    }
        
    cout << endl
        << "\tthis = " << n << endl
        << "\tdata = " << (n->getData() ? n->getData()->c_str() : string("")) << endl
        << "\tprev = " << n->getPrev() << endl
        << "\tnext = " << n->getNext() << endl;
}

void LinkedList::Add(const string& s)
{
    Node* newTail = new Node(s);
    
    ++nodeCount;
    tail->setNext(newTail);
    newTail->setPrev(tail);
    tail = newTail;
    
    cout << "-- Added Node[" << nodeCount << "] -- " << endl;
    Print(tail);
}


void LinkedList::Add(const char* s)
{
    string str(s);
    Add(str);
}

void LinkedList::Delete(const char* s)
{
    Delete(Find(string(s)));
}

void LinkedList::Delete(const string& s)
{
    Delete(Find(s));
}

void LinkedList::Delete(Node* removeNode)
{
    
    Node* prevNode;
    Node* nextNode;
    
    if ( !removeNode )
        return;

    prevNode = removeNode->getPrev();
    nextNode = removeNode->getNext();
    
    cout << "-- Deleting Node[" << nodeCount << "] --" << endl;
    nodeCount--;
    
    if ( removeNode == head )
    {
        cout << "-- Deleting ('head') --" << endl;
        Print(removeNode);
        head = tail = 0;
        return;
    }
    
    if ( removeNode == tail )
    {
        cout << "-- Deleting ('tail') --" << endl;
        Print(removeNode);
        
        delete removeNode;
        tail = prevNode;
        tail->setNext(0);
        return;
    }

    Print(removeNode);

    prevNode->setNext(nextNode);
    nextNode->setPrev(prevNode);

    delete removeNode;
    
}



--- Example Output ---

-- Added Node[0] --

    this = 0x7f999bc04bb0
    data = (null)
    prev = 0x0
    next = 0x0

    New node data: [First]
-- Added Node[1] --

    this = 0x7f999bc04bd0
    data = First
    prev = 0x7f999bc04bb0
    next = 0x0

    New node data: [Second]
-- Added Node[2] --

    this = 0x7f999bc04c10
    data = Second
    prev = 0x7f999bc04bd0
    next = 0x0



No comments:

Post a Comment