使用公共类函数初始化nodeType <type>指针C ++

时间:2016-04-18 23:12:47

标签: c++ pointers linked-list

我发布了一个类似的问题,但我再次提问,因为我的尝试错了。所以现在这是一个完全不同的问题。

这是关于Payroll对象链接列表的学校项目的一部分。我教授的反馈表明我需要从内部 InList函数遍历链表。我明白他在说什么,但不知道怎么做。

在InList函数中,我传递一个链表和一个int,创建Payroll对象并将int指定为员工编号变量。然后使用nodeType当前指针遍历列表并打印记录(如果找到)。

    void InList(const orderedLinkedList<Payroll>& P, int employee_number)
    {
    int listCount = P.length();
    Payroll object;
    object.setEmployeeNumber(employee_number);
    nodeType<Payroll> *current; //how to initialize this?
    if (P.isEmptyList())
        cout << "Sorry, there is nothing in the list yet." << endl;
    else
    {
        while (current != NULL && listCount > 0)
        {
            if (current->info.getEmployeeNumber() == employee_number)
            {
                current->info.printPayroll();
            }
            listCount--;
            current = current->link;
        } //end while
    } //end else
    }

以下是链表的所有功能。请注意,此头文件没有任何成员变量,并且派生自具有第一个和最后一个指针的虚拟基类。另请注意,第一个和最后一个在基类中受到保护。

template <class Type>
bool orderedLinkedList<Type>::search(const Type& searchItem) const
{
    bool found = false;
    nodeType<Type> *current; //pointer to traverse the list

    current = first;  //start the search at the first node

    while (current != NULL && !found)
        if (current->info >= searchItem)
            found = true;
        else
            current = current->link;

    if (found)
        found = (current->info == searchItem); //test for equality

    return found;
}//end search

template <class Type>
void orderedLinkedList<Type>::insert(const Type& newItem)
{
    nodeType<Type> *current; //pointer to traverse the list
    nodeType<Type> *trailCurrent = NULL;//pointer just before current
    nodeType<Type> *newNode;  //pointer to create a node

    bool  found;

    newNode = new nodeType<Type>; //create the node
    newNode->info = newItem;   //store newItem in the node
    newNode->link = NULL;      //set the link field of the node
                               //to NULL

    if (first == NULL)  //Case 1
    {
        first = newNode;
        last = newNode;
        count++;
    }
    else
    {
        current = first;
        found = false;

        while (current != NULL && !found) //search the list
            if (current->info >= newItem)
                found = true;
            else
            {
                trailCurrent = current;
                current = current->link;
            }

        if (current == first)      //Case 2
        {
            newNode->link = first;
            first = newNode;
            count++;
        }
        else                       //Case 3
        {
            trailCurrent->link = newNode;
            newNode->link = current;

            if (current == NULL)
                last = newNode;

            count++;
        }
    }//end else
}//end insert

template<class Type>
void orderedLinkedList<Type>::insertFirst(const Type& newItem)
{
    insert(newItem);
}//end insertFirst

template<class Type>
void orderedLinkedList<Type>::insertLast(const Type& newItem)
{
    insert(newItem);
}//end insertLast

template<class Type>
void orderedLinkedList<Type>::deleteNode(const Type& deleteItem)
{
    nodeType<Type> *current; //pointer to traverse the list
    nodeType<Type> *trailCurrent = NULL; //pointer just before current
    bool found;

    if (first == NULL) //Case 1
        cout << "Cannot delete from an empty list." << endl;
    else
    {
        current = first;
        found = false;

        while (current != NULL && !found)  //search the list
            if (current->info >= deleteItem)
                found = true;
            else
            {
                trailCurrent = current;
                current = current->link;
            }

        if (current == NULL)   //Case 4
            cout << "The item to be deleted is not in the "
            << "list." << endl;
        else
            if (current->info == deleteItem) //the item to be 
                                             //deleted is in the list
            {
                if (first == current)       //Case 2
                {
                    first = first->link;

                    if (first == NULL)
                        last = NULL;

                    delete current;
                }
                else                         //Case 3
                {
                    trailCurrent->link = current->link;

                    if (current == last)
                        last = trailCurrent;

                    delete current;
                }
                count--;
            }
            else                            //Case 4
                cout << "The item to be deleted is not in the "
                << "list." << endl;
    }
}//end deleteNode

