单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
(注意代码中的注释很重要!!!)以下代码均默认用户正确输入。
#include
#include
#define LEN sizeof(struct Student)
struct Student{
long num;
float score;
struct Student* next;
}; //分号!! 特别容易忘!!
int n;//n为节点数
//建立链表函数,creat函数带回一个起始地址
struct Student* creat(void){
struct Student* head;
struct Student* p1,* p2;
n=0;
//开辟新单元
// 编译系统会实现隐式类型转换,所以也可以写成p1=malloc(LEN);
p1=p2=(struct Student*)malloc(LEN);
//输入第一个学生的学号和成绩
scanf("%ld,%f",&p1->num,&p1->score);
head=NULL;
while (p1->num){
n+=1;
if(n==1)head=p1;
else p2->next=p1;
p2=p1;
p1=(struct Student*)malloc(LEN);
//输入其他学生的学号和成绩
scanf("%ld,%f",&p1->num,&p1->score);
}
p2->next=NULL;
return(head);
}
// 输出链表的函数
void print(struct Student* head){
struct Student* p;
printf("Now,There %d record are :\n",n);
p=head;
if(head!=NULL){
do{
printf("%ld %.1f\n",p->num,p->score);
p=p->next;
}while(p!=NULL);
}
}
int main(){
struct Student* head;
head=creat();//调用creat函数,返回第一个结点的起始地址
print(head);//调用print函数
return 0;
}
输入:
1001,55.6
1002,68.59
1003,88.6
0
输出:
Now,There 3 record are :
1001 55.6
1002 68.6
1003 88.6
头插法:(在上面创建的基础上,只贴了部分代码)
在头部插入结点,将头指针指向新结点。在将新结点指向原来的第一个结点。
#include
#include
#define LEN sizeof(struct Student)
int n
//需要插入的学生
void getInput(struct Student *stu){
printf("请输入学生及分数");
scanf("%ld,%f",&stu->num,&stu->score);
}
//执行插入操作
void addStu(struct Student **head){
struct Student* stu,*temp;
stu=(struct Student*)malloc(sizeof(struct Student));
if(stu==NULL){
printf("内存分配失败");
exit(1);
}
getInput(stu);
if (*head!=NULL)
{
temp=*head;
*head=stu;
stu->next=temp;
}else{
//如果链表里面没有数据
*head =stu;
stu->next=NULL;
}
n++;
}
int main(){
struct Student* head=NULL;
head=creat();//调用creat函数,返回第一个结点的起始地址
print(head);//调用print函数
addStu(&head);
print(head);
release(&head);
return 0;
}
//需要插入的学生
void getInput(struct Student *stu){
printf("请输入学生及分数:");
scanf("%ld,%f",&stu->num,&stu->score);
}
//执行插入操作
void addStu(struct Student **head,int n){
//需要插入位置的前一个结点
struct Student *previous;
//需要插入的结点
struct Student *current;
//需要插入位置的后一个结点
struct Student *new;
current=*head;
previous=NULL;
int count=1;
while (current!=NULL&&count<=num)
{
count++;
previous=current;
current=current->next;
}
new=(struct Student*)malloc(sizeof(struct Student));
if(new==NULL){
printf("内存分配失败");
exit(1);
}
getInput(new);
//如果链表里面没有数据
if(previous==NULL){
*head=new;
}else{
previous->next=new;
}
n++;
new->next=current;
}
//在尾部插入指针
void addStu(struct Student **head){
struct Student* stu,*temp;
stu=(struct Student*)malloc(sizeof(struct Student));
if(stu==NULL){
printf("内存分配失败");
exit(1);
}
getInput(stu);
if (*head!=NULL)
{
temp=*head;
// 定位单链表的尾部位置
while (temp->next !=NULL)
{
temp=temp->next;
}
// 插入数据
temp->next=stu;
stu->next=NULL;
}else{
*head =stu;
stu->next=NULL;
}
n++;
}
// 删除结点
void delStu(struct Student **head,int num){
//需要删除位置的前一个结点
struct Student *previous;
//需要删除位置的后一个结点
struct Student *current;
current=*head;
previous=NULL;
int count=1;
while (current!=NULL&&count!=num)
{
count++;
previous=current;
current=current->next;
}
if(current==NULL){
printf("找不到匹配的结点");
return;
}else{
//需要删除的结点在第一个位置时
if(previous==NULL){
*head=current->next;
}else{
previous->next=current->next;
n--;
}
free(current);
}
}