链表代码设计

1.单链表定义

单链表是线性表的链式存取结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元指针就是连接每个结点的地址数据

2.单链表与顺序表区别

单链表与顺序表区别:这是两种不同的存储结构,顺序表是顺序存储结构它的特点是逻辑关系上相邻的两个元素在物理位置上也相邻。但链表是链式存储结构的特点是不需要逻辑上相邻的元素在物理位置上也相邻。因为链式存储结构可以通过结点中的指针域直接找到下一个结点的位置。

优缺点:

单链表的优缺点:
1.优点:可以按照实际所需创建结点增减链表的长度,更大程度地使用内存。
2.缺点:进行尾部或者任意位置上插入或删除时时间复杂度和空间复杂度较大,每次都需要通过指针的移动找到所需要的位置,相对于顺序表查找而言效率较低。

顺序表的优缺点:
1.优点:可以通过下标直接访问所需要的数据
2.缺点:不能按实际所需分配内存,只能使用malloc或者realloc函数进行扩容,容易实现频繁扩容,容易导致内存浪费与数据泄露等问题。

本文设计了最基本的单链表代码,其主要功能如下:

  • initLinkList():初始化链表。
  • printList(NodePtr paraHeader):打印链表。
  • appendElement(NodePtr paraHeader, char paraChar):添加元素。
  • insertElement(NodePtr paraHeader, char paraChar, int paraPosition):指定位置添加。
  • deleteElement(NodePtr paraHeader, char paraChar):删除元素。
  • 新增outMemory(NodePtr paraHeader):打印地址。 

在老师的基础上改用c++进行编码,将地址测试的函数改为outMemory打印链表元素地址函数,更加方便表示链表各元素在内存中的位置,代码如下:

//
// Created by A on 2023/3/28.
//
#include <iostream>

using namespace std;

/**
 * Linked list of characters. The key is data.
 */
typedef struct LinkNode {
    char data;
    struct LinkNode *next;
} LNode, *LinkList, *NodePtr;

/**
 * Initialize the list with a header.
 * @return The pointer to the header.
 */
LinkList initLinkList() {
    NodePtr tempHeader = (NodePtr) malloc(sizeof(LNode));
    tempHeader->data = '\0';
    tempHeader->next = NULL;
    return tempHeader;
}// Of initLinkList

/**
 * Print the list.
 * @param paraHeader The header of the list.
 */
void printList(NodePtr paraHeader) {
    NodePtr p = paraHeader->next;
    while (p != NULL) {
        cout << p->data;
        p = p->next;
    }// Of while
    cout << endl;
}// Of printList

/**
 * Add an element to the tail.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void appendElement(NodePtr paraHeader, char paraChar) {
    NodePtr p, q;

    // Step 1. Construct a new node.
    q = (NodePtr) malloc(sizeof(LNode));
    q->data = paraChar;
    q->next = NULL;

    // Step 2. Search to the tail.
    p = paraHeader;
    while (p->next != NULL) {
        p = p->next;
    }// Of while

    // Step 3. Now add/link.
    p->next = q;
}// Of appendElement

/**
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 * @param paraPosition The given position.
 */
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition) {
    NodePtr p, q;

    // Step 1. Search to the position.
    p = paraHeader;
    for (int i = 0; i < paraPosition; i++) {
        p = p->next;
        if (p == NULL) {
            cout << "The position " << paraPosition << " is beyond the scope of the list.";
            return;
        }// Of if
    } // Of for i

    // Step 2. Construct a new node.
    q = (NodePtr) malloc(sizeof(LNode));
    q->data = paraChar;

    // Step 3. Now link.
    cout << "linking " << paraChar << " in position " << paraPosition << endl;
    q->next = p->next;
    p->next = q;
}// Of insertElement

/**
 * Delete an element from the list.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 */
void deleteElement(NodePtr paraHeader, char paraChar) {
    NodePtr p, q;
    p = paraHeader;
    while ((p->next != NULL) && (p->next->data != paraChar)) {
        p = p->next;
    }// Of while

    if (p->next == NULL) {
        cout << "Cannot delete " << paraChar << endl;
        return;
    }// Of if

    q = p->next;
    p->next = p->next->next;
    free(q);
}// Of deleteElement

/**
 * Output the memory for the list.
 */
void outMemory(NodePtr paraHeader) {
    NodePtr p = paraHeader->next;
    while (p != NULL) {
        cout << "The value is " << p->data << ",the address is " << p << endl;
        p = p->next;
    }// Of while
    cout << endl;
}// Of basicAddressTest

/**
 * The entrance.
 */
int main() {
    cout << "---- LinkListTest begins. ---- " << endl;
    // Step 1. Initialize an empty list.
    LinkList tempList = initLinkList();
    printList(tempList);

    // Step 2. Add some characters.
    appendElement(tempList, 'H');
    appendElement(tempList, 'e');
    appendElement(tempList, 'l');
    appendElement(tempList, 'l');
    appendElement(tempList, 'o');
    appendElement(tempList, '!');
    printList(tempList);

    // Step 3. Delete some characters (the first occurrence).
    deleteElement(tempList, 'e');
    deleteElement(tempList, 'a');
    deleteElement(tempList, 'o');
    printList(tempList);

    // Step 4. Insert to a given position.
    insertElement(tempList, 'o', 1);
    printList(tempList);

    outMemory(tempList);

    cout << "---- LinkListTest ends. ---- " << endl;
}// Of main

运行结果如下:

---- LinkListTest begins. ----

Hello!
Cannot delete a
Hll!
linking o in position 1
Holl!
The value is H,the address is 0x7d1670
The value is o,the address is 0x7d16f0
The value is l,the address is 0x7d16b0
The value is l,the address is 0x7d16d0
The value is !,the address is 0x7d1710

---- LinkListTest ends. ----

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值