zoukankan      html  css  js  c++  java
  • 【C/C++】链表

    #include <bits/stdc++.h>
    using namespace std;
    
    struct node
    {
        int data; // 数据
        node* next; // 指针
    };
    
    node* create(int Array[])
    {
        node *p, *pre, *head; // head头节点, pre当前节点的前驱节点,p当前节点
        head = new node;
        head->next = NULL;
        pre = head;
        for (int i = 0; i < 5; i++)
        {
            // 创建新节点
            p = new node;
            // 将Array[i]赋给新建的节点作为数据域,也可以scanf输入
            p->data = Array[i];
            p->next = NULL;
            pre->next = p;
            // 更新
            pre = p;
        }
        return head;
    }
    
    int search(node* head, int x)
    {
        int count = 0;
        node* p = head->next;
        while (p != NULL)
        {
            if(p->data == x)
            {
                count++;
            }
            p = p->next;
        }
        return count;
    }
    
    void insert(node* head, int pos, int x)
    {
        node* p = head; // 保护头指针
        // 找到位置
        for(int i = 0; i < pos - 1; i++)
        {
            p = p->next; // pos - 1是为了找到位置的前一个节点
        }
        node* q = new node;
        q->data = x;
        q->next = p->next;
        p->next = q;
    }
    
    void del(node* head, int x)
    {
        node* p = head->next;
        node* pre = head;
        while(p != NULL)
        {
            if(p->data == x)
            {
                pre->next = p->next;
                delete(p);
    
            }
            else
            {
                pre = p;
                p = p->next;
            }
            
        }
    }
    
    int main()
    {
        int Array[5] = {5, 3, 6, 1, 2};
        node* L = create(Array);
        L = L->next;
        while(L != NULL)
        {
            printf("%d", L->data);
            L = L->next;
        }
        system("pause"); 
    }
    
  • 相关阅读:
    C1FlexGrid双grid滚动条联动
    linux实用命令
    大数据学习1(linux环境搭建)
    一个数据开发工程师要知道的名词
    oracle快速向表插入大量数据
    读书笔记--(索引的扫描方式)
    SQL优化策略(数据仓库)
    oracle大表删除数据方案
    加密与解密
    Spring笔记之IOC
  • 原文地址:https://www.cnblogs.com/kinologic/p/14702058.html
Copyright © 2011-2022 走看看