zoukankan      html  css  js  c++  java
  • 单向链表2

    #include <iostream>
    using namespace std;

    class Node//节点
    {
    public:
    int data;
    Node* next;

    public:
    Node() :data(0), next(NULL){}
    };

    class List//链表
    {
    public:
    List() :headnode(NULL), lastnode(NULL), node(NULL), length(0){}//创建链表,链表的初始化
    void Add(int x);//增加一个节点
    bool isempty()const;//判断链表是否为空
    void travle()const;//遍历这个链表

    private:
    Node* headnode;//头节点
    Node* lastnode;//尾节点
    Node* node;//临时节点
    int length;//链表长度
    };

    void List::travle()const
    {
    Node* temp = headnode;
    while (temp!=NULL)
    {
    cout << temp->data<<" ";
    temp = temp->next;
    }
    return;
    }

    void List::Add(int x)
    {
    node = new Node();
    node->data = x;
    if (lastnode==NULL)
    {
    lastnode = node;
    headnode = node;
    }
    lastnode->next = node;
    lastnode = node;
    length++;//链表长度加1
    }

    bool List::isempty()const
    {
    return lastnode == NULL;
    }

    int main()
    {
    List a;
    a.Add(1);
    a.Add(3);
    a.Add(5);
    a.Add(7);

    a.travle();
    //int b;
    return 0;
    }

  • 相关阅读:
    Centos 7 KVM安装win10
    python3.6小程序
    linux随手笔记(Centos为主)
    python 3.6练习题(仿购物车)
    linux mint软件安装
    pacman详解及常见问题
    manjaro安装及设置
    Ansible安装及配置
    大盘分时黄白线
    渊海子平学习
  • 原文地址:https://www.cnblogs.com/a-dreaming-dreamer/p/5737995.html
Copyright © 2011-2022 走看看