C ++指针/列表实现(C++ Pointers / Lists Implementation)

系统教程 行业动态 更新时间:2024-06-14 16:58:30
C ++指针/列表实现(C++ Pointers / Lists Implementation)

编写一个具有以下属性的类ListNode:

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; }

我们在哪里可以改善这个?

Write a class ListNode which has the following properties:

int value; ListNode *next;

Provide the following functions:

ListNode(int v, ListNode *l) int getValue(); ListNode* getNext(); void insert(int i); bool listcontains(int j);

Write a program which asks the user to enter some integers and stores them as ListNodes, and then asks for a number which it should seek in the list.

Here is my code:

#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; }

Where can we improve this?

最满意答案

什么放松和ckarmann说。 这是一个提示,我实现listcontains,让你了解如何赋值:

class ListNode { private: int value; ListNode * next; public: bool listcontains(int v) { // does this node contain the value? if(value == v) return true; // was this the last node? if(next == 0) return false; // return whether nodes after us contain the value return next->listcontains(v); } };

因此,您只有列表的头部,它依次链接到下一个节点。 尾部将有next == 0 ;

What unwind and ckarmann say. Here is a hint, i implement listcontains for you to give you the idea how the assignment could be meant:

class ListNode { private: int value; ListNode * next; public: bool listcontains(int v) { // does this node contain the value? if(value == v) return true; // was this the last node? if(next == 0) return false; // return whether nodes after us contain the value return next->listcontains(v); } };

So, you only have the head of the list, which links to the next node in turn. The tail will have next == 0;

更多推荐

本文发布于:2023-04-15 03:37:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/dzcp/8e6980289092984a20d92110e45515f0.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:指针   列表   Pointers   Lists   Implementation

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!