链表
实例:学生管理 学号 成绩 若学号为0则程序停止 建立一个链表
# include <stdio.h>
# include <malloc.h>
struct stu_node* creat(void); /*函数声明*/
void list(struct stu_node* head);
struct stu_node /*建立一个结构体*/
{
int num;
float score;
struct stu_node* next; /*next是struct stu_node*类型的 是指针变量*/
};
#define LEN sizeof(struct stu_node) /*宏定义一个结构体的长度用于下面的动态链表*/
int main()
{
struct stu_node* head;
head = creat();
list(head);
return 0;
}
struct stu_node* creat(void) /*struct stu_node*代表函数的返回值是这个类型的,(void)代表没有形参*/
{
struct stu_node* p1 = NULL; /*这三个都是存放地址的指针变量*/
struct stu_node* p2 = NULL;
struct stu_node* head = NULL;
int n;
float s;
scanf_s("%d%f", &n, &s);
while (n != 0) /*实例,学号不等于0*/
{
p1 = (struct stu_node*)malloc(LEN); /*既建立了一个节点,又把这个节点的地址赋给p1。多次循环即建立出一个链表*/
p1->num = n; p1->score = s; /*赋值*/
if (head == NULL) /*第一次会走这个*/
head = p1;
else /*除第一次之外走这个*/
p2->next = p1;
p2 = p1;
scanf_s("%d%f", &n, &s);
}
p2->next = NULL; /*将尾节点的next赋为NULL*/
return head; /*返回头指针,因为头指针代表着这个链表,而且后边的链表的删除会有一种情况必须要返回头指针*/
}
void list(struct stu_node* head) /*输出链表中的所有节点*/
{
struct stu_node* p; /*存储*/
if (head == NULL)
printf("此链表为空!\n");
else
{
printf("链表内容如下:\n");
p = head;
while (p != NULL) /*循环的赋值然后输出*/
{
printf("%d %f\n", p->num, p->score);
p = p->next;
}
}
}
仍然是实例 链表要多写 多重复是好事 多了个删除功能
# include <stdio.h>
# include <malloc.h>
struct stu_node* creat(void);
void list(struct stu_node* head);
struct stu_node* del(struct stu_node* head, int num);
struct stu_node
{
int num;
float score;
struct stu_node* next;
};
#define LEN sizeof(struct stu_node)
int main()
{
int num;
struct stu_node* head;
head=creat();
printf("请输入想要删除的学生信息所对应的学号:");
scanf_s("%d", &num); /*n是要删除的学号对应的节点*/
head=del(head, num);
list(head);
return 0;
}
struct stu_node* creat(void)
{
struct stu_node* p1=NULL;
struct stu_node* p2=NULL;
struct stu_node* head=NULL;
int n; float s;
printf("请输入学生的学号和成绩,中间用空格隔开,若学号为0则停止\n");
scanf_s("%d%f", &n, &s);
while (n != 0)
{
p1 = (struct stu_node*)malloc(LEN);
p1->num = n; p1->score = s;
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
scanf_s("%d%f", &n, &s);
}
p2->next = NULL;
return head;
}
void list(struct stu_node* head)
{
struct stu_node* p;
if (head == NULL)
printf("空指针\n");
else
{
printf("链表的内容如下:\n");
p = head;
while (p != NULL)
{
printf("%d %f\n", p->num, p->score);
p = p->next;
}
}
}
struct stu_node* del(struct stu_node* head, int num)
{
struct stu_node* p1=NULL;
struct stu_node* p2=NULL;
if (head == NULL)
printf("链表为空无法进行删除!\n");
else
{
p1 = head;
while ((num != p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if (p1->num == num)
{
if (p1 == head)
head = p1->next;
else
p2->next = p1->next;
free(p1);
printf("该节点已经删除\n");
}
else
printf("链表中不存在该节点,无法删除!\n");
}
return head;
}
链表的插入 所有都不是复制粘贴,为的是熟练,但插入部分还要继续研究熟悉
# include <stdio.h>
# include <malloc.h>
struct stu_node* insert(struct stu_node* head);
struct stu_node* creat(void);
void list(struct stu_node* head);
struct stu_node
{
int num;
float score;
struct stu_node* next;
};
#define LEN sizeof(struct stu_node)
int main()
{
struct stu_node* head;
head = creat();
insert(head);
list(head);
return 0;
}
struct stu_node* creat(void)
{
struct stu_node* p1=NULL;
struct stu_node* p2=NULL;
struct stu_node* head=NULL;
int n; float s;
scanf_s("%d%f", &n, &s);
while (n != 0)
{
p1 = (struct stu_node*)malloc(LEN);
p1->num = n;
p1->score = s;
if (head == NULL)
head = p1;
else
p2->next = p1;
p2 = p1;
scanf_s("%d%f", &n, &s);
}
p2->next = NULL;
return head;
}
void list(struct stu_node* head)
{
struct stu_node* p;
if (head == NULL)
printf("空链表\n");
else
{
printf("链表的内容如下:\n");
p = head;
while (p != NULL)
{
printf("%d %f\n", p->num, p->score);
p = p->next;
}
}
}
struct stu_node* insert(struct stu_node* head)
{
struct stu_node* p0;
struct stu_node* p1;
struct stu_node* p2;
p0 = (struct stu_node*)malloc(LEN);
p0->next = NULL;
printf("请输入要插入的学号和成绩:\n");
scanf_s("%d%f", &p0->num, &p0->score);
getchar();
if (head == NULL)
head = p0;
else
{
p1 = head;
while ((p0->num > p1->num) && (p1->next != NULL))
{
p2 = p1;
p1 = p1->next;
}
if (p0->num <= p1->num)
{
if (head == p1)
head = p0;
else
p2->next = p0;
p0->next = p0;
}
else
p1->next = p0;
}
return head;
}