LinkedList模板/ LinkedList堆栈模板

时间:2017-11-15 13:40:39

标签: c++ compiler-errors linked-list

当我尝试使用模板方式实现链表时,我遇到了一些错误。我无法解决4-5小时的问题。会变得疯狂..

当我尝试创建对象时,我正在处理错误消息。在堆栈工具中采用相同的错误..

  

1> Kaynak1.obj:错误LNK2001:Çözümlenmemişdışsembol“public:   __thiscall linkedlist :: ~linklist(void)“(?? 1?$ linkedlist @ H @@ QAE @ XZ)   1> Kaynak1.obj:错误LNK2001:   Çözümlenmemişdışsembol“public:__ thishisall   linkedlist :: linkedlist(void)“(?? 0?$ linkedlist @ H @@ QAE @ XZ)

我的头文件是:

#pragma once
#include<iostream>
#include<cassert>
using namespace std;
template <typename Type1>
struct node
{
    Type1 data;
    node* link;
};
template <class Type>
class linkedlist {
    node <Type> *first, *last;
    int count;
public:
    linkedlist();
    linkedlist(linkedlist&);
    void initializeList();
    int length()const;
    bool isEmptyList()const;
    void print()const;

    Type front()const;
    Type back()const;
    bool search(Type) const;
    void insertFirst(Type);
    void insertLast(Type);
    void deleteNode(Type);
    ~linkedlist();
};

我的工具文件;

#include"Üst Bilgi.h"
template <class Type>
linkedlist<Type>::linkedlist() {
    first = NULL;
    last = NULL;
    count = 0;
}
template <class Type>
int linkedlist<Type>::length()const {
    return count;
}
template <class Type>
bool linkedlist<Type>::isEmptyList()const {
    return count == 0;
}
template <class Type>
Type linkedlist<Type>::front()const {
    assert(first != NULL);
    return first->data;
}
template <class Type>
Type linkedlist<Type>::back()const {
    assert(last != NULL);
    return last->data;
}
template <class Type>
void linkedlist<Type>::print()const {
    node *temp = first;

    if (temp == NULL)
    {
        cout << "Empty list..." << endl;
    }
    else
    {
        while (temp != NULL)
        {
            cout << temp->data << endl;
            temp = temp->link;
        }
    }
}
template <class Type>
void linkedlist<Type>::insertFirst(Type value) {

    node <Type1> *valuenode = new node;

    valuenode->data = value;
    valuenode->link = NULL;

    valuenode->link = first;
    first = valuenode;

    if (last == NULL)
    {
        last = valuenode;
    }
    count++;
}
template <class Type>
void linkedlist<Type>::insertLast(Type value) {
    node *valuenode = new node;

    valuenode->data = value;
    valuenode->link = NULL;

    if (last == NULL) {
        first = last = valuenode;
    }
    else
    {
        last->link = valuenode;
        last = valuenode;
    }
    count++;
}
template <class Type>
linkedlist<Type>::~linkedlist() {

    node* temp = first;

    while (temp != NULL)
    {
        first = first->link;
        delete temp;
        temp = first;
    }
    last = NULL;
    count = 0;
}
template <class Type>
linkedlist<Type>::linkedlist(linkedlist &other) {

    if (other.first == NULL)
    {
        first = last = NULL;
        count = 0;
    }
    else
    {
        count = other.count;
        first = new node;
        first->data = other.first->data;
        first->link = NULL;
        last = first;

        node *current = other.first->link;

        while (current != NULL)
        {
            insertLast(current->data);
            current = current->link;
        }
    }
}
template <class Type>
bool linkedlist<Type>::search(Type value)const {

    bool found = false;

    node *temp = first;

    while (temp != NULL)
    {
        if (temp->data == value)
        {
            found = true;
            break;
        }
        temp = temp->link;
    }

    return found;
}
template <class Type>
void linkedlist<Type>::initializeList() {

    node *temp = first;
    while (temp != NULL)
    {
        first = first->link;
        delete temp;
        temp = first;
    }
    last = NULL; count = 0;
}
template <class Type>
void linkedlist<Type>::deleteNode(Type value) {

    bool isfound = search(value);
    node *temp = first;

    if (isfound)
    {
        if (first == last)
        {
            delete temp;
            count = 0;
            last = NULL;
        }
        else if (first->data == value) {
            first = first->link;
            delete temp;
            temp = first;
            count--;
        }
        else if (last->data == value)
        {
            node *rtemp = temp;
            while (temp->link != NULL)
            {
                rtemp = temp;
                temp = temp->link;
            }
            last = rtemp;
            delete temp;

            last->link = NULL;
            count--;
        }
        else
        {
            node *rtemp = temp;
            while (temp->link != NULL)
            {
                rtemp = temp;
                temp = temp->link;
                if (temp->data == value)
                {
                    break;
                }
            }
            rtemp->link = temp->link;
            delete temp;
            count--;
        }
    }
}

0 个答案:

没有答案