⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 pr11025.cpp

📁 c++编程宝典源码及Quincy99编译器 是《标准C++编程宝典》电子工业出版社的光盘
💻 CPP
字号:
////////////////////////////////////////
// File Name: pr11025.cpp
////////////////////////////////////////
#include <iostream>
#include <cstring>

////////////////////////////////////////
// The ListEntry class.
////////////////////////////////////////
class ListEntry
{
    // Static list head and tail pointers.
    static ListEntry* firstentry;
    static ListEntry* lastentry;

    char* listvalue;
    ListEntry* nextentry;

public:
    ListEntry(char*);
    ~ListEntry()
        { delete [] listvalue;}

    // Static member function.
    static ListEntry* FirstEntry()
        { return firstentry; }

    ListEntry* NextEntry() const
        { return nextentry; };
    void display() const
        { std::cout << listvalue << std::endl; }
};

// Define the static pointers.
ListEntry* ListEntry::firstentry;
ListEntry* ListEntry::lastentry;

// The class constructor.
ListEntry::ListEntry(char* s)
{
    if (firstentry == 0)
        firstentry = this;
    if (lastentry != 0)
        lastentry->nextentry = this;
    lastentry = this;
    listvalue = new char[std::strlen(s)+1];
    std::strcpy(listvalue, s);
    nextentry = 0;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    // Read in some names.
    while (1)
    {
        std::cout << "\nEnter a name ('end' when done): ";
        char name[25];
        std::cin >> name;

        if (std::strncmp(name, "end", 3) == 0)
            break;

        // Make a list entry of the name.
        new ListEntry(name);
    }

    // Get a pointer to the first list entry.
    ListEntry* next = ListEntry::FirstEntry();

    // Display the names.
    while (next != 0)
    {
        next->display();
        ListEntry* hold = next;
        next = next->NextEntry();
        delete hold;
    }

    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -