Note that there are some explanatory texts on larger screens.

plurals
  1. POC++ Pointers / Lists Implementation
    text
    copied!<blockquote> <p>Write a class ListNode which has the following properties:</p> <ul> <li>int value;</li> <li>ListNode *next; </li> </ul> <p>Provide the following functions:</p> <ul> <li>ListNode(int v, ListNode *l) </li> <li>int getValue(); </li> <li>ListNode* getNext(); </li> <li>void insert(int i); </li> <li>bool listcontains(int j); </li> </ul> <p>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.</p> </blockquote> <p>Here is my code:</p> <pre><code>#include &lt;iostream&gt; 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-&gt;next ) { intCount++; } return intCount; } void ListNode::DisplayList() { Node *lnTemp; for( lnTemp = lnFirst ; lnTemp != NULL ; lnTemp = lnTemp-&gt;next ) cout&lt;&lt;endl&lt;&lt;lnTemp-&gt;value; } void ListNode::Insert(int num) { Node *lnCurrent, *lnNew; if( lnFirst == NULL ) { lnFirst = new Node; lnFirst-&gt;value = num; lnFirst-&gt;next = NULL; } else { lnCurrent = lnFirst; while( lnCurrent-&gt;next != NULL ) lnCurrent = lnCurrent-&gt;next; lnNew = new Node; lnNew-&gt;value = num; lnNew-&gt;next = NULL; lnCurrent-&gt;next = lnNew; } } bool ListNode::Contains(int num) { bool boolDoesContain = false; Node *lnTemp,*lnCurrent; lnCurrent = lnFirst; lnTemp = lnCurrent; while( lnCurrent!=NULL ) { if( lnCurrent-&gt;value == num ) { boolDoesContain = true; return boolDoesContain; } lnTemp = lnCurrent; lnCurrent = lnCurrent-&gt;next; } return boolDoesContain; } int ListNode::GetValue(int num) { Node *lnTemp; int intCount = 1; for( lnTemp=lnFirst; lnTemp != NULL; lnTemp = lnTemp-&gt;next ) { if (intCount == num) { return lnTemp-&gt;value; } intCount++; } } int main() { cout &lt;&lt; "Input integers below. Input the integer -1 to stop inputting.\n\n"; ListNode lnList; int intNode = 1, intInput = 0; while (intInput != -1) { cout &lt;&lt; "Please input integer number " &lt;&lt; intNode &lt;&lt; ": "; cin &gt;&gt; intInput; intNode++; if (intInput != -1) { lnList.Insert(intInput); } } lnList.DisplayList(); cout &lt;&lt; "\n\n"; int intListLength = lnList.Length(); cout &lt;&lt; "Which value do you wish to recall? (Between 1 and " &lt;&lt; intListLength &lt;&lt; "): "; cin &gt;&gt; intNode; if ( intNode &gt;= 1 &amp;&amp; intNode &lt;= intListLength ) { cout &lt;&lt; "Value at position " &lt;&lt; intNode &lt;&lt; " is " &lt;&lt; lnList.GetValue(intNode) &lt;&lt; "."; } else { cout &lt;&lt; "No such position in the list. Positions run from 1 to " &lt;&lt; intListLength &lt;&lt; ". You asked for " &lt;&lt; intNode &lt;&lt; "."; } cout &lt;&lt; "\n\nCheck if the following value is in the list: "; cin &gt;&gt; intNode; bool IsThere = lnList.Contains(intNode); if (IsThere) { cout &lt;&lt; intNode &lt;&lt; " is in the list."; } else { cout &lt;&lt; intNode &lt;&lt; " is not in the list."; } cout &lt;&lt; "\n\n"; system("pause"); return 0; } </code></pre> <p>Where can we improve this?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload