单链表的表示和实现

表示

typedef struct node_t {
    data_t data;
    struct node_t *next = NULL;
} linknode_t, *linklist_t;

实现

//创建
linklist_t CreateEmptyLinklist()
{
    linklist_t list;
    list = (linklist_t)malloc(sizeof(linknode_t));
    if (NULL != list) {
        list->next = NULL;
        return list;
    } else {
        return NULL;
    }
}
//清空
int ClearLinklist(linklist_t list)
{
    linknode_t *node;
    if (NULL != list) {
        while (NULL != list->next) {
            node = list->next;
            list->next = node->next;
            free(node);
        }
        return 0;
    } else {
        return -1;
    }
}
//销毁
int DestroyLinklist(linklist_t list)
{
    if (NULL != list) {
        ClearLinklist(list);
        free(list);
        return 0;
    } else {
        return -1;
    }
}
//是否为空
int EmptyLinklist(linklist_t list)
{
    if (NULL != list) {
        if (NULL != list->next)
            return 1;
        else
            return 0;
    } else {
        return -1;
    }
}
//当前长度
int LengthLinklist(linklist_t list)
{
    int i = 0;
    linknode_t *node;
    if (NULL != list) {
        node = list->next;
        while (NULL != node) {
            node = node->next;
            i++;
        }
        return i;
    } else {
        return -1;
    }
}
//查
int GetLinklist(linklist_t list, int at, data_t *x)
{
    int i = 0;
    linknode_t *node;
    if (NULL != list) {
        if (at < 0) return -1;
        node = list->next;
        while (NULL != node) {
            if (at == i) {
                if (x != NULL)
                    *x = node->data;
                return 0;
            }
            node = node->next;
            i++;
        }
    } else {
        return -1;
    }
}
//改
int SetLinklist(linklist_t list, int at, data_t x)
{
    int i = 0;
    linknode_t *node;
    int found = 0;
    if (NULL != list) {
        if (at < 0) return -1;
        node = list->next;
        while (NULL != node) {
            if (at == i) {
                node->data = x;
                found = 1;
                break;
            }
            node = node->next;
            i++;
        }
    } else {
        return -1;
    }
    if (found == 1)
        return 0;
    else
        return -1;
}
//增
int InsertLinklist(linklist_t list, int at, data_t x)
{
    int i = 0;
    linknode_t *node_at, *node_prev, *node_new;
    int found = 0;
    if (at < 0) return -1;
    if (NULL != list) {
        node_new = (linknode_t *)malloc(sizeof(linknode_t));
        if (NULL == node_new) {
            return -1;
        }
        node_new->data = x;
        node_new->next = NULL;

        node_prev = list;
        node_at = list->next;
        while (NULL != node_at) {
            if (at ==i) {
                found = 1;
                break;
            }
            node_prev = node_at;
            node_at = node_at->next;
            i++;
        }
    } else {
        return -1;
    }
    if (found) {
        node_new->next = node_prev->next;
        node_prev->next = node_new;
    } else {
        node_prev->next = node_new;
    }
    return 0;
}
//删
int DeleteLinklist(linklist_t list, int at)
{
    linknode_t *node_prev, *node_at;
    int pos = 0;
    int found = 0;
    if (NULL != list) {
        node_prev = list;
        node_at = list->next;
        if (at < 0) return -1;
        while (NULL != node_at) {
            if (pos == at) {
                found = 1;
                break;
            }
            node_prev = node_at;
            node_at = node_at->next;
            pos++;
        }
    } else {
        return -1;
    }
    if (found == 1) {
        node_prev->next = node_at->next;
        free(node_at);
        return 0;
    } else {
        return -1;
    }
}
//翻转:插入
linklist_t ReverseLinklist(linklist_t list)
{
    if (NULL == list) return NULL;

    linknode_t *node_at, *node;

    node_at = list->next;
    list->next = NULL;
    while (NULL != node_at) {
        node = node_at;
        node_at = node_at->next;
        node->next = list->next;
        list->next = node;
    }
    return list;
}
//翻转:掉头
linklist_t ReverseLinklist2(linklist_t list)
{
    if (NULL != list) return NULL;

    linknode_t *node_prev, *node_at, *node_next;

    node_prev = NULL;
    node_at = list->next;

    while (NULL != node_at) {
        node_next = node_at->next;

        if (NULL == node_next) {
            list->next = node_at;
        }
        node_at->next = node_prev;
        node_prev = node_at;
        node_at = node_next;
    }

}

