线性表链表

#include <iostream>
using namespace std;

namespace linklist {
    typedef int DataType;
    typedef struct node {
        DataType data;
        node *next;
    }LinkNode;
    typedef LinkNode *LinkList;
    LinkList head;
    LinkNode *tail_node;//最后一个节点

    void InitLink() {
        //在创建链表前先初始化
        head = (LinkList)malloc(sizeof(LinkNode));
        memset(head, 0, sizeof(LinkNode));
        tail_node = head;
    }
    void AddNode(LinkList head, DataType elem) {
        if (!head) {
            return;
        }
        LinkNode *new_node = (LinkNode*)malloc(sizeof(LinkNode));
        if (tail_node == head) {
            new_node->data = elem;
            new_node->next = nullptr;
            head->next = new_node;
            tail_node = new_node;
        } else {
            new_node->data = elem;
            new_node->next = nullptr;
            tail_node->next = new_node;
            tail_node = new_node;
        }

    }
    int Size() {
        if (!head) {
            return 0;
        }
        int count = 0;
        LinkNode *first_node = head->next;
        while (nullptr != first_node) {
            ++count;
            first_node = first_node->next;
        }
        return count;
    }
    LinkNode *GetNode(LinkList head, int pos) {
        if (!head) {
            return nullptr;
        }
        LinkNode *first_node = head->next;
        int j = 1;
        while (nullptr != first_node && j < pos) {
            first_node = first_node->next;
            ++j;
        }
        if (j == pos) {
            return first_node;
        }
        return nullptr;

    }
    void InsertList(LinkList head, int pos, DataType elem) {
        if (!head) {
            return;
        }
        LinkNode *current_node = head->next;
        int j = 0;
        while (nullptr != current_node && j < pos-1) {
            current_node = current_node->next;
            ++j;
        }
        if (nullptr == current_node) {
            cout << "插入元素失败" << endl;
        } else {
            LinkNode *new_node = (LinkNode*)malloc(sizeof(LinkNode));
            new_node->data = elem;
            new_node->next = current_node->next;
            current_node->next = new_node;
        } 
    }
    void DeleteList(LinkList head, int pos) {
        if (!head) {
            return;
        }
        LinkNode *current_node = head;
        int i = 0;
        while (nullptr != current_node && i < pos - 1) {
            current_node = current_node->next;
            ++i;
        }
        if (nullptr != current_node) {
            LinkNode *delete_node = current_node->next;
            current_node->next = delete_node->next;
            free(delete_node);
        }
    }
    void SortList(LinkList head) {
        int size = Size();

    }
}

void PrintLink() {
    cout << "[";
    int count = linklist::Size();
    for (int index = 0; index < count; ++index) {
        linklist::LinkNode *node = linklist::GetNode(linklist::head, index + 1);
        if (nullptr != node) {
            cout << node->data << " ";
        }
    }
    cout << "]" << endl;

}

int main() {
    linklist::InitLink();
   // linklist::InsertList(linklist::head, 1, 100);
    linklist::AddNode(linklist::head, 60);
    linklist::AddNode(linklist::head, 30);
    linklist::AddNode(linklist::head, 20);
    linklist::AddNode(linklist::head, 50);
    //linklist::InsertList(linklist::head, 4, 40);
    //linklist::InsertList(linklist::head, 7, 40);
    linklist::DeleteList(linklist::head, 1);
    PrintLink();
    return 0;
}

1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看REAdMe.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READme.md或论文文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 5、资源来自互联网采集,如有侵权,私聊博主删除。 6、可私信博主看论文后选择购买源代码。
1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。 、 1资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看READmE.文件(md如有),本项目仅用作交流学习参考,请切勿用于商业用途。 1、资源项目源码均已通过严格测试验证,保证能够正常运行; 2、项目问题、技术讨论,可以给博主私信或留言,博主看到后会第一时间与您进行沟通; 3、本项目比较适合计算机领域相关的毕业设计课题、课程作业等使用,尤其对于人工智能、计算机科学与技术等相关专业,更为适合; 4、下载使用后,可先查看README.md文件(如有),本项目仅用作交流学习参考,请切勿用于商业用途。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值