English 中文(简体)
(C++)
原标题:Turn An Unsorted Link List Into A Sorted Link List? (C++)
  • 时间:2012-05-08 22:09:11
  •  标签:
  • c++
  • list

我有问题试图将INSERT功能变成一种按字母顺序排列的标志。 我写下了我迄今为止所尝试的内容......但是,我只检查了第一线的名称,看看它是否比职务辩论中新点的名称大。 请允许我提出一个想法,说明我如何能够走过每一个节点,并能够比较其钥匙(姓名)并相应地离开和正确? 以下是我迄今为止在守则和我的INSERT职能中的内容。

  // UnSortedLnkList.h
    //----------------------------

    #include <iostream>
    #include <afxwin.h>

    using namespace std;

    #define new DEBUG_NEW

    struct Node  {
        string m_name;  // key
        int m_age;

        Node* m_next;
        Node(const string& name, int age, Node* next = NULL);
    };

    ostream& operator<< (ostream&, const Node&);

    class ULnkList  {
        friend ostream& operator<< (ostream&, const ULnkList&); // 1.5
    public:
        ULnkList();
        // copy ctor
        ULnkList(const ULnkList& existingList );            // 5
        ~ULnkList();                                        // 4

        bool IsEmpty() const;
        int Size() const;
        bool Insert(const string& name, int age);           // 1
        bool Delete(const string& name);                    // 3
        bool Lookup(const string& name, int& age) const;    // 2

        ULnkList& operator =(const ULnkList& list2);        // 6

        bool Delete2(const string& name);   


    private:

        Node* m_head;   // points to head of the list
        int m_num;     // the number of entries in the list

        // helper functions:
        void Clear();                                       // 7
        void Copy(const ULnkList& list2);                   // 8
    };


    // UnSortedLnkList.cpp
    //----------------------------
    #include "UnSortedLnkList.h"
    #include <iostream>
    #include <string>
    using namespace std;

    Node::Node(const string& name, int age, Node* next)
    : m_name(name), m_age(age), m_next(next)
    {}


    ostream& operator<< (ostream& os, const Node& n)    // cout << n;
    {
        os << "Name: " << n.m_name << "	Age: " << n.m_age;
        return os;
    }

    ULnkList::ULnkList()
    : m_head(new Node("",-99,NULL)), m_num(0)
    {
        //m_head = new Node("",-99,NULL);
    }
    //
    ULnkList::ULnkList(const ULnkList& existingList ) 
    {
        Copy(existingList);
    }

    void ULnkList::Copy(const ULnkList& existingList)
    {
        m_num = existingList.m_num;
        // create dummy node
        m_head = new Node("",-99,NULL);
        // traverse existing list
        Node *pe = existingList.m_head->m_next;
        Node *pThis = m_head;
        while( pe != 0)
        {
            // create a copy of the Node in OUR list
            pThis->m_next  = new Node(pe->m_name,pe->m_age,0);

            // update pointers
            pe = pe->m_next;
            pThis = pThis->m_next;
        }
    }

    void ULnkList::Clear()
    {
        Node *p = m_head->m_next;
        Node *tp = m_head;          // trail pointer
        while( p != 0)
        {
            delete tp;

            // update pointers
            tp = p;  // 
            p = p->m_next;
        }

        delete tp;
    }

    ULnkList& ULnkList::operator =(const ULnkList& list2)  // list1 = list2;
    {
        // list1 = list1;  // check for self-assignment
        if( this != &list2 )
        {
            this->Clear(); // normally Clear();
            this->Copy(list2);
        }

        // l1 = l2 = l3;

        return *this;
    }

    bool ULnkList::IsEmpty() const
    {
        return m_num == 0;
        // return m_head->m_next == NULL;
    }

    int ULnkList::Size() const
    {

        return m_num;
    }
    //

    ULnkList::Insert(const string& name, int age)
    {
        Node *current = m_head->m_next;
        Node *previous = m_head;
        if (m_head->m_next == NULL) 
        {
            m_head->m_next   = new Node(name,age,m_head->m_next); 
            m_num++;
            return true;
        }
        if (name < m_head->m_next->m_name)
        {
            m_head->m_next = new Node(name,age,m_head->m_next);
            m_num++;
            return true;
        }




        return true;
    }

    //
    ostream& operator<< (ostream& os, const ULnkList& list) // cout << list;
    {
        Node *p = list.m_head->m_next; // first node with data

        while( p != 0 )
        {
            cout << *p << endl;  // ????

            // update p
            p = p->m_next;
        }
        cout << "--------------------------------------" << endl;

        return os;
    }

    //   input: name
    //// output: age if found
    bool ULnkList::Lookup(const string& name, int& age) const
    {
        // linear search
        Node *p = m_head->m_next;

        while( p != 0)
        {
            if( name == p->m_name )
            {
                // found it
                age = p->m_age;
                return true;
            }

            // update p
            p = p->m_next;
        }
        return false;
    }
    //
    bool ULnkList::Delete(const string& name)
    {
        Node *p = m_head->m_next;
        Node *tp = m_head;          // trail pointer
        while( p != 0)
        {
            if( name == p->m_name )
            {
                // found it, so now remove it
                // fix links
                tp->m_next = p->m_next;
                // delete the node
                delete p;

                return true;
            }

            // update pointers
            tp = p;  // tp = tp->m_next;
            p = p->m_next;
        }
        return false;
    }

    bool ULnkList::Delete2(const string& name)
    {
        Node *p = m_head;
        while( p->m_next != 0 )     // ?????
        {
            if( p->m_next->m_name == name )
            {
                Node *save = p->m_next;
                // remove the node
                // fix links
                p->m_next = p->m_next->m_next;
                // delete memory
                delete save;
                return true;
            }

            // update pointers
            p = p->m_next;
        }
        return false;
    }
    //
    ULnkList::~ULnkList()
    {
        Clear();
    }
    //
问题回答

我对这一法典持谨慎态度。

if(name > m_head->m_name ){
while(name > current->m_next->m_name){
    current = current->m_next;
}
// add temp = current->next
current->m_next = new Node(name,age)
// current->next->next = temp

You don t want to lose the list after where you insert.

我猜测这是家事,因此我不想写这部法典。 我建议,你的插入()功能可以通过比较显示的投入,然后采用“逻辑”来从头上走到正确的现场。 假设你必须使用字面清单作为你的数据结构。 如果你想要更好地平均增加业绩,那么你可以把双 bin树作为你的基本结构,但这将使清单更加复杂。

你的问题在于这一法典:

    if (name < m_head->m_next->m_name)
    {
        m_head->m_next = new Node(name,age,m_head->m_next);
        m_num++;
        return true;
    }

为了以某种方式插入,我或许会以休养职能为某种帮助。

但这可以奏效:

Node *current = m_head;
if(name > m_head->m_name ){
    while(name > current->m_next->m_name){
        current = current->m_next;
    }
    current->m_next = new Node(name,age,current->m_next);
}
else{
    m_head = new Node(name,age,m_head);
}     

This will insert in an ascendingly sorted manner. I haven t tested it, but let me know if it works! hope this helps!





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签