单链表(创建、插入、删除)

单链表

单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。


(注意代码中的注释很重要!!!)以下代码均默认用户正确输入。

单链表的创建和输出

在这里插入图片描述 在这里插入图片描述 在这里插入图片描述

#include<stdio.h>
#include<stdlib.h>
#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<stdio.h>
#include<stdlib.h>
#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);
    }
}

  • 8
    点赞
  • 54
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

又又爱拍照

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值