PDA

View Full Version : سوال: مشکل با لیست پیوندی



sayeh1991
پنج شنبه 09 اردیبهشت 1389, 23:28 عصر
سلام

کسی میتونه بگه مشکل این کد چیه؟




#include<iostream>
using namespace std;
struct ScoreNode
{
char courseName[20];
int score;
ScoreNode*next;
};
struct StudentNode
{
char studentName[20];
ScoreNode*first;
StudentNode*next;
};
void AddStudent( StudentNode*& studentFirst,const char* studentName);
int count=0;
int main()
{
// int count=0;
// count++;
return 0;
}
void AddStudent( StudentNode*& studentFirst,const char studentName)
{
studentFirst=new StudentNode;
StudentNode*Next=new StudentNode;
studentFirst->next=Next;
/*for(int i=0;i<count;i++)
{*/
StudentNode* temp=new StudentNode;
Next->next=temp;
temp->studentName=studentName;
Next=temp;
//}
next->next=0;
}

sayeh1991
جمعه 10 اردیبهشت 1389, 10:11 صبح
خب نمیدونم باید چی کارش کنم...:ناراحت::ناراحت:

sayeh1991
شنبه 11 اردیبهشت 1389, 23:58 عصر
ممنون ولی الان run time eror داره...


#include<iostream>
#include<cstring>
using namespace std;

struct ScoreNode
{
char courseName[20];
int score;
ScoreNode*next;
};

struct StudentNode
{
char studentName[20];
ScoreNode*first;
StudentNode*next;
}*first;

void AddStudent( StudentNode*& studentFirst,const char* studentName);
void AddScore( StudentNode*& studentFirst,const char* studentName,const char* courseName,int score);
void DeleteScore( StudentNode*& studentFirst,const char* studentName,const char* courseName);

int count=0;

int main()
{
// int count=0;
// count++;
StudentNode *newstd=new StudentNode;
cin>>newstd->studentName;
StudentNode*p;
p=first;
while(p->next!=0)
{
p=p->next;
if(!strcmp(newstd->studentName,p->studentName))
AddStudent(first,newstd->studentName);
}
return 0;
}

void AddStudent( StudentNode*& studentFirst,const char* studentName)
{
studentFirst=new StudentNode;
StudentNode*Next=new StudentNode;
studentFirst->next=Next;
StudentNode* temp=new StudentNode;
Next->next=temp;
strcpy(temp->studentName,studentName);
}

/*void AddScore( StudentNode*& studentFirst,const char* studentName,const char* courseName,int score)
{
studentFirst=new StudentNode;
StudentNode*p=studentFirst;
while(p->next!=0)
{
if(strcmp(p->studentName,studentName))
{
StudentNode*q;
q=
*/
template<class T>
int FindNode(StudentNode*t,const T* stdName)
{
StudentNode*p;
p=t;
while(p->next!=NULL && !strcmp(p->studentName,stdName))
p==p->next;
if(strcmp(p->studentName,stdName))
return 1;
else
return 0;
}

/*void AddScore( StudentNode*& studentFirst,const char* studentName,const char* courseName,int score)
{
if(FindNode(studentFirst,studentName))
if(!FindNode(studentFirst,courseName))
{
studentFirst=new StudentNode;
StudentNode*Next=new StudentNode;
studentFirst->next=Next;
StudentNode* temp=new StudentNode;
Next->next=temp;
temp->first=new ScoreNode;
strcpy(temp->first->courseName,courseName);
strcpy(temp->first->score,score);
}
}*/
void DeleteScore( StudentNode*& studentFirst,const char* studentName,const char* courseName)
{
if(strcmp(studentFirst->first->courseName,courseName))
first=first->next;
else
{
StudentNode *p,*temp;
p=studentFirst;
while(p->next!=0 && p->first->courseName!=courseName)
{
temp=p;
p=p->next;
}
}
}

sayeh1991
یک شنبه 12 اردیبهشت 1389, 22:36 عصر
خب پس یعنی باید AddStudent رو هم تغیییر بدم.برای حالتی که لیست خالی باشه فرق میکنه...
بعد یه سوال دیگه...برای تابع deleteScore احتیاج نیست حافظه رو پاک کنم که garbage نشه؟
(خیلی ممنون از کمکتون)

sayeh1991
یک شنبه 12 اردیبهشت 1389, 23:57 عصر
اگه یه node رو delete کنیم زیر مجموعه هاشم delete میشه؟

sayeh1991
دوشنبه 13 اردیبهشت 1389, 01:01 صبح
خب این nodeفقط با یه دستور delete حذف میشه؟زیرمجموعه هاش garbage نمیشه؟