By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,116 Members | 1,319 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,116 IT Pros & Developers. It's quick & easy.

Linked list

P: 1
I just started to leard how to program and i need to output numbers that i entered. the user inputs how many students are in the class and then it tells him to input test scores. but i have to output the scores in the order that they were entered in. here is what i have so far, i don't even know if it's right.

#include <iostream>
#include <iomanip>

using namespace std;

struct NODE
{
NODE *pNext;
NODE *pPrev;
int nData;
};

NODE *pHead, *pTail;

void AppendNode (NODE *pNode)
{
if (pHead == NULL)
{
pHead = pNode;
pNode->pPrev = NULL;
}
else
{
pTail->pNext = pNode;
pNode->pPrev = pTail;
}
pTail = pNode;
pNode->pNext = NULL;
}

void InsertNode (NODE *pNode, NODE *pAfter)
{
pNode->pNext = pAfter->pNext;
pNode->pPrev = pAfter;

if (pAfter->pNext != NULL)
pAfter->pNext->pPrev = pNode;
else
pTail = pNode;
pAfter->pNext = pNode;
}

void RemoveNode (NODE *pNode)
{
if (pNode->pPrev == NULL)
pHead = pNode->pNext;
else
pNode->pPrev->pNext = pNode->pNext;
if (pNode->pNext == NULL)
pTail = pNode->pPrev;
else
pNode->pNext->pPrev = pNode->pPrev;
}

void DeleteAllNodes ()
{
while (pHead != NULL)
RemoveNode (pHead);
}

int main ()
{
int students;
int score;

cout << "Enter The Number of Students in the class: " ;
cin >> students;

try
{
if (students < 1)
{
cout << "Umm.....you have to have 1 or 20 students." ;
cout << endl;
return 0;
}

else if (students > 20)
{
cout << "You could only input 20 students." ;
cout << endl;
return 0;
}
}

catch (...)
{
cout << "ERROR. PLEASE RESTART!" ;
return 0;
}

system ("cls");

NODE *pNode;


for (int i = 1; i <= students; i++ )
{

cout << "Exam Score For Student #" << i << ": ";
cin >> score;

if (score < 0 || score > 100)
{
cout << "Invalid. Score should be between 0 and 100.";
cout << endl;
continue;
}

pNode = new NODE;
pNode->nData = i;
AppendNode(pNode);

}


DeleteAllNodes ();



}





can someome help me?
Sep 15 '06 #1
Share this Question
Share on Google+
1 Reply


Banfa
Expert Mod 5K+
P: 8,916
Looks about right to me, here are the problems I see are

1. The new your nodes but you never delete them causing a memory leak

2. If someone enters an invalid score for a student they don't get to try again, the student is just left off the list.

3. Some of the output text messages are not quite correct.

4. You need to add something to print out the values in the order they are in the list.
Sep 15 '06 #2

Post your reply

Sign in to post your reply or Sign up for a free account.