I'm try this simple linked-list program, this is the code :
student.h:
CODE
#include <stdio.h>
#include <stdlib.h>
#define MAX_ID 6
#define MAX_NAME 21
#define EQUAL 0
typedef struct date {
int dd;
int mm;
int yy;
} Date;
typedef struct student{
char studentID[MAX_ID]; /* A 5 character unique ID eg. 'S1234' */
char lastName[MAX_NAME]; /* Stores Last Name. No spaces! */
char firstName[MAX_NAME]; /* Stores rest of the name, inc. spaces*/
Date birthday; /* store the birthday of the student */
} StudentType;
typedef struct studentlistnode {
StudentType student;
struct studentlistnode *next;
} StudentListNode;
typedef StudentListNode * StudentListNodePtr;
extern StudentListNodePtr NewStudent(char *studentID, char* lastName,
char *firstName, Date birthday);
extern StudentListNodePtr AddStudent(StudentListNodePtr newStudent,
StudentListNodePtr head);
extern StudentListNodePtr DelStudent(char* studentID, StudentListNodePtr head);
student.c:
CODE
#include "student.h"
/* function implementation goes here */
/**/
StudentListNodePtr NewStudent(char *studentID, char* lastName,
char *firstName, Date birthday) {
StudentListNodePtr newN = malloc(sizeof(StudentType));
strncpy(newN->student.studentID, studentID, MAX_ID);
strncpy(newN->student.lastName, lastName, MAX_NAME);
strncpy(newN->student.firstName, firstName, MAX_NAME);
newN->student.birthday = birthday;
newN->next = NULL;
return newN;
}
/*add student*/
StudentListNodePtr AddStudent(StudentListNodePtr newStudent,
StudentListNodePtr head) {
StudentListNodePtr currentStudent;
currentStudent = head;
if(head == NULL){
/*newStudent->next = head;*/
head = newStudent;
}
else {
while(currentStudent->next!=NULL)
currentStudent = currentStudent->next;
currentStudent->next = newStudent;
}
return head;
}
/*delete student*/
StudentListNodePtr DelStudent(char* studentID, StudentListNodePtr head)
{
StudentListNodePtr prevStudent, curStudent, temp;
temp = NULL;
curStudent = head;
prevStudent = NULL;
if(strcmp(head->student.studentID, studentID) == EQUAL) {
temp = head;
head = head->next;
free(temp); /*============>error is right here*/
}
else {
while(curStudent != NULL) {
if(strcmp(curStudent->student.studentID, studentID) == EQUAL) {
temp = curStudent;
prevStudent->next = curStudent->next;
free(temp); /*==============>error is right here*/
}
else {
prevStudent = curStudent;
curStudent = curStudent->next;
}
}
}
return head;
}
when i try to code the driver class to test the function, the addStudent() function is ok, but when i try to delete and free the node, the error "invalid next size(fast)" is displayed. That doesn't happen when i just delete but not free the node.
Can anyone help me ?
*mod edit - please post code using the code tags, like this
This post has been edited by jjhaag: 5 Jan, 2008 - 04:37 AM