Написать класс ListNode, который имеет следующие свойства:C++ указатели/Списки реализации
- INT значение;
- ListNode * next;
обеспечивают следующие функции:
- ListNode (Int V, ListNode * L)
- INT GetValue();
- ListNode * getNext();
- void insert (int i);
- bool listcontains (int j);
Напишите программу, которая просит пользователя ввести некоторые целые и сохраняет их в качестве ListNodes, а затем запрашивает номер, который он должен искать в списке.
Вот мой код:
#include <iostream>
using namespace std;
class ListNode
{
private:
struct Node
{
int value;
Node *next;
} *lnFirst;
public:
ListNode();
int Length();
void DisplayList();
void Insert(int num);
bool Contains(int num);
int GetValue(int num);
};
ListNode::ListNode()
{
lnFirst = NULL;
}
int ListNode::Length()
{
Node *lnTemp;
int intCount = 0;
for(lnTemp=lnFirst ; lnTemp != NULL ; lnTemp = lnTemp->next)
{
intCount++;
}
return intCount;
}
void ListNode::DisplayList()
{
Node *lnTemp;
for(lnTemp = lnFirst ; lnTemp != NULL ; lnTemp = lnTemp->next)
cout<<endl<<lnTemp->value;
}
void ListNode::Insert(int num)
{
Node *lnCurrent, *lnNew;
if(lnFirst == NULL)
{
lnFirst = new Node;
lnFirst->value = num;
lnFirst->next = NULL;
}
else
{
lnCurrent = lnFirst;
while(lnCurrent->next != NULL)
lnCurrent = lnCurrent->next;
lnNew = new Node;
lnNew->value = num;
lnNew->next = NULL;
lnCurrent->next = lnNew;
}
}
bool ListNode::Contains(int num)
{
bool boolDoesContain = false;
Node *lnTemp,*lnCurrent;
lnCurrent = lnFirst;
lnTemp = lnCurrent;
while(lnCurrent!=NULL)
{
if(lnCurrent->value == num)
{
boolDoesContain = true;
return boolDoesContain;
}
lnTemp = lnCurrent;
lnCurrent = lnCurrent->next;
}
return boolDoesContain;
}
int ListNode::GetValue(int num)
{
Node *lnTemp;
int intCount = 1;
for(lnTemp=lnFirst; lnTemp != NULL; lnTemp = lnTemp->next)
{
if (intCount == num)
{
return lnTemp->value;
}
intCount++;
}
}
int main()
{
cout << "Input integers below. Input the integer -1 to stop inputting.\n\n";
ListNode lnList;
int intNode = 1, intInput = 0;
while (intInput != -1) {
cout << "Please input integer number " << intNode << ": "; cin >> intInput;
intNode++;
if (intInput != -1) { lnList.Insert(intInput); }
}
lnList.DisplayList();
cout << "\n\n";
int intListLength = lnList.Length();
cout << "Which value do you wish to recall? (Between 1 and " << intListLength << "): "; cin >> intNode;
if (intNode >= 1 && intNode <= intListLength) {
cout << "Value at position " << intNode << " is " << lnList.GetValue(intNode) << ".";
} else {
cout << "No such position in the list. Positions run from 1 to " << intListLength << ". You asked for " << intNode << ".";
}
cout << "\n\nCheck if the following value is in the list: "; cin >> intNode;
bool IsThere = lnList.Contains(intNode);
if (IsThere) {
cout << intNode << " is in the list.";
} else {
cout << intNode << " is not in the list.";
}
cout << "\n\n";
system("pause");
return 0;
}
Где мы можем улучшить это?
что говорить по-разному. например, ваш getValue имеет параметр, который принимает индекс. поэтому ваш ListNode на самом деле является списком, а не самим узлом. – 2008-11-27 13:58:40
Похоже, у вас есть хорошие ответы здесь, но я уверен, что [Code Review] (http://codereview.stackexchange.com/) будет благодарен за ваше участие в подобных вопросах. – Collin 2012-03-21 12:54:43
Вопрос был задан в ноябре 2008 года ... Я не думаю, что к тому времени SO даже не попал в частную/публичную бета-версию. Не говоря уже о Трилогии или SE2.0! :-) – 2012-03-23 15:57:03