c语言在静态链表中的节点前后插入以及删除节点的用法

静态链表
使用到了:
静态链表的遍历
静态链表的遍历统计
静态链表的节点前插
静态链表的节点后插
静态链表的节点删除
静态链表的节点中的数据修改

#include <stdio.h>
//#include<stdiob.h>
struct student{
    
    
    int yu;
    int shu;
    int ying;
    struct student *next;
};

void printlink(struct student *head){
    
      //先while遍历时printf出来
    struct student *p;
    p = head;
    while(p != NULL){
    
    
        printf("%d %d %d\n",p->yu,p->shu,p->ying);
        p = p->next;
    }
    putchar('\n');
}

int insertfrombehind(struct student *head, int data,struct student *new){
    
    
    struct student *p = head;
    while(p != NULL){
    
    
        if(p->yu == data){
    
    
            new->next = p->next;
            p->next = new;
            return 1;
        }        
        p = p->next;
    }    
    return 0;
}

//需要返回一个结构体 所以要定义
struct student* insertfromfor(struct student *head, int data,struct student *new){
    
    
    struct student *p = head;
   if(p->yu == data){
    
       //在第一个进行插入
        new->next = head;
        return new;
   }
    while(p != NULL){
    
    
        if(p->next->yu == data){
    
     //跨一个节点判断
            new->next = p->next;
            p->next = new;
            return head; //检测到就插入不需要再遍历直接返回
        }
        p = p->next;
    }    
    return head; //检测不到 就直接返回
}

int getlinknodenum(struct student *head){
    
      //while遍历的时候用int一个数进行++运算就可以得出
    int cnt = 0;
    while(head != NULL){
    
    
        cnt ++;
        head = head->next;
    }
    return cnt;
}
 
struct student* deletenode(struct student *head, int data){
    
    
    struct student *p = head;
   if(p->yu == data){
    
       //在第一个进行插入
        head = head->next;
        return head;
   }
    while(p != NULL){
    
    
        if(p->next->yu == data){
    
     //跨一个节点判断
            //struct student *tmp = p;
            p->next = p->next->next;
            //free(tmp)
            return head; //检测到就插入不需要再遍历直接返回
        }
        p = p->next;
    }    
    return head; //检测不到 就直接返回
}

void changedata(struct student *head, int data,int newdata){
    
    
    struct student *p;
    p = head;
    while(p != NULL){
    
    
        if(p->yu == data){
    
    
            p->yu=newdata;
            return head;
        }
        p = p->next;
    }
}

int main()
{
    
    
    struct student *head = NULL;
    struct student t1 = {
    
    11,22,33,NULL};
    struct student t2 = {
    
    12,23,34,NULL};
    struct student t3 = {
    
    13,24,35,NULL};
    struct student t4 = {
    
    14,25,36,NULL};    
    t1.next = &t2;
    t2.next = &t3;
    t3.next = &t4;
    
    head = &t1;
  
    struct student new = {
    
    66,25,36,NULL};
    insertfrombehind(&t1,12,&new);
    int ret = getlinknodenum(&t1);
    printf("total=%d\n",ret);    
    printlink(&t1);
    
    struct student new2 = {
    
    100,25,36,NULL};
    insertfromfor(&t1,13,&new2);
    ret = getlinknodenum(&t1);
    printf("total=%d\n",ret);    
    printlink(&t1);
    
    deletenode(&t1,13);
    ret = getlinknodenum(&t1);
    printf("total=%d\n",ret);
    printlink(&t1);  
      
    changedata(&t1,14,110);
    printlink(&t1);

    //system("pause");
    return 0;
}

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/weixin_41679960/article/details/114764531