创建2个字符串C ++的链表

时间:2015-02-02 01:36:26

标签: c++ string linked-list

我正在尝试通过使用2个字符串制作链接列表来自学链接列表,该字符串基于字母表中的第一个字符串。然后,该函数将返回指向按字母顺序排序的2个字符串列表。我不知道为什么,但我的下面的代码没有做我期望它做的事情。如果有人可以查看我的代码并让我知道为什么这不起作用,我将非常感激。

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

Node* makePairList (string s1, string s2) {
    Node* h = NULL;
    Node* n = NULL;
    Node* t = NULL;

    if(s1 > s2)
    {
        n = new Node();
        n->val = s1;
        t=n;
        h=n;

        n= new Node();
        n -> val = s2;
        n-> next = NULL;
        t -> next = n;
        t=n;
    }
    else
    {
        n = new Node();
        n->val = s2;
        t=n;
        h=n;

        n= new Node();
        n -> val = s1;
        n->next = NULL;
        t -> next = n;
        t=n;
    }

    return h;
}

0 个答案:

没有答案