链表是一种常见的数据结构,链表有静态链表和动态链表之分,且通常和结构体、指针一起使用。链表由头指针和结点组成,头指针指向结构体的变量的地址( 通常是第一个变量)。链表里有一系列结点,每个结点包括两部分:一个是存储数据元素的 数据域,一个是存储下一个结点地址的 指针域。
静态链表:
动态链表的创建、删除、插入结点:
#include<stdio.h>
#include<malloc.h>
#define LENGTH sizeof(struct student)
struct student* create(); // 创建链表函数
struct student print(struct student* head); // 打印链表函数
struct student* del(struct student* head, int num); // 删除结点函数
struct student* insert_1(struct student* head, int num); // 插入结点函数
struct student* insert_2(struct student* head, struct student* enter);
int n; // 全局变量,用来记录存放的结点数
struct student {
int num;
float score;
struct student* next; // 链条
};
void main() {
struct student* stu, * p,* enter;
int num;
stu = create(); // 创建链表
print(stu);
printf("\n\n");
p = NULL;
printf("input the num to delete:\n");
scanf("%d", &num); printf("\n");
p = del(stu, num); // 删除链表结点
print(p);
printf("\n\n");
/*printf("input the num to insert:\n");
scanf("%d", &num);
p = insert(stu,num);
print(p);*/
enter = (struct student*)malloc(LENGTH);
printf("input the num to insert:\n");
scanf("%d", &enter->num);
printf("input the score to insert:\n");
scanf("%f", &enter->score);
p = insert_2(stu, enter);
print(p);
printf("\n\n");
}
struct student* create() {
struct student* head; // 头结点
struct student* p1, * p2; // p1放新结点,p2放老结点,接收新结点数据的是p1
p1 = p2 = (struct student*)malloc(LENGTH);
// malloc()的返回值默认是void*型,要转换成struct student*型
printf("input your num:\n");
scanf("%d", &p1->num);
printf("input your score:\n");
scanf("%f", &p1->score);
printf("\n");
head = NULL; // 头结点指向空指针
n = 0;
while (p1->num != -1) { // num 不等于 -1 时为有效数字
n++;
if (n == 1) head = p1;// 如果是第一个数字,新结点 p1赋给头结点head
else p2->next = p1; // 把新结点 p1放进下一个结点 p2的链条里
p2 = p1; // 此时 p2指向 p1,p1为老结点 (p2 永远是老结点)
p1 = (struct student*)malloc(LENGTH); // 此时 p1为新结点,指向新的地址
printf("input your num:\n");
scanf("%d", &p1->num);
printf("input your score:\n");
scanf("%f", &p1->score);
printf("\n");
}
p2->next = NULL;
return head; // 返回头结点
}
struct student print(struct student* head) {
struct student* p;
p = head;
printf("there are %d student:\n", n);
do {
printf("num is %d score is %6.2f\n", p->num, p->score);
p = p->next;
} while (p != NULL);
}
struct student* del(struct student* head, int num) {
struct student* p1, * p2; // p1 指向新结点,p2指向老结点
if (head == NULL) {
printf("This is NULL\n");
return NULL;
}
p1 = head;
while (p1->num != num && p1->next != NULL) {
p2 = p1;
p1 = p1->next;
}
if (p1->num == num) {
if (p1 == head) {
head = p1->next;
}
else {
p2->next = p1->next;
}
printf("delete NO.%d success!\n", num);
n = n - 1;
}
else {
printf("%d not been found!\n", num);
}
return head;
}
struct student* insert_1(struct student* head, int num) {
struct student* new, * old;
struct student* enter;
enter = (struct student*)malloc(LENGTH);
enter->num = num;
printf("input the score:\n");
scanf("%f", &enter->score);
new = head;
if (head == NULL) { // 空链表
head = enter;
enter->next = NULL;
}
if (enter->num <= new->num) { // enter比第一个小
head = enter;
head->next = new;
return head;
}
while ((enter->num > new->num) && (new->next != NULL)) {
old = new;
new = new->next;
}
if (new->next != NULL) {
old->next = enter;
enter->next = new;
}
if ((new->next == NULL) && (enter->num > new->num)){
new->next = enter;
enter->next = NULL;
}
if ((new->next == NULL) && (enter->num <= new->num)) {
old->next = enter;
enter->next = new;
}
n = n + 1;
return head;
}
struct student* insert_2(struct student* head, struct student* enter){
struct student* new, * old;
new = head;
if (head == NULL) { // 空链表
head = enter;
head->next = new;
}
while ((enter->num > new->num) && (new->next != NULL)) { // 遍历
old = new;
new = new->next;
}
if (enter->num <= new->num) { // 找到插入点
if (new == head) { // 如果插入值小于头结点的
head = enter;
enter->next = new;
}
else {
old->next = enter;
enter->next = new;
}
}
else{ // 没有找到插入点
new->next = enter;
enter->next = NULL;
}
n = n + 1;
return head;
}
输出为:
动态链表的创建也可以用下面这种形式实现:
#include<stdio.h>
#include<malloc.h>
struct list {
int data;
struct list *addr; // 定义内部指针成员变量
};
void creatlist(struct list* head);
void print(struct list* abc);
void main() {
struct list* head, * abc;
head = (struct list*)malloc(sizeof(struct list));
head->addr = NULL;
creatlist( head);
print(head);
}
void creatlist(struct list *head) {
struct list* old, * new;
old = head;
int data1;
scanf("%d", &data1);
while (data1 != -1) {
new = (struct list*)malloc(sizeof(struct list));//D4
new->data = data1;
new->addr = old;
old->addr = new;
old = new; // D3
scanf("%d", &data1);
}
old->addr = NULL;
}
void print(struct list* abc) {
struct list* p;
p = abc->addr;
printf("\n");
printf(" ");
while (p != NULL) {
printf("%d\n", p->data);
printf(" ");
p = p->addr;
}
}
/*
void main() {
void* p;
int a[] = { 1,2,3 };
p = a; // 指针 p 指向 a,则指针 p 的地址和 a 的地址相等
printf("%d\n%d\n", p, a);
}
*/