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

📄 pr11020.cpp

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

////////////////////////////////////////
// The ListEntry class.
////////////////////////////////////////
class ListEntry
{
    char* listvalue;
    ListEntry* preventry;

public:
    ListEntry(char*);
    ~ListEntry()
        { delete [] listvalue; }
    ListEntry* PrevEntry() const
        { return preventry; };
    void display() const
        { std::cout << std::endl << listvalue; }

    // Use the 'this' pointer to chain the list.
    void AddEntry(ListEntry& le)
        { le.preventry = this; }
};

// The constructor definition.
ListEntry::ListEntry(char* s)
{
    listvalue = new char[std::strlen(s)+1];
    std::strcpy(listvalue, s);
    preventry = 0;
}

////////////////////////////////////////
// The main() function.
////////////////////////////////////////
int main()
{
    ListEntry* prev = 0;

    // Read in some names.
    while (1)
    {
        std::cout << std::endl 
                  << "Enter 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.
        ListEntry* list = new ListEntry(name);

        // Add the entry to the linked list.
        if (prev != 0)
            prev->AddEntry(*list);
        prev = list;
    }

    // Display the names in reverse order.
    while (prev != 0)
    {
        prev->display();
        ListEntry* hold = prev;
        prev = prev->PrevEntry();

        // Delete the ListEntry object.
        delete hold;
    }

    return 0;
}

⌨️ 快捷键说明

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