这是虚拟基类:

#pragma once
#ifndef H_LinkedListType
#define H_LinkedListType
#include <iostream>
#include <cassert>
using namespace std;
//Definition of the node
template <class Type>
struct nodeType
{
    Type info;
    nodeType<Type> *link;
};
template <class Type>
class linkedListIterator
{
public:
    linkedListIterator();
    linkedListIterator(nodeType<Type> *ptr);
    Type operator*();
    linkedListIterator<Type> operator++();
    bool operator==(const linkedListIterator<Type>& right) const;
    bool operator!=(const linkedListIterator<Type>& right) const;
private:
    nodeType<Type> *current; //pointer to point to the current 
};
template <class Type>
linkedListIterator<Type>::linkedListIterator()
{
    current = NULL;
}
template <class Type>
linkedListIterator<Type>::
linkedListIterator(nodeType<Type> *ptr)
{
    current = ptr;
}
template <class Type>
Type linkedListIterator<Type>::operator*()
{
    return current->info;
}
template <class Type>
linkedListIterator<Type> linkedListIterator<Type>::operator++()
{
    current = current->link;
    return *this;
}
template <class Type>
bool linkedListIterator<Type>::operator==
(const linkedListIterator<Type>& right) const
{
    return (current == right.current);
}
template <class Type>
bool linkedListIterator<Type>::operator!=
(const linkedListIterator<Type>& right) const
{
    return (current != right.current);
}
//*****************  class linkedListType   ****************
template <class Type>
class linkedListType
{
public:
    const linkedListType<Type>& operator=
        (const linkedListType<Type>&);
    void initializeList();
    bool isEmptyList() const;
    void print() const;
    int length() const;
    void destroyList();
    Type front() const;
    Type back() const;
    virtual bool search(const Type& searchItem) const = 0;
    virtual void insertFirst(const Type& newItem) = 0;
    virtual void insertLast(const Type& newItem) = 0;
    virtual void deleteNode(const Type& deleteItem) = 0;
    linkedListIterator<Type> begin();
    linkedListIterator<Type> end();
    linkedListType();
    linkedListType(const linkedListType<Type>& otherList);
    ~linkedListType();
protected:
    int count;   //variable to store the number of elements in the list
    nodeType<Type> *first; //pointer to the first node of the list
    nodeType<Type> *last;  //pointer to the last node of the list
private:
    void copyList(const linkedListType<Type>& otherList);
};
template <class Type>
bool linkedListType<Type>::isEmptyList() const
{
    return(first == NULL);
}
template <class Type>
linkedListType<Type>::linkedListType() //default constructor
{
    first = NULL;
    last = NULL;
    count = 0;
}
template <class Type>
void linkedListType<Type>::destroyList()
{
    nodeType<Type> *temp;   //pointer to deallocate the memory
                            //occupied by the node
    while (first != NULL)   //while there are nodes in the list
    {
        temp = first;        //set temp to the current node
        first = first->link; //advance first to the next node
        delete temp;   //deallocate the memory occupied by temp
    }
    last = NULL; //initialize last to NULL; first has already
                 //been set to NULL by the while loop
    count = 0;
}
template <class Type>
void linkedListType<Type>::initializeList()
{
    destroyList(); //if the list has any nodes, delete them
}
template <class Type>
void linkedListType<Type>::print() const
{
    nodeType<Type> *current; //pointer to traverse the list

    current = first;    //set current so that it points to 
                        //the first node
    while (current != NULL) //while more data to print
    {
        cout << current->info << " ";
        current = current->link;
    }
}//end print
template <class Type>
int linkedListType<Type>::length() const
{
    return count;
}  //end length
template <class Type>
Type linkedListType<Type>::front() const
{
    assert(first != NULL);

    return first->info; //return the info of the first node 
}//end front
template <class Type>
Type linkedListType<Type>::back() const
{
    assert(last != NULL);

    return last->info; //return the info of the last node   
}//end back
template <class Type>
linkedListIterator<Type> linkedListType<Type>::begin()
{
    linkedListIterator<Type> temp(first);

    return temp;
}
template <class Type>
linkedListIterator<Type> linkedListType<Type>::end()
{
    linkedListIterator<Type> temp(NULL);

    return temp;
}
template <class Type>
void linkedListType<Type>::copyList
(const linkedListType<Type>& otherList)
{
    nodeType<Type> *newNode; //pointer to create a node
    nodeType<Type> *current; //pointer to traverse the list
    if (first != NULL) //if the list is nonempty, make it empty
        destroyList();
    if (otherList.first == NULL) //otherList is empty
    {
        first = NULL;
        last = NULL;
        count = 0;
    }
    else
    {
        current = otherList.first; //current points to the list to be copied
        count = otherList.count;
        //copy the first node
        first = new nodeType<Type>;  //create the node
        first->info = current->info; //copy the info
        first->link = NULL;        //set the link field of the node to NULL
        last = first;              //make last point to the first node
        current = current->link;     //make current point to
        while (current != NULL)
        {
            newNode = new nodeType<Type>;  //create a node
            newNode->info = current->info; //copy the info
            newNode->link = NULL;       //set the link of newNode to NULL
            last->link = newNode;  //attach newNode after last
            last = newNode;        //make last point tothe actual last node
            current = current->link;   //make current point to the next node
        }//end while
    }//end else
}//end copyList
template <class Type>
linkedListType<Type>::~linkedListType() //destructor
{
    destroyList();
}//end destructor
template <class Type>
linkedListType<Type>::linkedListType
(const linkedListType<Type>& otherList)
{
    first = NULL;
    copyList(otherList);
}//end copy constructor
 //overload the assignment operator
