简单 单链表的增删改查 c++描述

硅谷探秘者 2705 0 0

简单 单链表的增删改查 c++描述

class node{
public :
    int data;
    node * next;
};
#include "node.h"
using namespace std;
class stack{
public :
    node * head;
    stack(){
        head=NULL;
    }
    void add(int data);
    void del(int data);
    void print();
    void edit(int olddata,int newdata);
    node * getnode(int data);
};
#include<iostream>
#include "stack.h"
using namespace std;
void stack::add(int data){//添加节点
        if(head==NULL){    
            head=new node();
            head->data=data;
            head->next=NULL;
        }else{
            node * n=head;
            while(n->next!=NULL){
                n=n->next;
            }
            node * n_=new node();
            n_->data=data;
            n_->next=NULL;
            n->next=n_;
        }
}
void stack::print(){//打印节点
        node * n=head;
        while(n!=NULL){
            cout<<n->data<<" ";
            n=n->next;
        }
}
void stack::del(int data){//删除节点
    if(head!=NULL){
        if(head->data==data){
            head=head->next;
            return;
        }
    }else{
        return;
    }
    node * n=head;
    while(n->next!=NULL){
        if(n->next->data==data){
            node * n_=n->next;
            n_=NULL;
            n->next=n->next->next;
            return;
        }
        n=n->next;
    }
}
void stack::edit(int olddata,int newdata){//更改节点data
    node * n=head;
    while(n!=NULL){
        if(n->data==olddata){
            n->data=newdata;
            return;
        }
        n=n->next;
    }
}
node* stack::getnode(int data){//获取节点
    node * n=head;
    while(n!=NULL){
        if(n->data==data){
            return n;
        }
        n=n->next;
    }
    return NULL;
}

void main(){
    stack * s=new stack();
    s->add(1);
    s->add(2);
    s->add(3);
    s->add(4);
    s->del(1);
    s->edit(3,6);
    s->print();
    cout<<endl;
    cout<<s->getnode(6)->data<<endl;
}


QQ截图20181128214407.png



评论区
请写下您的评论...
暂无评论...
猜你喜欢
数据结构与算法 2154 双向c++classnode{public:intdata;node*next;node*prev;};#include"node.h"classrelink
数据结构与算法 2971 式栈出栈入栈操作c++基于双向//节点classnode{public:intdata;node*next;node*prev;};//双向#include"node.h
official 831 leetcode第237题()原接:https://leetcode-cn.com/problems/delete-node-in-a-linked-list/submissions/问题
linux系统 1649 编写shell脚本有两点要求1.脚本一般都是以#!/bin/bash开头(告诉我们系统我们这个脚本使用bash进行解释,不加也可以运行)2.给予脚本可执行权限3.执行shell脚本方式1)./脚本名2)sh+脚本名(此种方式不需要有可执行权限,但是不建议使用)
框架 2210 创建一个Maven项目,2.修jdk版本(因为这里使用springboot是1.5,在2.0一下springboot推荐使用1.7)!--修jdk版本springboot2.0之前推荐使用
数据结构与算法 4351 递归实现合并两递-合并后保持递序列java数据结构:算法:递归节点packageclub.test;/****节点*@authorjiajia
框架 2595 1.项目结构2.pom文件依赖?xmlversion="1.0"encoding="UTF-8"?projectxmlns="http://maven.apache.org/POM/4.0.0"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.
official 771 leetcode第79题(中等)原接:https://leetcode-cn.com/problems/word-search/问题给定一个二维网格和一个词,找出该词是否存在于网格中。
归档
2018-11  12 2018-12  33 2019-01  28 2019-02  28 2019-03  32 2019-04  27 2019-05  33 2019-06  6 2019-07  12 2019-08  12 2019-09  21 2019-10  8 2019-11  15 2019-12  25 2020-01  9 2020-02  5 2020-03  16 2020-04  4 2020-06  1 2020-07  7 2020-08  13 2020-09  9 2020-10  5 2020-12  3 2021-01  1 2021-02  5 2021-03  7 2021-04  4 2021-05  4 2021-06  1 2021-07  7 2021-08  2 2021-09  8 2021-10  9 2021-11  16 2021-12  14 2022-01  7 2022-05  1 2022-08  3 2022-09  2 2022-10  2 2022-12  5 2023-01  3 2023-02  1 2023-03  4 2023-04  2 2023-06  3 2023-07  4 2023-08  1 2023-10  1 2024-02  1 2024-03  1 2024-04  1
标签
算法基础 linux 前端 c++ 数据结构 框架 数据库 计算机基础 储备知识 java基础 ASM 其他 深入理解java虚拟机 nginx git 消息中间件 搜索 maven redis docker dubbo vue 导入导出 软件使用 idea插件 协议 无聊的知识 jenkins springboot mqtt协议 keepalived minio mysql ensp 网络基础 xxl-job rabbitmq haproxy srs 音视频 webrtc javascript
目录
没有一个冬天不可逾越,没有一个春天不会来临。最慢的步伐不是跬步,而是徘徊,最快的脚步不是冲刺,而是坚持。