测试代码

void iterate_list(linklist_t list)
{
    linknode_t *node;

    if (!list) return;

    printf("list = {");

    /* start from the first element */
    node = list->next;
    while (NULL != node) {
        printf("%d->", node->data);

        /* move to the next */
        node = node->next;
    }

    if (LengthLinklist(list) > 0)
        printf("\b\b} \b\n");
    else
        printf("}\n");
}

int main(int argc, char *argv[])
{
    int i;
    data_t a[10] = {2, 4, 6, 8, 10, 12, 14, 16, 18, 20};
    data_t x;

    linklist_t list;

    list = CreateEmptyLinklist();

    if (NULL == list) return -1;

    printf("insert method 1: insert each elment\n");
    for (i = 0; i < 10; i++) {
        if (InsertLinklist(list, i, a[i]) < 0)
            break;
    }
    iterate_list(list);

    GetLinklist(list, 4, &x);
    printf("list[4] = %d\n", x);

    printf("updated list[4] to 100\n");
    SetLinklist(list, 4, 100);
    GetLinklist(list, 4, &x);
    printf("now list[4] = %d\n", x);
    iterate_list(list);

    printf("removed list[4]\n");
    DeleteLinklist(list, 4);
    GetLinklist(list, 4, &x);
    printf("now list[4] = %d\n", x);
    printf("and total number of list is %d\n", LengthLinklist(list));
    iterate_list(list);

    printf("insert \"1\" at the %dth position of the list\n", 0);
    InsertLinklist(list, 0, 1);
    iterate_list(list);

    printf("reversed the list\n");
    ReverseLinklist(list);
    iterate_list(list);

    ClearLinklist(list);
    printf("after clear, total number of list is %d and ", LengthLinklist(list));
    iterate_list(list);

    DestroyLinklist(list);

    return 0;
}

结果

insert method 1: insert each elment
list = {2->4->6->8->10->12->14->16->18->20}
list[4] = 10
updated list[4] to 100
now list[4] = 100
list = {2->4->6->8->100->12->14->16->18->20}
removed list[4]
now list[4] = 12
and total number of list is 9
list = {2->4->6->8->12->14->16->18->20}
insert "1" at the 0th position of the list
list = {1->2->4->6->8->12->14->16->18->20}
reversed the list
list = {20->18->16->14->12->8->6->4->2->1}
after clear, total number of list is 0 and list = {}
时间: 2024-08-12 14:42:17

单链表的表示和实现的相关文章

单链表逆置

重写单链表逆置,熟能生巧- #include <iostream> #include <cstdlib> using namespace std; typedef struct List{ int num; struct List *next; }ListNode,*pListNode; void display(ListNode *pHead) { while(pHead) { cout<<pHead->num<<"--"; pH

02 单链表

线性表之链式存储---单链表 1 #include <stdio.h> 2 #include <stdlib.h> 3 #include <string.h> 4 5 // 数据结构 6 typedef struct node 7 { 8 int data; 9 struct node *next; 10 }linkList; 11 12 // 创建单链表,并初始化 13 linkList *linkList_init(void) 14 { 15 linkList *l

(单链表)单链表的整体逆序和局部逆序

