数据结构:链表创建、显示、求和、插入、删除等操作的代码实现

链表是数据结构最基础的内容之一,原理不算难,网上有非常多的基础介绍,我就不赘述了。在知道了链表这个数据结构后,最重要的是自己学会用代码去实现它,之后慢慢去运用它。一定要自己写才能真正搞懂它的含义,这里贴上我自己的实现代码仅供参考。

#include<iostream>


using namespace std;


//节点
struct node
{
    int data;
    node * next;
};


node *head;
int length, nodeValue, i, sum = 0, insert_value, insert_position, delete_position;


//初始化链表
void initialList()
{
    head = new node;
    head->data = NULL;
    head->next = NULL;
    length = 0;
}


//表尾插入法创建链表
void CreateList()
{
    int inputValue;
    cin >> inputValue;
    //尾指针指向头节点
    node *rearNode = head;
    // 1 新节点插入尾节点后 2 尾节点指向新节点 3 新节点后继置空
    while (inputValue != 0)
    {
        length++;
        //node *newNode;newNode = new node;
        node *newNode = new node;
        newNode->data = inputValue;
        //rearNode->next即rearNode当前所指向的节点的next,如最初rearNode->next代表head->next,即newNode加入head后
        rearNode->next = newNode;
        //尾指针后移,指向新加入的节点设为新的尾节点
        rearNode = newNode;
        //新节点后继指针为NULL
        rearNode->next = NULL;
        cin >> inputValue;
    }
}


//显示链表
void ViewList(node * head)
{
    node* tempNode = head->next;
    while (tempNode != NULL)
    {
        cout << tempNode->data << " ";
        tempNode = tempNode->next;
    }
    cout << endl << endl;
}



//递归计算链表总和/积
int calcSum(node *listNode)
{
    if (listNode->next == NULL)
        //乘法返回1,加法返回0
        return 0;
    return listNode->next->data + calcSum(listNode->next);
}


//指定序号节点值
int get_element_of_list(int i, node* listNode)
{
    int j = 0;
    node *tempNode = head;
    while (j != i&&tempNode != NULL)
    {
        tempNode = tempNode->next;
        j++;
    }
    if (tempNode == NULL)
        return 0;
    return tempNode->data;
}


//查找指定值的元素节点位置
void search(int value, node * listNode, int array[50])
{
    int i = 0, j = 1;
    node *tempNode = head->next;
    while (tempNode != NULL)
    {
        if (tempNode->data == value)
        {
            array[i] = j;
            i++;
            tempNode = tempNode->next;
        }
        else
            tempNode = tempNode->next;
        j++;
    }
}


//显示指定值所在的所有位置
void get_position()
{
    cin >> i;
    int *position = new int[50];
    memset(position, 0, 50);
    search(i, head, position);
    cout << i << " is the ";
    for (int i = 0;position[i] > 0;i++)
    {
        cout << position[i] << " ";
    }
    cout << "th value in your linked list." << endl << endl;
    delete[] position;
}


//插入指定值到指定位置
void insert(int value, int position)
{
    node *tempNode = head;
    node *insertNode = new node;
    int i = 0;
    //搜索第n-1个节点
    while (tempNode != NULL&&i != position - 1)
    {
        tempNode = tempNode->next;
        i++;
    }
    insertNode->data = value;
    insertNode->next = tempNode->next;
    tempNode->next = insertNode;
    length++;
}


//删除指定位置节点
void delete_list_position(int position)
{
    node *tempNode = head;
    node *deleteNode = new node;
    int i = 0;
    //搜索第n-1个节点
    while (tempNode != NULL&&i != position - 1)
    {
        tempNode = tempNode->next;
        i++;
    }
    deleteNode = tempNode->next;
    tempNode->next = deleteNode->next;
    delete deleteNode;
    length--;
}



int main()
{
    cout << "Please enter integer numbers and end up with 0 to create your linked list." << endl;
    //初始化链表
    initialList();
    //创建链表
    CreateList();
    //显示链表
    cout << "Your list is: ";
    ViewList(head);
    cout << "------------------------------------------------------------------------------------------" << endl;
    //求链表总和/积
    sum = calcSum(head);
    cout << "The sum of all list nodes is " << sum << "." << endl << endl;
    //链表长度
    cout << "The length of list is " << length << "." << endl << endl;
    cout << "------------------------------------------------------------------------------------------" << endl;
    //指定序号节点值
    cout << "Which node do you wanna know?(Please enter number standing for the position in 1-" << length << ")" << endl;
    cin >> i;
    nodeValue = get_element_of_list(i, head);
    cout << "The value of " << i << "th node is " << nodeValue << endl << endl;
    cout << "------------------------------------------------------------------------------------------" << endl;
    //显示指定值所在的所有位置
    cout << "Please enter a value that exists in your linked list, i'll show you its postion." << endl;
    get_position();
    cout << "------------------------------------------------------------------------------------------" << endl;
    //插入指定值到指定位置
    cout << "Insert any value to a position what you want." << endl << "Now first, please enter a value:";cin >> insert_value;
    cout << "Second, please enter a position (the value of this position is in 1-" << length + 1 << " ):";cin >> insert_position;
    insert(insert_value, insert_position);
    cout << endl << "Upgraded list is: ";
    ViewList(head);
    cout << "The length of upgraded list is " << length << "." << endl << endl;
    cout << "------------------------------------------------------------------------------------------" << endl;
    //删除指定位置节点
    cout << "Delete any value in a position what you want." << endl << "Please enter a position (the value of this position is in 1-" << length << " ):";cin >> delete_position;
    delete_list_position(delete_position);
    cout << endl << "Upgraded list is: ";
    ViewList(head);
    cout << "The length of upgraded list is " << length << "." << endl << endl;
    cout << "------------------------------------------------------------------------------------------" << endl;

    return 0;
}

参考资料:

怎样让函数返回数组

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值