Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Linked List Problem

Status
Not open for further replies.

codehead

Programmer
Aug 25, 1999
96
0
0
US
Hello,<br>
<br>
I can't get the following code to compile. It compiles O.K. if you REM out the second list code. Is VC++ getting confused with my names or is it a structual problem?<br>
<br>
TIA,<br>
<br>
Bob<br>
<br>
#include &lt;iostream.h&gt;<br>
<br>
class CharList {<br>
public:<br>
char Character;<br>
char CharacterTwo;<br>
CharList *Next;<br>
secondCharacterList *secondNext;<br>
};<br>
<br>
//Input first list<br>
char GetCharacter() {<br>
char Character;<br>
<br>
cout &lt;&lt; &quot;What is the List 1 character?\n&quot;;<br>
cin &gt;&gt; Character;<br>
return Character;<br>
}<br>
<br>
char GetCharacterTwo() {<br>
char CharacterTwo;<br>
<br>
cout &lt;&lt; &quot;What is the List 2 character?\n&quot;;<br>
cin &gt;&gt; CharacterTwo;<br>
return CharacterTwo;<br>
}<br>
<br>
<br>
//Here is where the program begins<br>
void main()<br>
{<br>
int i;<br>
char Character;<br>
char CharacterTwo;<br>
<br>
//This points to the current List1 IntList item<br>
CharList *ListPtr;<br>
<br>
//This points to the last List1 IntList item created<br>
CharList *LastPtr = 0;<br>
<br>
//This points to the first List1 item in the list<br>
CharList *First = 0;<br>
<br>
//Read in characters from the user<br>
for (i=0; i&lt;2; i++)<br>
{<br>
Character = GetCharacter();<br>
//Create a new structure to hold the character.<br>
ListPtr = new CharList;<br>
<br>
//Fill in the structures values.<br>
ListPtr-&gt;Character = Character;<br>
ListPtr-&gt;Next = 0;<br>
<br>
//If this isn't the first item created,<br>
//connect it to the list. Otherwise, it<br>
//is the start of the list.<br>
if (LastPtr)<br>
//LastPtr-&gt;Next is the Next pointer from<br>
//the last item in the list. Connect it.<br>
LastPtr-&gt;Next = ListPtr;<br>
else<br>
//If this is the first item, be sure<br>
//to save where it starts.<br>
First = ListPtr;<br>
<br>
//Set the LastPtr<br>
LastPtr = ListPtr;<br>
};<br>
<br>
// Print out<br>
if (First) {<br>
cout &lt;&lt; &quot;The list is &quot;;<br>
<br>
//Now use ListPtr to traverse the list<br>
ListPtr = First;<br>
<br>
//Read through the list until Next is null<br>
do {<br>
//Print out the number<br>
cout &lt;&lt; ListPtr-&gt;Character &lt;&lt; &quot; &quot;;<br>
<br>
// Move on to the next item in the list<br>
ListPtr = ListPtr-&gt;Next;<br>
}<br>
//Stop when ListPtr is 0<br>
while (ListPtr);<br>
<br>
cout &lt;&lt; endl;<br>
} //end of if<br>
<br>
//This points to the current List2 IntList item<br>
secondCharacterList *secondListPtr;<br>
<br>
//This points to the last List2 IntList item created<br>
secondCharacterList *secondLastPtr = 0;<br>
<br>
//This points to the first List2 item in the list<br>
secondCharacterList *secondFirst = 0;<br>
<br>
//Read in characters from the user<br>
for (i=0; i&lt;2; i++)<br>
{<br>
CharacterTwo = GetCharacterTwo();<br>
//Create a new structure to hold the character.<br>
//ListPtr will now point to this structure.<br>
secondListPtr = new secondCharacterList;<br>
<br>
//Fill in the structures values.<br>
secondListPtr-&gt;CharacterTwo = CharacterTwo;<br>
secondListPtr-&gt;secondNext = 0;<br>
<br>
//If this isn't the first item created,<br>
//connect it to the list. Otherwise, it<br>
//is the start of the list.<br>
if (secondLastPtr)<br>
//LastPtr-&gt;Next is the Next pointer from<br>
//the last item in the list. Connect it.<br>
secondLastPtr-&gt;secondNext = secondListPtr;<br>
else<br>
//If this is the first item, be sure<br>
//to save where it starts.<br>
secondFirst = secondListPtr;<br>
<br>
//Set the LastPtr<br>
secondLastPtr = secondListPtr;<br>
};<br>
<br>
// Print out.<br>
if (secondFirst) {<br>
cout &lt;&lt; &quot;The list is &quot;;<br>
<br>
//Now use ListPtr to traverse the list<br>
secondListPtr = secondFirst;<br>
<br>
//Read through the list until Next is null<br>
do {<br>
//Print out the number<br>
cout &lt;&lt; secondListPtr-&gt;CharacterTwo &lt;&lt; &quot; &quot;;<br>
<br>
//Now move on to the next item in the list<br>
secondListPtr = secondListPtr-&gt;secondNext;<br>
}<br>
//Stop when ListPtr is 0<br>
while (secondListPtr);<br>
<br>
cout &lt;&lt; endl;<br>
} //end of if<br>
<br>
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top