对链表的函数访问



我已经搜索了几天的答案,但似乎找不到。我是C++的初学者,我的任何课程项目都是创建一个包含天气信息的课程。我的问题是,在这个项目中,我必须找到晴天、雪天和雨天最多和最少的月份。这个函数必须存在于main中。链表类来自以前的赋值,指令中没有说明要修改它。下面是我的主菜。正如你所看到的,这是一个相对简单的程序,它对月份进行计数,然后询问天数类型,然后将其附加到链接列表中。这部分程序运行良好,我只是不知道如何从类外部访问链表节点,因为结构是私有的。是否有方法或唯一选项可以修改链表类?

#include "LinkedList.h"
#include "weather stats.h"
void findHighLow(LinkList<WeatherStats> &);
void main()
{
LinkList<WeatherStats> weather;
WeatherStats stats;
double rain, snow;
int sun,months;
cout << "Please enter the amount of months you would like to store data forn" << endl;
cin >> months;
stats.setMonths(months);
cout << endl;
for(int i=0; i<months; i++)
{
    cout << "Enter amount of rainy days for " << i+1 << endl;
    cin >> rain;
    stats.setRain(rain);
    cout << endl;
    cout << "Enter amount of snowy days for " << i+1 << endl;
    cin >> snow;
    stats.setSnow(snow);
    cout << endl;
    cout << "Enter amount of sunny days for " << i+1 << endl;
    cin >> sun;
    stats.setSun(sun);
    cout << endl;
            weather.appendNode(stats);
}
weather.displayList();
}
void findHighLow(LinkList<WeatherStats> &value)
{
}

链接列表模板类

#ifndef LINKLIST_H
#define LINKLIST_H
#include <iostream>
using namespace std;
template <class L>
class LinkList
{
private:
    //structure for list
    //stores value of node
    //pointer to next node
    struct LinkNode
    {
        L value;
        struct LinkNode *next;
    };
    LinkNode *head;
public:
    //constructor
    //sets head to NULL
    LinkList()
    {
        head = NULL;
    }
    //destructor
    ~LinkList();
    //list functions
    void appendNode(L);
    void insertNode(L);
    void deleteNode(L);
    void displayList();
    void searchList(L);
    //overload operators
    friend ostream &operator << (ostream&, LinkList<L> &);
    L &operator = (LinkList<L> &);
    L &operator != (LinkList<L> &);
    L &operator == (LinkList<L> &);
    L &operator < (LinkList<L> &);
};
//gets value passed into value and appends to list
template <class L>
void LinkList<L>::appendNode(L value)
{
    LinkNode *newNode; //pointer to new node
    LinkNode *newPtr; //ptr to transverse list
    //creates new node and sets value and next pointer
    newNode = new LinkNode;
    newNode -> value = value;
    newNode -> next = NULL;
    //checks to see if there are no nodes
    //if there are none newNode is set to head
    if(!head)
        head = newNode;
    //inserts newNode at the end of list
    else
    {
        newPtr = head;
        while (newPtr -> next)
            newPtr = newPtr -> next;
        newPtr -> next = newNode;
    }
}
//gets value from value and inserts new node
template <class L>
void LinkList<L>::insertNode(L value)
{
    LinkNode *newNode; //pointer to new node
    LinkNode *newPtr; //pointer to transverse list
    LinkNode *prevNode = NULL; //sets previous node
    newNode = new LinkNode;
    newNode -> value = value;
    //checks for no nodes
    //if none head is set to newNode
    if(!head)
    {
        head = newNode;
        newNode -> next = NULL;
    }
    //inserts newNode at the end of list
    else
    {
        //pointer set to head
        newPtr = head;
        prevNode = NULL;
        //checks for nodes less than value passed
        while(newPtr != NULL && newPtr -> value < value)
        {
            prevNode = newPtr;
            newPtr = newPtr -> next;
        }
        //sets new node to first node
        if(prevNode == NULL)
        {
            head = newNode;
            newNode -> next = newPtr;
        }
        //puts node after previous node
        else
        {
            prevNode -> next = newNode;
            newNode -> next = newPtr;
        }
    }
}
//searches for passed value and deletes that value from list
template <class L>
void LinkList<L>::deleteNode(L value)
{
    LinkNode *newPtr;
    LinkNode *prevNode;
    if(!head)
        return;
    //checks to see if first node is the value passed
    if(head -> value == value)
    {
        newPtr = head -> next;
        delete head;
        head = newPtr;
    }
    //searches list for value
    else
    {
        newPtr = head;
        while(newPtr != NULL && newPtr -> value != value)
        {
            prevNode = newPtr;
            newPtr = newPtr -> next;
        }
        if(newPtr)
        {
            prevNode -> next = newPtr -> next;
            delete newPtr;
        }
    }
}
//displays list
template <class L>
void LinkList<L>::displayList()
{
    LinkNode *newPtr;
    newPtr = head;
    while(newPtr)
    {
        cout << newPtr -> value << endl;
        newPtr = newPtr -> next;
    }
}
//searches list
template <class L>
void LinkList<L>::searchList(L value)
{
    temp = value;
    LinkNode *newPtr;
    newPtr = head;
    while(newPtr)
    {
        if(newPtr -> value = value)
        {
            return temp;
        }
        newPtr = newPtr ->next;
    }
    return NULL;
}
//list distructor deletes all values in list
template<class L>
LinkList<L>::~LinkList()
{
    LinkNode *newPtr;
    LinkNode *nextNode;
    newPtr = head;
    while(newPtr != NULL)
    {
        nextNode = newPtr -> next;
        delete newPtr;
        newPtr = nextNode;
    }
}
//overload operators
template <class L>
ostream &operator << (ostream stream, LinkList<L> &obj)
{
    stream >> obj.value;
    return stream;
}
template <class L>
L &LinkList<L>::operator = (LinkList<L> &obj)
{
    value = obj.value;
    return value;
}
template <class L>
L &LinkList<L>::operator == (LinkList<L> &obj)
{
    if(value = obj.value && obj.value = value)
        return true;
    else
        return false;
}
template <class L>
L &LinkList<L>::operator != (LinkList<L> &obj)
{
    if(value != obj.value && obj.value != value)
        return true;
    else
        return false;
}
template <class L>
L &LinkList<L>::operator < (LinkList<L> &obj)
{
    bool status;
    if(value > obj.value)
        status = true;
    else if(value == obj.value)
        return true;
    else
        status false;
    return status;
}
#endif

您唯一的选择是修改LinkedList类

最有意义的是添加迭代支持:

使iterator成为另一个内部类(它只是指向LinkNode的指针)

添加一个返回指向head 的迭代器的begin()方法

和返回指向NULL 的迭代器的end()方法

iterator中添加一个(使其变得简单get()方法,该方法返回迭代器的LinkNode保持的值

iterator中添加一个(使其变得简单next()方法,该方法将迭代器指向的LinkNode更改为LinkNode的下一个

根据请求-未编写代码

相关内容

  • 没有找到相关文章

最新更新