-1
我一直在尋找一個答案几天,但似乎無法找到它。我是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 for\n" << 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''中main'的'WeatherStats'結構是不公開的。只要使用它。但是,您的循環不會填充列表。 'LinkedList'的接口應該允許添加和遍歷其元素,否則它是無用的。 –
Inspired
我添加了我的LinkList類來更好地解釋,我也忽略了將填充列表的附加函數。這些指令表示用WeatherStats創建LinkList實例作爲數據類型。 LinkList是保存私有結構的東西。如果我徹底關閉了這一點,我不想浪費任何時間重新執行我的代碼。我已經回到了指針和類,但似乎無法找到解決方案 –