单链表Link的基本操作

学习笔记 单链表的基本操作

经过这次上手打出单链表后 感觉完全掌握单链表, 增删查改样样俱全 无头插法 太过简单

#include<stdlib.h>
#include<stdio.h>

typedef int ElementType;

int MaxSize;

typedef struct Node{
    int data;
    struct Node* next;
}Node,*List,*Position;
//初始化一个链表
void initializer_list(List L){
    L = (List)malloc(sizeof(Node));//创建头结点
    if(!L)
         exit(0);
     L->next = NULL;
}
//尾插法
List TailInsert(List L){
    int x;
    Node *P, *r;
    L = (Node *)malloc(sizeof(Node));
    L->next == NULL;
    r = L;
    printf("请输入你的链表长度:");
    scanf("%d",&MaxSize);
    for(int i = 1;i <= MaxSize;i++){
    P = (Node *)malloc(sizeof(Node));
    if(P == NULL){
        printf("Out of space!");
        exit(0);
    }else{
        printf("请输入你的第%d个结点的数据:",i);
        scanf("%d",&x);
        P->data = x;
        r->next = P;
        r = P;
        }
    }
    r->next = NULL;
    printList(L);
    return L;
}
//插入一个数据
List Insert(List L,int Pos,ElementType X){
    Node* P = L;
    Position TempCell = malloc(sizeof(struct Node));
    if(TempCell == NULL){
        printf("Out of space!");
        exit(0);
    }else{
        for(int i = 1;i < Pos;i++){
            P = P->next;
        }
        TempCell->data = X;
        TempCell->next = P->next;
        P->next = TempCell;
        MaxSize++;
    }
    printList(L);

    return L;
}
//输出单链表
void printList(List L){
     List q;
     q = L->next;
     while(q){
         printf("%d ",q->data);
         q = q->next;
     }
     printf("\n");
 }
//返回该链表的长度
int Lenth(List L){
    int Lenth = 0;
    Node *P = L->next;
    while(P){
        P = P->next;
        Lenth++;
    }
    return Lenth;
}
//返回该位置的前一个结点
List PreNode(List L,int Pos){
    Node *r = L;
    for(int i = 1; i < Pos; i++){
        r = r->next;
    }
    return r;
}
//删除一个位置的结点
List Delete_Position(List L){
    int Pos;
    printf("请输入你要删除的结点的位置:");
    scanf("%d",&Pos);
    if(Pos > MaxSize){
        printf("you little bitch,you'r out of MaxSize.\n");
    }else
    {
    List P;
    List r = L->next;
    for(int i = 1; i < Pos; i++){
        r = r->next;
    }
    P = PreNode(L, Pos);
    P->next = r->next;
    free(r);
    }
    printList(L);
    return L;
}
//删除一个数据
List Delete_data(List L){
    int X,Pos = 0;
    Position P;
    Node *r = L;
    printf("请输入你要删除的数据:");
    scanf("%d",&X);
    while(Pos <= MaxSize){
        r = r->next;
        Pos++;
        if(r->data == X){
            P = PreNode(L,Pos);
            P->next = r->next;
            free(r);
        }
    }
    printList(L);
    return L;
}

int main(){
    int lenth;
    List L;
    L = TailInsert(L);
    lenth = Lenth(L);
    printf("你的链表长度为:");
    printf("%d\n",lenth);
    L = Insert(L,3,3);
    L = Delete_data(L);
    L = Delete_Position(L);

    return 0;
}

在这里插入图片描述

  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值