13.3 链表-按顺序插入和查找删除节点
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct Student
{
int id;
char name[16];
Student* next;
};
Student m_head = {0};
int insert(Student* obj)
{
Student* cur = m_head.next;
Student* pre = &m_head;
while(cur)
{
if(obj->id < cur->id)
break;
pre = cur;
cur = cur->next;
}
obj->next = pre->next;
pre->next = obj;
return 0;
}
void remove(int id)
{
Student* cur = m_head.next;
Student* pre = &m_head;
while(cur)
{
if(id == cur->id)
{
pre->next = cur->next;
free(cur);
break;
}
pre = cur;
cur = cur->next;
}
}
void show_all()
{
Student* p = m_head.next;
while(p)
{
printf("ID: %d, name: %s\n", p->id, p->name);
p = p->next;
}
}
int main()
{
Student* obj = NULL;
obj = (Student*)malloc (sizeof(Student));
obj->id = 8;
strcpy(obj->name, "888");
insert(obj);
obj = (Student*)malloc (sizeof(Student));
obj->id = 1;
strcpy(obj->name, "111");
insert(obj);
obj = (Student*)malloc (sizeof(Student));
obj->id = 4;
strcpy(obj->name, "444");
insert(obj);
obj = (Student*)malloc (sizeof(Student));
obj->id = 3;
strcpy(obj->name, "333");
insert(obj);
obj = (Student*)malloc (sizeof(Student));
obj->id = 5;
strcpy(obj->name, "555");
insert(obj);
remove(3);
remove(2);
show_all();
return 0;
}