7.26 数据结构学习笔记-链表

数据结构-链表

一. 初始化和添加(insert)时出现的问题

typedef int E;   //这个还是老样子
struct ListNode {
    E element;   //保存当前元素
    struct ListNode * next;   //指向下一个结点的指针
};
typedef struct ListNode * Node;   //这里我们直接为结点指针起别名,可以直接作为表实现
void initList(Node head){
    head->next = NULL;   //头结点默认下一个为NULL
}
bool insertList(Node after, E element, int index){//index表示在第几个地方插入,实际上是取代第几个的意思,原来的向后排
    printf("刚开始运行嵌入函数");
    if(index < 1) return 0;   //如果插入的位置小于1,那肯定是非法的
    while (index--) {   //通过--index的方式不断向后寻找前驱结点
        after = after->next;   //正常情况下继续向后找
          if(after == NULL) return 0;  
          //如果在寻找的过程中发型已经没有后续结点了,那么说明index超出可插入的范围了,也是非法的,直接润
    }
    Node node=(Node)malloc(sizeof (struct ListNode));
    node->element=element;
    node->next=after->next->next;//错 笔记
    after->next=node;
    printf("%d",node->element);
    printf("运行完了嵌入函数");
    return 1;
}

错误1. while (index–) {

index表示在第几个地方插入,实际上是取代第几个的意思,原来的向后排,并且这里要寻找的是after节点,所以在第index地方插时,从头开始往后 index-1 次就到了

错误2. node->next=after->next->next;

添加节点时,之前的节点的next就是新加节点的next,这里还没有把新节点添加到链表中,所以不需要next->next

二. 删除节点时出现的问题

void deleteList(Node head,int index)
{
    while(--index)
    {
        head=head->next;
    }
    head->next=head->next->next;
    free(?);
}

错误1. 没有检查输入数据合理性

_Bool deleteList(Node head, int index){
    if(index < 1) return 0;  
    while (--index) {
        head = head->next;
        if(head == NULL) return 0;
    }
    if(head->next == NULL) return 0;  //注意删除的范围,如果前驱结点的下一个已经是NULL了,那么也说明超过了范围
    return 1;
}

错误2. 释放无用节点

Node tmp = head->next;   //先拿到待删除结点
    head->next = head->next->next;   //直接让前驱结点指向下一个的下一个结点
    free(tmp);   //最后使用free函数释放掉待删除结点的内存

三. 相对完整且正确的代码,实现了对链表的增删改查等功能

#include <bits/stdc++.h>
using namespace std;

typedef int E;   //这个还是老样子
struct ListNode {
    E element;   //保存当前元素
    struct ListNode * next;   //指向下一个结点的指针
};
typedef struct ListNode * Node;   //这里我们直接为结点指针起别名,可以直接作为表实现

void initList(Node head);
bool insertList(Node after, E element, int index);
void printList(Node head);
bool deleteList(Node head,int index);
int getList(Node head, int index);
int sizeList(Node head);
int findList(Node head, int element);

int main()
{
    ListNode Linked;
    Node Linked_List=&Linked;
    initList(Linked_List);
    insertList(Linked_List,1,1);
    insertList(Linked_List,2,2);
    insertList(Linked_List,3,3);
    deleteList(Linked_List,2);
    printf("打印链表中所有节点的值:");
    printList(Linked_List);
    printf("查询链表中第二个节点的值:%d\n",getList(Linked_List,2));
    printf("链表中查询值为3的节点的位置:%d\n",findList(Linked_List,3));
    printf("链表中查询值为5的节点的位置:%d\n",findList(Linked_List,5));
    printf("查询链表长度:%d\n",sizeList(Linked_List));
}
void initList(Node head){
    head->next = NULL;   //头结点默认下一个为NULL
}
bool insertList(Node after, E element, int index){//index表示在第几个地方插入,实际上是取代第几个的意思,原来的向后排
    if(index < 1) return 0;   //如果插入的位置小于1,那肯定是非法的
    while (--index) {   //通过--index的方式不断向后寻找前驱结点
        after = after->next;   //正常情况下继续向后找
          if(after == NULL) return 0;  
          //如果在寻找的过程中发型已经没有后续结点了,那么说明index超出可插入的范围了,也是非法的,直接润
    }
    Node node=(Node)malloc(sizeof (struct ListNode));
    node->element=element;
    node->next=after->next;
    after->next=node;
    return 1;
}
void printList(Node head)
{
    while(1)
    {
        head=head->next;
        if(head==NULL) break; 
        printf("%d",head->element);
    }
}
bool deleteList(Node head,int index)
{
    if(index<1) return 0;
    while(--index)
    {
        head=head->next;
        if(head == NULL) return 0;
    }
    if(head->next == NULL) return 0;
    Node tmp = head->next;   //先拿到待删除结点
    head->next = head->next->next;   //直接让前驱结点指向下一个的下一个结点
    free(tmp);   //最后使用free函数释放掉待删除结点的内存
    return 1;
}
int getList(Node head, int index)
{
    if(index<1) return 0;
    while(index--)
    {
        head=head->next;
    }
    return head->element;
}
int findList(Node head, int element)
{
    int index=0;
    while(1)
    {
        head=head->next;
        index++;
        if(head->element==element) return index;
        if(head->next==NULL) return 0; 
    }
}
int sizeList(Node head)
{
    int size= 0;
    while(1)
    {
        if(head->next==NULL) return size;
        head=head->next;
        size++;
    }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值