1.什么是静态链表?
所有节点都是在程序中建立的,不是临时开辟的,也不能用完后释放,这种链表称之为静态链表。
例下:
#include <stdio.h>
struct Student
{
int num;
float score;
struct Student *next;
};
void main()
{
struct Student a,b,c,*head,*p;
a.num = 10001; a.score = 98;
b.num = 10002; b.score = 94;
c.num = 10003; c.score = 99;
head = &a;
a.next = &b;
b.next = &c;
c.next = NULL;
p = head;
while(p!=NULL)
{
printf("%5d %5.2f\n",p->num,p->score);
p=p->next;
}
}
2.什么是动态链表?
指在程序执行过程中,从无到有地建立起一个链表,即一个一个地开辟节点和输入各个节点的数据,并建立起前后相链的关系。
(1). 建立链表
带头链表
(建立带头链表时,也需要给头节点分配内存单元,否则会出现错误)
struct Student *creat() //创建带头链表
{
struct Student *head ,*p,*q;
head = q =(struct Student *)malloc(sizeof(struct Student));
q->next =NULL;
p =(struct Student *)malloc(sizeof(struct Student));
printf("请输入学号,分数(用空格隔开,0结束):\n");
scanf("%d %f",&p->num,&p->score);
while( 0 != p->num)
{
q->next = p;
q = p;
p = (struct Student *)malloc(sizeof(struct Student));
scanf("%d %f",&p->num,&p->score);
}
q = NULL;
return head;
}
不带头链表
struct Student *creat1() //创建不带头
{
struct Student *head=NULL ,*p,*q;
int n=0;
q = p =(struct Student *)malloc(sizeof(struct Student));
p =(struct Student *)malloc(sizeof(struct Student));
printf("请输入学号,分数(用空格隔开,0结束):\n");
scanf("%d %f",&p->num,&p->score);
while( 0 != p->num)
{
n++;
if(n==1)
head = p;
else
q->next = p;
q = p;
p = (struct Student *)malloc(sizeof(struct Student));
scanf("%d %f",&p->num,&p->score);
}
q->next = NULL;
return head;
}
2. 输出链表
带头链表
void print(struct Student *head)//输出带头链表所有节点
{
struct Student *p;
if(NULL != head)
{
p = head ->next;
while(NULL != p)
{
printf("学号 :%5d 分数 :%5.2f\n",p->num,p->score);
p = p->next;
}
}
}
不带头结点
void print1(struct Student *head)//输出不带头链表所有节点
{
struct Student *p;
if(NULL != head)
{
p = head;
while(NULL != p)
{
printf("学号 :%5d 分数 :%5.2f\n",p->num,p->score);
p = p->next;
}
}
}
3.删除节点
(1)带头链表
根据学生学号,删除带头节点的函数,删除后返回头指针
struct Student * delete(struct Student *head,int studentnum)//删除函数,带头链表,根据学号删除
{
struct Student *p ,*q;
p = q = head;
while(NULL != p->next)
{
if(studentnum == p->next->num)
{
p = q->next;
q->next = p->next;
free(p);
}
p=p->next;
q=q->next;
}
return head;
}
(2)不带头链表
不带头结点的删除,需要多考虑,要删除的结点是否为第一个结点。
struct Student * delete1(struct Student *head,int studentnum)//不带头链表,根据学号删除删除
{
struct Student *p ,*q;
p =q = head;
if(studentnum == head->num)
{
head = head->next;
free(q);
}
else
{
while(NULL != p->next)
{
if(studentnum == p->next->num)
{
p = q->next;
q->next = p->next;
free(p);
}
p=p->next;
q=q->next;
}
}
return head;
}
3.增加结点
带头链表增加节点
struct Student * add(struct Student * head,int studentnum,int studentscore)
{
struct Student *temp, *q = head ;
temp =(struct Student *)malloc(sizeof(struct Student));
temp->num = studentnum;
temp->score = studentscore;
while(NULL != q->next)
{
q=q->next;
}
q->next= temp;
q= temp;
q->next = NULL;
return head;
}
4.更改结点
struct Student *updata(struct Student *head,int studentnum,int studentscore)
{
struct Student *p = head->next;
while(NULL != p)
{
if(studentnum == p->num)
{
p->score = studentscore;
}
p = p->next;
}
return head;
}
所有源码:
#include <stdio.h>
#include <malloc.h>
struct Student *creat(); //创建带头链表
struct Student *creat1(); //创建不带头链表
void print(struct Student *head);//输出带头链表所有节点
void print1(struct Student *head);//输出不带头链表所有节点
struct Student * delete(struct Student *head,int studentnum);//带头链表,根据学号删除
struct Student * delete1(struct Student *head,int studentnum);//不带头链表,根据学号删除
struct Student * add(struct Student * head,int studentnum,int studentscore);
struct Student *updata(struct Student *head,int studentnum,int studentscore);
struct Student
{
int num;
float score;
struct Student *next;
};
void main()
{
struct Student *stu;
stu =creat();
//stu = creat1();
printf("***************************************\n");
printf("学生的数据为:\n");
print(stu);
//print1(stu);
printf("***************************************\n");
printf("删除后学生的数据为:\n");
printf("***************************************\n");
stu = delete(stu,10001);
//stu = delete1(stu,10001);
print(stu);
//print1(stu);
printf("***************************************\n");
printf("增加后学生的数据为:\n");
printf("***************************************\n");
stu = add(stu,20000,100);
print(stu);
printf("***************************************\n");
printf("修改后学生的数据为:\n");
printf("***************************************\n");
stu = updata(stu,20000,50);
print(stu);
printf("***************************************\n");
}
struct Student *creat() //创建带头链表
{
struct Student *head ,*p,*q;
head = q =(struct Student *)malloc(sizeof(struct Student));
q ->next =NULL;
p =(struct Student *)malloc(sizeof(struct Student));
printf("请输入学号,分数(用空格隔开,0结束):\n");
scanf("%d %f",&p->num,&p->score);
while( 0 != p->num)
{
q->next = p;
q = p;
p = (struct Student *)malloc(sizeof(struct Student));
scanf("%d %f",&p->num,&p->score);
}
q->next= NULL;
return head;
}
struct Student *creat1() //创建不带头
{
struct Student *head=NULL ,*p,*q;
int n=0;
q = p =(struct Student *)malloc(sizeof(struct Student));
p =(struct Student *)malloc(sizeof(struct Student));
printf("请输入学号,分数(用空格隔开,0结束):\n");
scanf("%d %f",&p->num,&p->score);
while( 0 != p->num)
{
n++;
if(n==1)
head = p;
else
q->next = p;
q = p;
p = (struct Student *)malloc(sizeof(struct Student));
scanf("%d %f",&p->num,&p->score);
}
q->next = NULL;
return head;
}
void print(struct Student *head)//输出带头链表所有节点
{
struct Student *p;
if(NULL != head)
{
p = head ->next;
while(NULL != p)
{
printf("学号 :%5d 分数 :%5.2f\n",p->num,p->score);
p = p->next;
}
}
}
void print1(struct Student *head)//输出不带头链表所有节点
{
struct Student *p;
if(NULL != head)
{
p = head;
while(NULL != p)
{
printf("学号 :%5d 分数 :%5.2f\n",p->num,p->score);
p = p->next;
}
}
}
struct Student * delete(struct Student *head,int studentnum)//带头链表,根据学号删除删除
{
struct Student *p ,*q;
p = q = head;
while(NULL != p->next)
{
if(studentnum == p->next->num)
{
p = q->next;
q->next = p->next;
free(p);
}
p=p->next;
q=q->next;
}
return head;
}
struct Student * delete1(struct Student *head,int studentnum)//不带头链表,根据学号删除删除
{
struct Student *p ,*q;
q = head;
if(studentnum == head->num)
{
head = head->next;
free(q);
}
else
{
p =head;
while(NULL != p->next)
{
if(studentnum == p->next->num)
{
p = q->next;
q->next = p->next;
free(p);
}
p=p->next;
q=q->next;
}
}
return head;
}
struct Student * add(struct Student * head,int studentnum,int studentscore)
{
struct Student *temp, *q = head ;
temp =(struct Student *)malloc(sizeof(struct Student));
temp->num = studentnum;
temp->score = studentscore;
while(NULL != q->next)
{
q=q->next;
}
q->next= temp;
q= temp;
q->next = NULL;
return head;
}
struct Student *updata(struct Student *head,int studentnum,int studentscore)
{
struct Student *p = head->next;
while(NULL != p)
{
if(studentnum == p->num)
{
p->score = studentscore;
}
p = p->next;
}
return head;
}
运行结果:
请输入学号,分数(用空格隔开,0结束):
10000 100
10001 99
10002 98
10003 97
10004 96
10005 95
0 0
学生的数据为:
学号 :10000 分数 :100.00
学号 :10001 分数 :99.00
学号 :10002 分数 :98.00
学号 :10003 分数 :97.00
学号 :10004 分数 :96.00
学号 :10005 分数 :95.00
删除后学生的数据为:
学号 :10000 分数 :100.00
学号 :10002 分数 :98.00
学号 :10003 分数 :97.00
学号 :10004 分数 :96.00
学号 :10005 分数 :95.00
增加后学生的数据为:
学号 :10000 分数 :100.00
学号 :10002 分数 :98.00
学号 :10003 分数 :97.00
学号 :10004 分数 :96.00
学号 :10005 分数 :95.00
学号 :20000 分数 :100.00
修改后学生的数据为:
学号 :10000 分数 :100.00
学号 :10002 分数 :98.00
学号 :10003 分数 :97.00
学号 :10004 分数 :96.00
学号 :10005 分数 :95.00
学号 :20000 分数 :50.00