数据结构-线性表-单链表

单链表定义

单链表:具有指向下一个节点的结构体.

功能描述

  • 创建新链表
  • 获取长度
  • 按索引查找
  • 按值查找
  • 删除某节点
  • 更改某结点值
  • 向后添加一个结点
  • 显示所有结点内容

代码实现

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

typedef struct LNode{
    int num;
    LNode * next;
};
//创建链表
LNode * creatLNode(){
    LNode * lNode=(LNode *)malloc(sizeof(LNode));
    lNode->next=NULL;
    return lNode;
}
//获取长度
int lengthLNode(LNode *L){
    int i=0;
    LNode *lNode=L;
    while(lNode->next!=NULL){
        lNode=lNode->next;
        i++;
    }
    return i+1;//之所以+1,因为指针指向最后一个元素,但i没用自增,故+1
}
int getLenth(LNode *L){
    int length=0;
    LNode *p=L;
    while (p){
        p=p->next;
        length++;
    }
    return length;
}
//向后添加一个结点
bool addLnode(LNode * L,int num){
    LNode * last=L;
    LNode * lnode=(LNode *)malloc(sizeof(LNode));
    lnode->num=num;
    lnode->next=NULL;
    while (last->next!=NULL){
        last=last->next;
    }
    last->next=lnode;
    return true;
}
//查找 通过索引值
LNode * findNodeByIndex(LNode *L,int index){
    LNode *p=L;
    int i=0;
    while (p!=NULL&&i<index){
        p=p->next;
        i++;
    }
    if(i==index)return p;
    else return NULL;
}
//查找 通过值
LNode *findLNodeByValue(LNode *L,int value){
    LNode *p=L;
    while(p){
        if(p->num==value){
            return p;
        }
        p=p->next;
    }
    return NULL;
}
LNode * findLNodeByV2(LNode *L, int value){
    LNode *p=L;
    while (p!=NULL&&p->num!=value){
        p=p->next;
    }
    return p;
}
//插入 索引值为插入结点
//存在插入头结点的情况,故以返回值形式的函数
LNode* insert(LNode *L,int index,int num){
    LNode* p;
    LNode *newL=(LNode *)malloc(sizeof(LNode));
    newL->num=num;
    if (index==0){
        newL->next=L;
        return newL;
    }
    //获取前结点
    p=findNodeByIndex(L,index-1);
    //判断是否为空
    if (p=NULL){
        return NULL;
    }
    //进行插入操作
    newL->next=p->next;
    p->next=newL;
    return L;
}
//修改某节点值
bool changeValueByIndex(LNode *L,int index, int value){
    if (index<0||index>getLenth(L)){
        return false;
    }
    LNode * p=findNodeByIndex(L,index);
    p->num=value;
    return true;
}
//删除某结点 存在删除头结点,故用函数返回值方式返回链表
LNode * delNodeByIndex(LNode *L,int index){
    if (index==0){
        LNode *p=L;
        p=p->next;
        free(L);
        return p;
    }
    LNode * p1=findNodeByIndex(L,index-1);
    if (p1==NULL||p1->next==NULL){
        return NULL;
    }
    LNode *p2=p1->next;
    p1->next=p2->next;
    free(p2);
    return L;
}

//显示所有节点内容
void showAllLNode(LNode *L){
    LNode * lNode=L;
    while(lNode){
        printf("%d\t",lNode->num);
        lNode=lNode->next;
    }
    printf("\n");
}
int main(){
    LNode *lNode=creatLNode();
    for (int i = 0; i <10 ; i++) {
        addLnode(lNode,i);
    }
    LNode *head=findNodeByIndex(lNode,0);
    changeValueByIndex(lNode,0,-1);
    lNode=delNodeByIndex(lNode,5);
    showAllLNode(lNode);

    return 0;
}
### 数据结构实验:单链表的实现方法与示例代码 #### 单链表简介 单链表是一种常见的线性数据结构,其中每个节点包含两部分:存储的数据项以及指向一个节点的指针。这种结构允许动态分配内存来保存任意数量的元素。 #### 改进后的单链表设计思路 为了克服传统单链表长时间使用后可能出现大量堆空间碎片化的问题,在“单链表”的内部增加了一片预留的空间,所有的`Node`对象都在这片空间中动态创建和销毁[^1]。此改进有助于减少因频繁增删操作而产生的内存碎片,从而提高程序性能。 #### Python中的单链表实现 下面是一个简单的Python版本单链表类定义及其基本功能: ```python class Node: def __init__(self, data=None): self.data = data # 节点储存的数据 self.next = None # 下一节点链接 class SingleLinkedList: def __init__(self): self.head = None def append(self, new_data): newNode = Node(new_data) if not self.head: self.head = newNode else: last_node = self.head while last_node.next: last_node = last_node.next last_node.next = newNode def display(self): current_node = self.head while current_node is not None: print(current_node.data,end=" -> ") current_node = current_node.next print("None") # 创建并测试单链表实例 linked_list = SingleLinkedList() elements_to_add = ['a', 'b', 'c'] for element in elements_to_add: linked_list.append(element) linked_list.display() # 输出 a -> b -> c -> None ``` 上述代码展示了如何构建一个具有追加新节点(`append`)和遍历显示所有节点(`display`)能力的基础版单向链表。通过这种方式可以有效地管理一系列相互关联的对象,并支持高效的插入/移除操作。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值