C语言简单的链表操作
链表(Linked List)是C语言中一种常见的数据结构,它通过节点(Node)的集合以及节点之间的链接来存储数据。链表不像数组那样在内存中连续存储,而是可以动态地分配和释放内存,因此在处理大量数据或需要频繁插入和删除操作时,链表比数组更加高效和灵活。
1.链表的创建
struct person
{
char *name;
int *age;
struct person *next;
};
struct list{
char *name;
struct person *next;
};
首先先定义一个结构体类型,struct person里面包含人的名字、年龄以及下一个人。
定义一个链表list,里面包含姓名和下一个人。
2.链表初始化
void InitList(struct list *pList, char *name)
{
pList->name = name;
pList->next = NULL;
}
将链表初始化,链表名字指向传入的名字,传入姓名的下一个人为NULL;
3.往链表尾部插入一个元素
void AddItemToList(struct list *pList, struct person *new_person)
{
struct person *last;
//如果是空链表
if(pList->next == NULL)
{
pList->next = new_person;
new_person->next = NULL;
}
else if(pList->next != NULL)
{
last = pList->next;
while (last->next)
{
last = last ->next;
}
last->next = new_person;
new_person->next = NULL;
}
}
该函数一次插入一个元素。往链表里插入元素有两种情况。一种是空链表,另一种是非空链表。空链表很好理解,直接说非空链表的情况。通过对链表进行遍历操作,找到链表的最后的位置,往链表插入元素即可;
4.往链表指定位置插入
void AddItemToListPoint(struct list *pList,struct person *oldperson, struct person *new_person)
{
struct person *last = &pList->head;
struct person *temp = oldperson->next;
//如果是空链表
while(last->next!=oldperson)
{
last = last->next;
}
oldperson->next = NULL;
last->next = new_person;
new_person->next = temp;
}
该函数是往链表指定位置插入一个元素,该函数传入的参数有三个,struct list 链表、
struct person *oldperson, struct person *newperson, 实现原理跟上一个插入函数差不多,这个是通过查找到oldperson的地方,然后往后面插入一个newperson;
5.删除链表元素
void DelItemFromList(struct list *plist, struct person *person)
{
struct person *p = plist->next;
struct person *pre = NULL;
//如果是头节点
if(p!=NULL&&p==person)
{
plist->next = p->next;
}
else if(p!=NULL&&p!=person)//要删除的不是头结点
{
if(person!=NULL)
{
while (p!=person)
{
/* 后面还有人, 移动到下一个 */
pre = p;
p = p->next;
}
pre->next = p->next;
}
}
}
删除操作实现的原理是,找到要删除的元素的上一个跟下一个,并把需要删除的元素的上一个的元素的下一个,指向被删除元素的下一个即可。这样元素删除成功
6.链表排序
void SortList(struct list *pList)
{
struct person *pre;
struct person *next;
char *temp_name;
int temp_age;
pre = pList->next;
if(pre == NULL)
{
return;
}
while(pre)
{
next = pre->next;
while(next)
{
if(pre->age > next->age)
{
temp_age = next->age;
next->age = pre->age;
pre->age = temp_age;
temp_name = next->name;
next->name = pre->name;
pre->name = temp_name;
}
next = next->next;
}
pre = pre->next;
}
}
7.附件代码
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct person
{
char *name;
int *age;
struct person *next;
};
struct list{
char *name;
struct person *next;
};
void InitList(struct list *pList, char *name)
{
pList->name = name;
pList->next = NULL;
printf("InitList OK\r\n");
}
void AddItemToList(struct list *pList, struct person *new_person)
{
struct person *last;
//如果是空链表
if(pList->next == NULL)
{
pList->next = new_person;
new_person->next = NULL;
}
else if(pList->next != NULL)
{
last = pList->next;
while (last->next)
{
last = last ->next;
}
last->next = new_person;
new_person->next = NULL;
}
}
void printList(struct list *plist)
{
int i = 0;
struct person *p = plist->next;
while (p!=NULL)
{
printf("person %d: %s is %d\r\n",i++,p->name,p->age);
p = p->next;
}
}
void DelItemFromList(struct list *plist, struct person *person)
{
struct person *p = plist->next;
struct person *pre = NULL;
//如果是头节点
if(p!=NULL&&p==person)
{
plist->next = p->next;
}
else if(p!=NULL&&p!=person)//要删除的不是头结点
{
if(person!=NULL)
{
while (p!=person)
{
/* 后面还有人, 移动到下一个 */
pre = p;
p = p->next;
}
pre->next = p->next;
}
}
}
void SortList(struct list *pList)
{
struct person *pre;
struct person *next;
char *temp_name;
int temp_age;
pre = pList->next;
if(pre == NULL)
{
return;
}
while(pre)
{
next = pre->next;
while(next)
{
if(pre->age > next->age)
{
temp_age = next->age;
next->age = pre->age;
pre->age = temp_age;
temp_name = next->name;
next->name = pre->name;
pre->name = temp_name;
}
next = next->next;
}
pre = pre->next;
}
}
int main(void)
{
printf("hello world\r\n");
struct list a_list;
int i;
struct person p[] = {
{"p1", 10, NULL},
{"p2", 20, NULL},
{"p3", 13, NULL},
{"p4", 41, NULL},
{"p5", 56, NULL},
{"p6", 12, NULL},
{"p7", 9, NULL},
{"p8", 21, NULL},
{NULL, 0, NULL},
};
InitList(&a_list, "A_class");
i = 0;
while (p[i].name != NULL)
{
AddItemToList(&a_list, &p[i]);
i++;
}
printf("add all person:\r\n");
printList(&a_list);
DelItemFromList(&a_list, &p[0]);
printf("del person %s:\r\n", p[0].name);
printList(&a_list);
DelItemFromList(&a_list, &p[3]);
printf("del person %s:\r\n", p[3].name);
printList(&a_list);
SortList(&a_list);
printf("sort list, all person:\r\n");
printList(&a_list);
return 0;
}