数据结构 单链表 C语言实现

单链表

单链表 顺序存储线性表
分配方式 分散存储 集中存储
时间性能 查找O(n) 插入删除O(1) 查找O(1) 插入删除O(n)
空间性能 无需分配空间,元素可以无限扩充 分配空间,元素个数有限

c语言代码实现(单链表)

//
//  main.c
//  线性表链式存储结构
//
//  Created by Kinble Wu on 2020/3/2.
//  Copyright © 2020 Kinble Wu. All rights reserved.
//
#include "include.h"

//定义节点数据类型名称
typedef int ElemType;

//定义节点数据结构
typedef struct Node{
    ElemType data;  //数据域
    struct Node* next;  //指针域
}Node;

//定义节点指针
typedef struct Node* LinkList;

//获取链表中第pos个元素
int GetElem(LinkList list,int pos){
    LinkList p;
    ElemType *e;
    p = list->next;
    int j = 1;

    //进入循环,直到j==pos时,退出循环
    while (p&&j<pos) {
        p = p->next;
        ++j;
    }
    //判断,当到链表尾部或者j大于pos时,返回失败
    if(!p||j>pos){
        return FALSE;
    }
    //将p所指向的数据的地址赋值给e
    e = &p->data;
    //返回e所指向的数据
    return *e;
}

//对链表中第pos个位置进行元素的插入
int InsertElem(LinkList list,int pos,ElemType data){
    /*****************************************************************
    *Function://InsertElem
    *Description://将元素插入链表中
    *Calls://null
    *Called By://null
    *Input://list为指向链表头结点的指针,pos为要插入的位置,data为要插入的元素
    *Output://null
    *Return://TRUE或者FALSE
    *Others://无
    *****************************************************************/
    int j = 0;
    LinkList P,node_insert;
    
    P = list->next;
    j++;

    while (P&&j<pos){
        P = P->next;
    }
    //判断,当到链表尾部或者j大于pos时,返回失败
    if(!P||j>pos){
        return FALSE;
    }
    //给插入节点分配内存h空间
    node_insert = (LinkList)malloc(sizeof(Node));
    node_insert->data = data;
    node_insert->next = P->next;
    P->next = node_insert;
    return TRUE;
    
}

//对链表中第pos个节点删除
int DeleteElem(LinkList list,int pos){
    int j = 0;
    LinkList P;
    
    P = list->next;
    j++;
    //循环到第pos-1个节点处
    while (P->next&&j<pos-1){
        P = P->next;
    }
    //判断,当到链表尾部或者j大于pos时,返回失败
    if(!P->next||j>pos-1){
        return FALSE;
    }
    //直接将pos-1的next指向pos-1next的next
    P->next = P->next->next;
    return TRUE;
    
}
//创建一个含有n个节点的链表(头插法)
void CreatList_head(LinkList *L,int num){
    LinkList P;
    int i = 0;
    *L = (LinkList)malloc(sizeof(Node));
    (*L)->next = NULL;
    while (i<num) {
        P = (LinkList)malloc(sizeof(Node));
        P->data = i;
        P->next = (*L)->next;
        (*L)->next = P;
        i++;
    }
}
//创建一个含有n个节点的链表(尾插法)
void CreatList_tail (LinkList *L,int num){
    LinkList P,Q;
    int i = 0;
    *L = (LinkList)malloc(sizeof(Node));
    P = *L;
    
    while (i<num) {
        Q = (LinkList)malloc(sizeof(Node));
        Q->data = i;
        P->next = Q;
        P = Q;
        free(Q);//个人想法
        i++;
    }
    P->next = NULL;
}

//单链表的整表删除
int Delet_WholeList(LinkList *L){
    LinkList P,Q;
    P = (*L)->next;
    while (P) {
        Q = P->next;
        free(P);
        P = Q;
    }
    (*L)->next = NULL;
    return TRUE;
}
int main(int argc, const char * argv[]) {
    // insert code here...
    
    
    
    printf("Hello, World!\n");
    return 0;
}

发布了35 篇原创文章 · 获赞 102 · 访问量 5万+

猜你喜欢

转载自blog.csdn.net/Baron_wu/article/details/104672662