题目一:将单链表翻转. 思路:有三种方式. 一:用数组存储单链表的值,然后重新逆序赋值,效率较低. 二:利用三个指针,在原来的基础上进行逆序.这种方法比较实用,效率也高. 三:从第2个节点到第N个节点,依次逐节点插入到第1个节点(head节点)之后,最后将第一个节点挪到新表的表尾.需要新建一个链表,这种方法和第二种差不多. 这里我就写出第二种方法,比较实用. 代码(方法二): struct ListNode { int val; ListNode *next; ListNode(int x) :

[c语言]单链表的实现

一.基础知识:链表(线性表的链式存储结构) (1)特点:逻辑关系相邻,物理位置不一定相邻. (2)分类: a.不带头节点 b.带头节点 (3)单链表的存储结构: typedef struct SListNode {  DataType data;  struct SListNode* next; }SListNode; 二.代码实现(因避开使用二级指针,所以代码中使用了c++中的引用):此处构造的为不带头节点的链表 (1)sList.h   #pragma once typedef int Da

单链表基本操作

//头文件 #pragma once #include <stdio.h> #include <assert.h> #include <malloc.h> #include <stdlib.h> typedef int DateType; typedef struct LinkNode {  DateType _data;  struct  LinkNode* _next; } LinkNode; void PrintList(LinkNode* pHead

C++单链表的创建与操作

链表是一种动态数据结构,他的特点是用一组任意的存储单元(可以是连续的,也可以是不连续的)存放数据元素.链表中每一个元素成为“结点”,每一个结点都是由数据域和指针域组成的,每个结点中的指针域指向下一个结点.Head是“头指针”,表示链表的开始,用来指向第一个结点,而最后一个指针的指针域为NULL(空地址),表示链表的结束.可以看出链表结构必须利用指针才能实现,即一个结点中必须包含一个指针变量,用来存放下一个结点的地址.结点中只有一个next指针的链表称为单链表,这是最简单的链表结构. 首先定义一个

每日一题3:判断单链表是否相交

由于单链表的特殊性,如果某一链表与另一链表相交,那么从相交的节点开始,以后两个链表的每个节点都相同,因此判断两个链表是否相交,只需判断每条链表的最后一个节点是否相同即可! #include "stdafx.h" #include <iostream> using namespace std; struct list_node { int data; list_node* next; }; list_node* CreateList(int datas[],int n) {

数据结构上机测试2-1:单链表操作A (顺序建表+关键字删除)

数据结构上机测试2-1:单链表操作A Time Limit: 1000MS Memory limit: 4096K 题目描述 输入n个整数,先按照数据输入的顺序建立一个带头结点的单链表,再输入一个数据m,将单链表中的值为m的结点全部删除.分别输出建立的初始单链表和完成删除后的单链表. 输入 第一行输入数据个数n: 第二行依次输入n个整数: 第三行输入欲删除数据m. 输出 第一行输出原始单链表的长度: 第二行依次输出原始单链表的数据: 第三行输出完成删除后的单链表长度: 第四行依次输出完成删除后的

大话数据结构---单链表

单链表在存储结构上与顺序存储的区别:不用开辟连续的存储空间,存储位置任意,只需要让数据的前驱知道它的位置就可以,而使用单链表示只需要知道单链表的第一个元素就能找到其他所有的元素,为了方便 一般会设置一个头指针指向第一个元素. 单链表的数据读取:通过头指针一个一个往后遍历 单链表的插入: 删除: 自定义单链表的简单实现: package com.neuedu.entity; /* * 项目名称:JavaSqList * @author:wzc * @date 创建时间:2017年9月2日 上午9:

数据结构(三)之单链表反向查找

一.反向查找单链表 1.简单查找 先遍历获取单链表单长度n,然后通过计算得到倒数第k个元素的下标为n-k,然后查找下标为n-k的元素. 2.优化查找 先找到下标为k的元素为记录点p1,然后设置新的记录点p2的下标从0开始,同时遍历两个记录点,直到p1的值为null,p2是倒数第k个元素. 单链表结点: package cn.edu.scau.mk; /** * * @author MK * @param <T> */ public class Node<T> { private T