template <class Type>
const linkedListType<Type>& linkedListType<Type>::operator=
(const linkedListType<Type>& otherList)
{
    if (this != &otherList) //avoid self-copy
    {
        copyList(otherList);
    }//end else

    return *this;
}
#endif

他说我可以从主程序访问所有公共函数,并使用返回列表的第一个指针的函数来初始化当前指针。我该怎么做?

谢谢!

1 个答案:

答案 0 :(得分:0)

啊,链表使用标准begin()函数,它给你一个迭代器。您的代码需要使用迭代器来遍历每个节点 - 您永远不会直接处理节点指针。

所以你的InList将从获取迭代器开始:

linkedListIterator<Payroll> iter = P.begin();

使用*iter

访问迭代器节点的工资核算对象

使用++iter

将迭代器推进到下一个节点

但在做这些事情之前,你必须确保你不在列表的末尾。要检查最后是否结束,您可以使用if(iter != P.end()) {...}

我现在暂时把它们放在一起 - 我不想破坏你的乐趣!

修改

看起来*iter可能不是访问Payroll对象的最佳方式。要访问Payroll成员,您应该能够使用->运算符。

iter->info.getEmployeeNumber()

这样的东西

修改

使用它似乎不是一个好主意,但这是你可以添加到orderedLinkedList以从const引用中获取first的成员函数。

声明(在class orderedLinkedList ... {区内,在公共部分):

const nodeType<Type>* GetHeadNode() const;

定义:

template <class Type>
const nodeType<Type>* orderedLinkedList<Type>::GetHeadNode() const {
    return first; 
}

您可能需要确保使用此指针在Payroll类中访问的任何成员函数也是const。如果你真的需要从const移除const nodeType<Type>*,它仍然有效。