本文最后更新于2022年02月24日,已超过3天没有更新。如果文章内容或图片资源失效,请留言反馈,我会及时处理,谢谢!
void *malloc(unsigned int size)
void *calloc(unsigned n,unsigned size);
void free(void *p)
struct Student {
// 结点的数据域
int no;
char name[20];
float score;
// 结点的指针域 类型是自身结构体类型
struct Student *next;
};
在C语言中,静态链表的表现形式为结构体数组,是在程序中定义,不是临时开辟的,也不能用完后释放, 每个数组元素包含数据域(data)和指针域(next)。
#include <stdio.h>
struct Student {
// 结点的数据域
int no;
char name[20];
float score;
// 结点的指针域 类型是自身结构体类型
struct Student *next;
};
void main() {
struct Student head, *p;
struct Student stu1 = {10001,"Zhang Yin", 100};
struct Student stu2 = {10002,"Qian Feng", 90};
struct Student stu3 = {10003,"Liu Liang", 91};
head.next = &stu1;
// 将stu2的起始地址赋给stu1的next成员
stu1.next = &stu2;
stu2.next = &stu3;
stu3.next = NULL;
for (p = head.next; p!= NULL; p = p->next) {
printf("%d\t%s\t%.2f\n",p->no,p->name,p->score);
}
}
建立一个带有头结点的学生链表,直到输入的学生学号小于等于0时结束。
#include <stdio.h>
#include <string.h>
struct Student {
int no;
char name[20];
unsigned sex;
struct Student *next;
};
void main() {
void setdata(struct Student *temp);
struct Student *creatlink();
void printlink(struct Student *head);
struct Student *head;
head = creatlink();
printlink(head);
}
void setdata(struct Student *temp) {
printf("No:");
scanf_s("%d", &temp->no);
getchar();
printf("Name:");
gets(temp->name);
printf("Sex(1:boy;0:girl):");
scanf_s("%d", &temp->sex);
}
struct Student *creatlink() {
int i = 0;
struct Student *head, *p, *q;
head = (struct Student *)malloc(sizeof(struct Student));
head -> next = NULL;
p = head;
q = (struct Student *)malloc(sizeof(struct Student));
printf("请输入学生信息:\n");
setdata(q);
while ((q->no) > 0) {
p->next = q;
p = q;
q = (struct Student *)malloc(sizeof(struct Student));
printf("请输入学生信息:\n");
setdata(q);
}
p->next = NULL;
return head;
}
void printlink(struct Student *head) {
struct Student *p;
printf("-学生信息-\n");
for (p = head->next; p != NULL; p = p->next) {
printf("%d\t%s\t%d\n", p->no, p->name, p->sex);
}
}
对链表的删除操作
struct Student *del(struct Student *head, int no) {
struct Student *p1, *p2;
if (head == NULL) {
printf("\n 链表为空! \n");
return head;
}
p1 = head;
p2 = NULL;
while (no != p1->no && p1->next != NULL) {
p2 = p1;
p1 = p1->next;
}
if (no == p1->no) {
if (p1 == head) {
head = p1->next;
}
else {
p2->next = p1->next;
}
printf("已删除:%d\n", no);
}
else{
printf("\n没有找到要删除的对象:%d\n", no);
}
return(head);
}
对链表的插入操作
struct Student *insert(struct Student *head, struct Student *stud) {
struct Student *p0, *p1, *p2;
p1 = head; // 使p1指向第一个结点
p0 = stud; // p0指向要插入的结点
p2 = NULL;
if (head == NULL) { // 原来的链表是空表
head = p0;
p0 -> next = NULL; // 使p0指向的结点
}
else {
while ((p0->no > p1->no) && (p1->next != NULL)) {
p2 = p1; // 使p2指向刚才p1指向的结点
p1 = p1->next; // p1后移一个结点
}
if (p0->no <= p1->no) {
if (head == p1) {
head = p0; //插到原来第一个结点之前
}
else {
p2->next = p0; // 插到p2指向的结点之后
}
p0->next = p1;
}
else {
p1->next = p0; // 插到最后一个结点之后
p0->next = NULL;
}
return(head);
}
}
#include <stdio.h>
#include <string.h>
struct Student {
int no;
char name[20];
unsigned sex;
struct Student *next;
};
void main() {
void setdata(struct Student *temp);
struct Student *creatlink();
void printlink(struct Student *head);
struct Student *del(struct Student *head, int no);
struct Student *insert(struct Student *head, struct Student *stud);
struct Student *head, *stu;
int num;
head = creatlink();
printlink(head);
printf("请输入要删除的学号:");
scanf_s("%d", &num);
while (num != 0) { // 为0退出删除
head = del(head, num);
printlink(head);
printf("请输入要删除的学号:");
scanf_s("%d", &num);
}
printf("请输入要插入的信息:\n");
stu = (struct Student *)malloc(sizeof(struct Student));
setdata(stu);
while (stu->no != 0) {
head = insert(head, stu);
printlink(head);
printf("请输入要插入的信息:\n");
stu = (struct Student *)malloc(sizeof(struct Student));
setdata(stu);
}
}
void setdata(struct Student *temp) {
printf("No:");
scanf_s("%d", &temp->no);
getchar();
printf("Name:");
gets(temp->name);
printf("Sex(1:boy;0:girl):");
scanf_s("%d", &temp->sex);
}
struct Student *creatlink() {
int i = 0;
struct Student *head, *p, *q;
head = (struct Student *)malloc(sizeof(struct Student));
head -> next = NULL;
p = head;
q = (struct Student *)malloc(sizeof(struct Student));
printf("请输入学生信息:\n");
setdata(q);
while ((q->no) > 0) {
p->next = q;
p = q;
q = (struct Student *)malloc(sizeof(struct Student));
printf("请输入学生信息:\n");
setdata(q);
}
p->next = NULL;
return head;
}
void printlink(struct Student *head) {
struct Student *p;
printf("-学生信息-\n");
for (p = head->next; p != NULL; p = p->next) {
printf("%d\t%s\t%d\n", p->no, p->name, p->sex);
}
}
struct Student *del(struct Student *head, int no) {
struct Student *p1, *p2;
if (head == NULL) {
printf("\n 链表为空! \n");
return head;
}
p1 = head;
p2 = NULL;
while (no != p1->no && p1->next != NULL) {
p2 = p1;
p1 = p1->next;
}
if (no == p1->no) {
if (p1 == head) {
head = p1->next;
}
else {
p2->next = p1->next;
}
printf("已删除:%d\n", no);
}
else{
printf("\n没有找到要删除的对象:%d\n", no);
}
return(head);
}
struct Student *insert(struct Student *head, struct Student *stud) {
struct Student *p0, *p1, *p2;
p1 = head; // 使p1指向第一个结点
p0 = stud; // p0指向要插入的结点
p2 = NULL;
if (head == NULL) { // 原来的链表是空表
head = p0;
p0 -> next = NULL; // 使p0指向的结点
}
else {
while ((p0->no > p1->no) && (p1->next != NULL)) {
p2 = p1; // 使p2指向刚才p1指向的结点
p1 = p1->next; // p1后移一个结点
}
if (p0->no <= p1->no) {
if (head == p1) {
head = p0; //插到原来第一个结点之前
}
else {
p2->next = p0; // 插到p2指向的结点之后
}
p0->next = p1;
}
else {
p1->next = p0; // 插到最后一个结点之后
p0->next = NULL;
}
return(head);
}
}