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;
    }

  • 相关阅读:
    The requested resource (/) is not available解决办法
    字符问题
    Unknown column in 'field list'
    table 和 div 简单布局
    css简介
    div 与 table 的优点
    瞎搞
    html
    小计--关联 复制表结构
    ddl dml dcl
  • 原文地址:https://www.cnblogs.com/a-dreaming-dreamer/p/5737995.html
Copyright © 2011-2022 走看看