如何使用C语言在双链表中添加和删除节点?



我有3个结构节点:

struct Student{
char id[255];
char name[255];
float gpa;
};
struct Elemen{
struct Student *mhs;
struct Elemen *next;
};
struct List{
struct Elemen *first;
};

如果我将数据添加到学生节点,则添加的数据不能再多了,代码如下:

int main() 
{ 
head = (struct List*)malloc(sizeof(struct List));   
after = (struct List*)malloc(sizeof(struct List));  
last = (struct List*)malloc(sizeof(struct List));

after->first = (struct Elemen*)malloc(sizeof(struct Elemen));
head->first = (struct Elemen*)malloc(sizeof(struct Elemen));
last->first = (struct Elemen*)malloc(sizeof(struct Elemen));

after->first->next = (struct Elemen*)malloc(sizeof(struct Elemen));
head->first->next = (struct Elemen*)malloc(sizeof(struct Elemen));
last->first->next = (struct Elemen*)malloc(sizeof(struct Elemen));

after->first->mhs = (struct Student*)malloc(sizeof(struct Student));
head->first->mhs = (struct Student*)malloc(sizeof(struct Student));
last->first->mhs = (struct Student*)malloc(sizeof(struct Student));

strcpy(head->first->mhs->id, "1"); 
strcpy(head->first->mhs->name, "Student 1"); 
head->first->mhs->gpa = 4;
head->first->next = after->first;

strcpy(after->first->mhs->id, "2"); 
strcpy(after->first->mhs->name, "Student 2");
after->first->mhs->gpa = 5;
after->first->next = last->first;

strcpy(last->first->mhs->id, "3"); 
strcpy(last->first->mhs->name, "Student 3");
last->first->mhs->gpa = 6;
last->first->next = NULL;

printAllElemen(head);
return 0; 
}
void printAllElemen(struct List *e){
printf("IDt|Namet|GPAn");
while(e->first != NULL)
{
printf("%st|%st|%.2fn", e->first->mhs->id, e->first->mhs->name, e->first->mhs->gpa);
e->first = e->first->next;
}
}

像这样的程序示例:

void addFirst(char id[], char name[], float gpa, struct List *e);
void addAfter(struct Elemen *prev, char id[], char name[], float gpa, struct List *e);
void addLast(char id[], char name[], float gpa, struct List *e);
void deleteFirst(struct List *e);
void deleteAfter(struct Elemen *prev, struct List *e);
void deleteLast(struct List *e);

我的问题是如何在功能上添加数据节点,并且节点列表中的数据可以超过1?

感谢

在此处重写代码。

void addFirst(const char *id, const char *name, float gpa, struct List *e);
void addAfter(struct Elemen *prev, const char *id, const char *name, float gpa, struct List *e);
void addLast(const char *id, const char *name, float gpa, struct List *e);
void deleteFirst(struct List *e);
void deleteAfter(struct Elemen *prev, struct List *e);
void deleteLast(struct List *e);


void addFirst(const char *id, const char *name, float gpa, struct List *e){
struct Elemen*newE = (struct Elemen*)malloc(sizeof(struct Elemen));
newE->mhs = (struct Student*)malloc(sizeof(struct Student));
strcpy(newE->mhs->id, id);
strcpy(newE->mhs->name,name);
newE->mhs->gpa = gpa;
newE->next = e->first->next;
e->first = newE;
}
void addAfter(struct Elemen *prev, const char *id, const char *name, float gpa, struct List *e){
struct Elemen*newE = (struct Elemen*)malloc(sizeof(struct Elemen));
newE->mhs = (struct Student*)malloc(sizeof(struct Student));
strcpy(newE->mhs->id, id);
strcpy(newE->mhs->name,name);
newE->mhs->gpa = gpa;
newE->next = 0;
struct Elemen*ptr = e->first;
if(ptr == 0){ // emptry list
e->first = newE;
return;
}
while(ptr != prev) ptr = ptr->next;
newE->next = ptr->next;
ptr->next = newE;
}

void addLast(const char *id, const char *name, float gpa, struct List *e){
struct Elemen*newE = (struct Elemen*)malloc(sizeof(struct Elemen));
newE->mhs = (struct Student*)malloc(sizeof(struct Student));
strcpy(newE->mhs->id, id);
strcpy(newE->mhs->name,name);
newE->mhs->gpa = gpa;
newE->next = 0;
// insert new node
struct Elemen *ptr = e->first;
if(ptr == 0){ // there are no nodes in list.
e->first = newE;
}else{
while(ptr->next != 0) ptr = ptr->next; // find last node
ptr->next = newE;
}
}
void deleteFirst(struct List *e){
if(e->first == 0) return; // empty list
struct Elemen*firptr = e->first;
e->first = e->first->next;
free(firptr);
}
void deleteAfter(struct Elemen *prev, struct List *e){
struct Elemen*ptr = e->first;
if(ptr == 0){ // emptry list
return;
}
while((ptr != prev) && ptr->next != 0) ptr = ptr->next;

Elemen*ptr1 = ptr->next;
ptr->next = ptr->next->next;
free(ptr1);
}
void deleteLast(struct List *e){
struct Elemen*ptr = e->first;
struct Elemen*prev = ptr;
if(ptr == 0){ // emptry list
return;
}
if(ptr->next == 0) {
free(e->first);e->first = 0;return;
} // only first node exists.
while((ptr->next != 0) { prev = ptr;ptr = ptr->next;}
free(ptr);
prev->next = 0;

}

void printAllElemen(struct List *e){
printf("IDt|Namet|GPAn");
struct Elemen*ptr = e->first;
while(ptr != 0)
{
printf("%st|%st|%.2fn", ptr->mhs->id, ptr->mhs->name, ptr->mhs->gpa);
ptr = ptr->next;
}
}




int main(){
struct List*mList = (struct List*)malloc(sizeof(struct List));
mList->first = 0;
addFirst("1", "stud1", 4, mList);
addLast("2", "std2", 5, mList);
addLast("3", "std3", 6, mList);
printAllElemen(head);
return 0; 
}

相关内容

  • 没有找到相关文章

最新更新