双链表反向打印?

时间:2013-12-15 08:42:06

标签: c++ linked-list doubly-linked-list

我必须将一个名为“reversePrint”的成员函数添加到这个双向链表中。并反向打印出名称。 例如,输入: 贾森 瑞安 迪伦 路易

输出应该是: 路易 迪伦 瑞安 杰森

我想完成这项任务的方法是首先一直向下移动到列表的末尾,即最后一个节点。然后使用“Previous”指针一次返回前一个节点,同时打印出名称。因此我会反转印刷。 但是,我认为我的指针有问题,我无法弄清楚如何修复它。一些帮助非常感谢!!!

**更新:此代码最初是单独链接的。我试图把它变成双重链接并添加“reversePrint”功能。但是我认为问题在于,我没有修改“添加”方法,所以“prev”指针是断开的。那么任何有关如何修改“添加”的帮助,以便我可以将其变成一个完全双重链接列表?

这是它现在提供的输出:  prev 0x0  地址0x100200000数据a  下一个0x100300000

prev 0x0  地址0x100300000数据b  下一个0x100400000

prev 0x0  地址0x100400000数据c  下一个0x0

这是我们需要的输出:  prev 0x0  地址0x100200000数据a  下一个0x100300000

prev 0x100200000  地址0x100300000数据b  下一个0x100400000

prev 0x100300000  地址0x100400000数据c  下一个0x0

以下是我的代码:

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

    // define a node for storage and linking
    class node{
    public:
        string name;
        node *next;
        node *prev; // to be implemented by students
    };

    class linkedList{
    public:
        linkedList():top(NULL){}
        bool empty(){return top == NULL;}
        node *getTop(){return top;}
        void setTop(node *n){top = n;}
        void add(string);
        int menu();
        void remove(string);
        ~linkedList();
        void reversePrint(); // to be implemented by students
        friend ostream& operator << (ostream&, const linkedList&); // default output   is in-order print.
    private:
        node *top;
        node *end; // to be used for reverse print and implemented by students
    };

    int main(void){
        linkedList l;
        //cout << l.empty() << endl;
        int option = 0;
        string s;
        bool go = true;
        while(go){
            option = l.menu();
            switch(option){
                case 1: cout << "enter a name: ";cin >> s; l.add(s); break;
                case 2: cout << "enter name to be deleted: "; cin >> s; l.remove(s);break;
                case 3: cout << l; break;
                case 4: l.reversePrint(); break;
                case 5: cout << "exiting" << endl; go = false; break;
            }
        }
        // l goes out of scope and calls ~linkedList()
        return(NULL);
    }
    // can not call this method "delete" - "delete" is a reserved keyword.
    void linkedList::remove(string s){
        bool found = false;
        node *curr = getTop(), *prev=NULL;
        while(curr != NULL){
            // match found, delete
            if(curr->name == s){
                found = true;
                // found at top
                if(prev == NULL){
                    node *temp = getTop(); // delete the current node(which is the head), and set the "new head" to as the next node
                    setTop(curr->next);
                    delete(temp);
                    // found in list - not top
                }else{
                    prev->next = curr->next; //Skip the current deleted node, connect previous node directly to the next node
                    delete(curr);
                } }
            // not found, advance pointers
            if(!found){
                prev = curr;
                curr = curr->next; }
            // found, exit loop
            else curr = NULL; }
        if(found)cout << "Deleted " << s << endl;
        else cout << s << " Not Found "<< endl; }


    void linkedList::add(string s){
        node *n = new node();
        n->name = s;
        n->next = NULL;
        // take care of empty list case
        if(empty()){ top = n;
            // take care of node belongs at beginning case
        } else if(getTop()->name > s)
        {
            n->next = getTop();
            setTop(n);
            // take care of inorder and end insert
        }else{
            // insert in order case
            node *curr = getTop(), *prev = curr;
            while(curr != NULL){
                if(curr->name > s)break;
                prev = curr;
                curr = curr->next;
            }
            if(curr != NULL){ // search found insert point
                n->next = curr;
                prev->next = n; }
            // take care of end of list insertion
            else if(curr == NULL){// search did not find insert point
                prev->next = n; }
        } }

    void linkedList::reversePrint(){
        node *curr = getTop(), *prev=NULL;
        // jump to the last node
        while(curr->next != NULL){
            prev = curr;
            curr = curr->next;

            //curr = curr->next;
            cout << "!!!!hahaha" << curr->name <<" Prev:" <<curr->prev << "    " << prev->name <<endl ;//testing purpose

        }

       //print the name then jump back to the previous node, stops at the first node which curr->prev = NULL
       while(prev != 0 ){
            cout << "NULL is not 0!";
            prev->prev = curr->next;
            //cout << curr->name;
            curr = prev;
        }


    }

    /*ostream& operator << (ostream& os, const linkedList& ll){
        //linkedList x = ll; // put this in and the code blows up - why?
        node *n = ll.top;
        if(n == NULL)cout << "List is empty." << endl;
        else
            while(n != NULL){
                os << n->name << endl;
                n = n->next;
            } return os;
    }*/
    ostream& operator << (ostream& os, const linkedList& ll){
        //linkedList x = ll; // put this in and the code blows up - why?
        node *n = ll.top;
        if(n == NULL)cout << "List is empty." << endl;
        else
            while(n != NULL){
                os << " prev " << n->prev << endl;
                os << " address " << n << " data " << n->name << endl;
                os << " next " << n->next << endl;
                n->prev = n;
                n = n->next;
            } return os;
    }

    // return memory to heap

    linkedList::~linkedList(){
        cout << "~linkedList called." << endl;
        node *curr = getTop(), *del;
        while(curr != NULL){
            del = curr;
            curr = curr->next;
            delete(del);
        }
    }

    int linkedList::menu(){
        int choice = 0;
        while(choice < 1 || choice > 5){
            cout << "\nEnter your choice" << endl;
            cout << " 1. Add a name." << endl;
            cout << " 2. Delete a name." << endl;
            cout << " 3. Show list." << endl;
            cout << " 4. Show reverse list. " << endl; // to be implemented by students
            cout << " 5. Exit. " << endl;
            cin >> choice;
        }
        return choice;
    }

2 个答案:

答案 0 :(得分:2)

这看起来有问题:

prev->prev = curr->next;

您正在设置上一个节点的上一个节点。您可能只想设置上一个节点,对吗?

你可能想做这样的事情:

while(curr != NULL)
{
    cout << curr->name;
    curr = curr->prev
}

从那时起,您已确定curr是列表中的最后一个元素,您现在可以通过为其前一个元素分配curr来'递减'curr。由于第一个元素没有前一个节点,因此prev将为NULL。这样,当curr被分配给第一个元素的前一个元素时,循环将停止,该元素再次为NULL

答案 1 :(得分:1)

您的功能添加无效。例如,考虑列表为空的情况。以下代码段将与此案例相对应。

void linkedList::add(string s){
    node *n = new node();
    n->name = s;
    n->next = NULL;
    // take care of empty list case
    if(empty()){ top = n;
        // take care of node belongs at beginning case
    } else if(getTop()->name > s)

如您所见,未设置列表的数据成员结尾。

因此,在编写函数reversePrint的实现之前,应确保函数add正常工作。