0

我正在编写带有指针和结构数组的代码。代码是:

struct Student_List{
    int roll;
    char name[20];
    int mark1;
    int mark2;
    int mark3;
    struct Student_List *next;
};
struct Student_List *Class[] = { NULL };// to generate student's list for various classes.
struct Student_List *current =NULL;

struct Student_List * check(int rollNo, int classNo)
{

struct Student_List *temp=NULL;
temp=Class[classNo];

while (temp!=NULL) {
    if(temp->rollNo == rollNo)
    {
        //element is found
        return temp;
    }

    temp = temp->next;
}    
if(temp==NULL)
{
    //element not found
    return NULL;
}
//scan serially and if found return address of that node
//if no element found return NULL

}

//add elements to list pointed by Class
struct Student_List * add(char studentName, int rollNo,int classNo)
{
    struct Studen_List *newNode=(struct Student_List *)malloc(sizeof(struct Student_List ));
if (newNode == NULL) {
        printf("malloc failed\n");
        }  

newNode->roll=rollNo;
strcpy(newNode->name,studentName);
newNode->mark1=0;
newNode->mark2=0;
newNode->mark3=0;
struct Student_List *temp = NULL, *previous = NULL;

temp=Class[classNo];
prev = temp;
}

if(temp==NULL)
{
    Class[classNo]=newNode;
    return Class[classNo];


}

while(temp!=NULL)
{
    prev=temp;
    temp=temp->next;
}

prev->next=newNode;
return newNode;
//add node to end of the list
}

void Delete_List(struct Student_List *temp)
{
delete []temp;
//temp = NULL;

/*while(temp!=NULL)
{
    struct Student_List *del=temp;
    temp=temp->next;
    free(del);

}*/
}

int main();
{
int classNo,rollNo,i;
char *name;

printf("\nEnter Class No: ");
scanf("%d",&classNo);
printf("\nEnter Name: ")
gets(name);
printf("\nEnter Roll No: ");
scanf("%d",&rollNo);


current = check(rollNo,classNo);

if(current == NULL){
current = add(name,rollNo,classNo);
}
// others is the marks data fetched from file and calculations.
// this code is enough to reproduce my error.

for(int i=0;i<10;i++)
{
    Delete_List(Class[i]);
}
  return;
}

问题:当我分配值并执行计算时,结果是错误的。在调试时我发现我Class[3]的起始地址与Class[1]->nextRoll->nextRoll;

我无法删除它。请帮我删除循环。欢迎任何有关语法或建议的帮助。

4

1 回答 1

2

你不应该使用malloc,使用new

struct Student_List{
    int roll;
    char name[20];
    int mark1;
    int mark2;
    int mark 3;
    Student_List *nextRoll;
};

Student_List * Class[] = { NULL };

int main();
{
    // various initializations

    // for each new student, do this
    Student_List *newNode=new Student_List;
    newNode->next = Class[0]; // 0 is the Class of the student
    Class[0] = newNode;

    // code to fetch data from text file and perform calculations.

    // do not delete Class; !!
}

请注意,您的方法是错误的,您应该尽可能使用 STL。例子:

struct Student {
    int roll;
    std::string name;
    int mark1;
    int mark2;
    int mark3;
};

typedef std::list< Student > Class;

typedef std::map< std::string, Class > Classes;
Classes classes;

int main()
{
    Student s;
    s.name = "Daniel";
    // ...

    classes[ "1b" ].push_back( s ); // Add student to a class "1b".
}
于 2013-10-15T06:25:22.740 回答