按字母顺序放置节点链表

时间:2015-02-24 16:44:53

标签: c++

我需要这样做,以便用户可以输入文字直到他们点击输入' xxx'。这部分已经完成,但是我需要列表始终按字母顺序排列。我无法弄清楚如何做这个部分。我必须在添加新节点的函数中按字母顺序进行,而不是将节点打印到屏幕的函数。这是因为它是一个课程,是必需的。

节点头文件:

struct Node
{
    string       word;
    struct Node *next;
};

函数原型头文件:

Node *add_node(Node *list, const string &s);
Node *del_node(Node *list, const string &s);

void deallocate(Node *list);
void print(Node *list);

编译的代码:

#include    <iostream>
#include    <iomanip>
#include    <string>
using namespace std;

#include    "Node.h"
#include    "funcs.h"

int main()
{
    struct Node *list = 0;      // list is a pointer to struct Node

    cout << "please enter a few words (xxx to terminate list):\n";

    string s;                   // s is a string object

    while (cin >> s)           // read a string into s
    {
        if (s == "xxx")
        {
            break;              // terminate loop when string is equal to         "xxx"
        }
        // add s to list in alphabetical order
        list = add_node(list, s);

        cout << "\nlist:\n";
        print(list);
        cout << '\n';
    }

    cout << "\nhere is the list:\n";
    print(list);
    cout << '\n';

    cout << "please enter word words to delete from the list (xxx to    terminate):\n";
        while (cin >> s)
        {
            if (s == "xxx")
            {
                break;              // terminate loop when string is equal to "xxx"
            }
            // delete first node containing string s
            //list = del_node(list, s);

            cout << "\nlist:\n";
            print(list);
            cout << '\n';
        }

    cout << "\nthe final list:\n";
    print(list);
    cout << '\n';
    // deallocate the linked list
    cout << "\ndeallocating the list:\n";
    deallocate(list);

    cout << "\nall done!\n";
    return 0;
}

具有以下功能的代码:

#include    <iostream>
#include    <iomanip>
#include    <string>
using namespace std;

#include    "Node.h"


Node *add_node(Node *list, const string &s)
{
    struct Node *n = new struct Node;
    n->word = s;            // copy string s to word
    n->next = list;

    // add node n to the list 
    // the list should always be in ascending alphabetical order

    list = n;

    return list;            // returning pointer to beginning of the list
}


Node *del_node(Node *list, const string &s)
{
    // delete node in the list that contains s
    // the list should always be in ascending alphabetical order

    // if s does not appear in the list, there is nothing to do
    // if s appears multiple times in the list, delete the first occurrence
    Node *lastp = 0;
    Node *p = list;
    for (; p; p = p->next)
    {
        if (p->word == s)
        {
            lastp->next = p->next;
            delete p;
            break;
        }
        lastp = p;
    } 


    return list;            // returning pointer to beginning of the list
}


void deallocate(Node *list)
{
    for (struct Node *p = list; p;)
    {
        struct Node *tmp = p;   // remember current pointer

        p = p->next;            // advance p to the next node

        delete tmp;             // deallocate tmp

        // OK to print pointers tmp and p
        cout << "deallocated\t" << tmp << "\tnext is\t" << p << '\n';
    }
}


void print(Node *list)
{
    for (struct Node *p = list; p; p = p->next)
    {
        cout << p << '\t' << setw(8) << p->word
            << '\t' << "next:" << '\t' << p->next << '\n';
    }
}

所有代码可能都不需要回答这个问题,但我想我会把它包括在内。

1 个答案:

答案 0 :(得分:1)

尝试以下演示程序中显示的以下函数add_node。考虑到您需要再定义一个将释放列表中所有已分配内存的函数。

#include <iostream>
#include <string>

struct Node
{
    std::string word;
    struct Node *next;
};

Node *add_node( struct Node *list, const std::string &s )
{
    struct Node *prev = nullptr;
    struct Node *current = list;

    while ( current && !( s < current->word ) )
    {
        prev = current;
        current = current->next;
    }

    if ( prev == nullptr )
    {
        list = new Node { s, list };
    }
    else
    {
        prev->next = new Node { s, prev->next };
    }

    return list;
}

void print_list( const struct Node *list )
{
    for ( ; list != nullptr; list = list->next ) std::cout << list->word << ' ';
}

int main()
{
    struct Node *list = nullptr;

    // for ( const std::string &s : { "B", "X", "A", "C", "F", "G" } )
    for ( const char *s : { "B", "X", "A", "C", "F", "G" } )
    {
        list = add_node( list, s );
    }

    print_list( list );
    std::cout << std::endl;

    return 0;
}

输出

A B C F G X 
相关问题