All pastes #55776 Raw Edit

Miscellany

public text v1 · immutable
#55776 ·published 2006-05-14 12:14 UTC
rendered paste body
#include <iostream>

using namespace std;

template<class T>
class Node
{
    friend class LinkedList<T>;
    private:
        Node<T> * next;
        Node<T> * prev;
        T data;
    public:
        Node();
};

template<class T>
Node<T>::Node()
{
    next = NULL;
    prev = NULL;
}

template<class T>
class LinkedList<T>
{
    private:
        Node<T> * head;
        int position;
    public:
        LinkedList();
        void print() const;
        void remove(int position);
        void insert(T x);

};

template <class T>
LinkedList<T>::LinkedList()
{
    head = NULL;
    position = 0;
}

template <class T>
void LinkedList<T>::print()
{
    Node<T> *tmp;
    tmp = head;
    while(tmp)
    {
        cout << "you have a " << (tmp->data).get_name() << " in slot " << position << endl;
        tmp = tmp -> next;
    }
}

template <class T>
void LinkedList<T>::remove(int position)
{
    Node<T> * tmp;
    Node<T> * tmp2;
    Node<T> * tmp3;
    tmp = head;
    int count = 1;
    while(tmp)
    {
        if(count == position)
        {
            if(position == 1)
            {
                head = tmp -> next;
                break;
            }
            else if(position > 1)
            {
                if(!tmp->next)
                {
                    tmp->prev->next = NULL;
                }
                else
                {
                    tmp2 = tmp->prev;
                    tmp3 = tmp->next;
                    tmp2->next = tmp3;
                    tmp3->prev = temp2;
                    break;
                }
            }
            else
            {
                break;
            }
        }
        tmp = tmp->next;
        count++;
    }
}

template<class T>
void LinkedList<T>::insert(T x)
{
    Node<T> * tmp = head;
    Node<T> * new_node = new Node<T>;

    new_node -> data = x;
    if(!head)
    {
        head = new_node;
        position = 1;
    }
    else
    {
        while(tmp->next != NULL)
        {
            position++;
            tmp = tmp->next;
        }
        new_node = tmp -> next;
        position ++;
    }
}