• [002]链表笔记--编程实现一个单链表的创建/测长/打印


    利用类实现链表的创建

     1 #include<iostream>
     2 
     3 using namespace std;
     4 
     5 class Link {
     6 public:
     7     Link() {}
     8     Link *_pNext;
     9     void Create();
    10     void Output();
    11     int GetElement();
    12 private:
    13     int _id;
    14     int _cnt;
    15 };
    16 
    17 void Link::Create() {
    18     cin >> this->_id;
    19 }
    20 
    21 void Link::Output() {
    22     cout << this->_id;
    23 }
    24 
    25 int Link::GetElement() {
    26     return this->_id;
    27 }
    28 
    29 int main() {
    30     Link *pLink = new Link;
    31     Link *pHead = pLink;
    32     bool isfull = false;
    33 
    34     while (!isfull) {
    35         Link *data = new Link;
    36         data->Create();
    37         if (data->GetElement() != 0) {
    38             data->_pNext = NULL;
    39             pHead->_pNext = data;
    40             pHead = pHead->_pNext;
    41         }
    42         else {
    43             isfull = true;
    44         }
    45     }
    46 
    47     pLink = pLink->_pNext;
    48 
    49     while (pLink != 0) {
    50         pLink->Output();
    51         pLink = pLink->_pNext;
    52     }
    53 
    54     return 0;
    55 }
  • 相关阅读:
    c语言中逆向显示数值
    c语言中无限循环语句
    css静态网页css代码
    css静态网页css代码
    考试html代码
    考试css代码
    考试html框架代码
    css静态网页代码
    5个CSS书写技巧
    5个CSS书写技巧
  • 原文地址:https://www.cnblogs.com/hustcser/p/3941561.html
Copyright © 2020-2023  润新知