【数据结构】单链表+王道课后题

#include<iostream>
using namespace std;

typedef int ElemType;
typedef struct ListNode {
    ElemType val;
    ListNode* next;
}ListNode,* LinkedList;

bool InitList(LinkedList& L);   // 初始化
int length(LinkedList& L);      // 计算长度
int LocateElem(LinkedList& L, ElemType e); // 按找值查询元素e在顺序表中的位置
ElemType GetElem(LinkedList& L, int i);    // 查询顺序表中i位置对应的元素
bool ListInsert(LinkedList& L, int i, ElemType e); // 在顺序表L的i位置插入元素e
bool ListDelete(LinkedList& L, int i, ElemType& e);// 删除顺序表L中i位置的元素, 并且将删除的元素存储到引用e中
void PrintList(LinkedList& L);  // 打印链表
bool Empty(LinkedList& L);      // 判断是否为空
void DestroyList(LinkedList& L);// 销毁
bool HeadAdd(LinkedList& L, ElemType e); // 头插法
bool LastHead(LinkedList& L, ElemType e); // 尾插法
bool NewNode(LinkedList& node, ElemType e);// 创建节点

/**
 * 不带头节点链表实现
 * @param L
 * @return
 */
bool InitList(LinkedList& L) {
    L = nullptr;
    return true;
}

/**
 * 返回链表是否为空
 * @param L
 * @return
 */
bool Empty(LinkedList& L) {
    return L == nullptr;
}

/**
 * 计算链表长度
 * @param L
 * @return
 */
int length(LinkedList& L) {
    int ans = 0;
    LinkedList cur = L;
    while (cur) {
        cur = cur->next;
        ans++;
    }
    return ans;
}

/**
 * 遍历打印
 * @param L
 */
void PrintList(LinkedList& L) {
    LinkedList cur = L;
    while (cur) {
        cout << cur->val << " ";
        cur = cur->next;
    }
    puts("");
}

/**
 * 销毁链表
 * @param L
 */
void DestroyList(LinkedList& L) {
    LinkedList cur = L;
    while (cur) {
        LinkedList next = cur->next;
        free(cur);
        cur = next;
    }
    L = nullptr;
}

/**
 * 查找链表中i下标的值,在顺序表中元素是从1开始
 * @param L
 * @param i
 * @return
 */
ElemType GetElem(LinkedList& L, int i) {
    if (Empty(L) || i <= 0) return -1;
    LinkedList cur = L;
    while (cur) {
        if (i-- == 1) return cur->val;
        cur = cur->next;
    }
    return -1;
}

/**
 * 返回e在链表中的位置,在顺序表中元素是从1开始
 * @param L
 * @param e
 * @return
 */
int LocateElem(LinkedList& L, ElemType e) {
    LinkedList cur = L;
    int idx = 1;
    while (cur && cur->val != e) idx++, cur = cur->next;
    return idx;
}

/**
 * 在链表的i位置插入e,在顺序表中元素是从1开始
 * @param L
 * @param i
 * @param e
 * @return
 */
bool ListInsert(LinkedList& L, int i, ElemType e) {
    // 1. 进行参数合法性校验
    if (Empty(L) || i <= 0) return false;
    // 2. 初始特殊插入---头插
    if (i == 1) return HeadAdd(L, e);

    // 3. 寻找i位置的前一个节点
    LinkedList cur = L;
    while (cur && i-- != 2) {
        cur = cur->next;
    }

    // 4. 创建插入的节点
    if (cur == nullptr) return false;  // 非法下标
    LinkedList node;
    NewNode(node, e);

    // 5. 插入操作
    node->next = cur->next;
    cur->next = node;

    // 6. 返回
    return true;
}

/**
 * 删除i位置的元素,将其存储到e中
 * @param L
 * @param i
 * @param e
 * @return
 */
bool ListDelete(LinkedList& L, int i, ElemType& e) {
    // 1. 进行参数合法性校验
    if (Empty(L) || i <= 0) return false;
    // 2. 初始特殊插入---头节点
    if (i == 1) {
        L = L->next;
        return true;
    }

    // 3. 寻找i位置的前一个节点
    LinkedList cur = L;
    while (cur && i-- != 2) {
        cur = cur->next;
    }

    // 4. delete
    if (cur == nullptr) return false;
    e = cur->next == nullptr ? -1 : cur->next->val;
    cur->next = cur->next->next;

    // 5. 返回
    return true;
}

/**
 * 头插法
 * @param L
 * @param e
 * @return
 */
bool HeadAdd(LinkedList& L, ElemType e) {
    LinkedList node;
    if (!NewNode(node, e)) return false;
    node->next = L;
    L = node;
    return true;
}

/**
 * 创建节点
 * @param node
 * @param e
 * @return
 */
bool NewNode(LinkedList& node, ElemType e) {
    node = (LinkedList) malloc(sizeof(ListNode));
    if (node == nullptr) return false;
    node->val = e, node->next = nullptr;
    return true;
}

/**
 * 尾插法
 * @param L
 * @param e
 * @return
 */
bool LastHead(LinkedList& L, ElemType e) {
    if (L == nullptr) {
        LinkedList node;
        if (!NewNode(node, e)) return false;
        L = node;
        return true;
    }
    LinkedList cur = L;
    while (cur->next) cur = cur->next;
    LinkedList node;
    if (!NewNode(node, e)) return false;
    cur->next = node;
    return true;
}

int main2() {
    LinkedList head;
    InitList(head);
    HeadAdd(head, 1);
    HeadAdd(head, 11);
    HeadAdd(head, 111);
    HeadAdd(head, 1111);
    HeadAdd(head, 11111);
    HeadAdd(head, 111111);
    PrintList(head);
    ListInsert(head, 2, 222);
    PrintList(head);
    int e = 0;
    ListDelete(head, 2, e);
    cout << "delete -- e:" << e << endl;
    PrintList(head);
    cout << "locateElem 111: " << LocateElem(head, 111) << endl;
    cout << "length:" << length(head) << endl;
    cout << "getElem 1:" << GetElem(head, 1) << endl;
    LastHead(head, 100);
    PrintList(head);
    DestroyList(head);
    PrintList(head);

    return 0;
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

1886i

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值