使用链表C +向后打印字符串

时间:2016-03-02 21:41:16

标签: c++ string linked-list reverse singly-linked-list

好吧,好吧,我已经准备好接受教育和侮辱了。我必须创建一个接受字符串的简单程序,并使用链接列表向后打印。下面是我的实现,我尝试通过打印第一个节点来测试它,但没有出现。为什么会这样?

#include <iostream>
#include <cstddef>
#include <string>
#include <stdio.h>
#include <cstring>
#include <sstream>

using std::cout;
using std::endl;
using std::string;

void addNode(char x);

int main(int argc, const char *argv[]){
    string text;
    std::cout<<"Insert texst to see it backwards: ";
    std::cin >> text;

    //convert string to char array
    char ctext[1024];
    strcpy(ctext, text.c_str());

}
class Node{
      friend class LinkedList;
    private:
        string data;
        Node *pnext;

    public:

        //general constructor
        Node(void):pnext(NULL)
        {}

        //constructor with data value
        Node(string val):data(val),pnext(NULL)
        {}
        //constructor with data and next values
        Node(string val, Node* next):data(val), pnext(next)
        {}

        //Getters
        string getValue(void){
            return data;
        }

        Node* getNext(void){
            return pnext;
        }

};

class LinkedList
{
    private:

        Node *head;
        Node *tail;

    public:
        LinkedList(void); //general constructor
        LinkedList(string val);//constructor with value of a list node

        void addNode(char* x);
};

LinkedList::LinkedList(){
    head = tail = NULL;
}

LinkedList::LinkedList(string val){
    head = new Node(val);
    tail = head;
}

void LinkedList::addNode(char* x){

    if (head == NULL){
        tail = head= new Node(x[0]);
    }
    else{
    int i = 0;
    while (x[i]!=NULL){
        //creat new node with value
        Node* tmp = new Node(x[i]);
        //tmp node points to head
        tmp->pnext = head;
        //tmp becomes new head
        head=tmp;
        i++;
    }
    Node *p = head;
    std::cout<<p->data;



}
}

2 个答案:

答案 0 :(得分:1)

除了您从未创建列表或在其中放置任何内容之外......

您测试列表中是否没有节点:

if (head == NULL)

这是真的,因为它是第一次插入。然后创建一个节点:

tail = head= new Node(x[0]);

那就是它。你的if块在那里结束,并跳到if-else的末尾,那里没有更多的语句,只是函数体的结束括号。您可能不想使用if-else而只想使用if

int i = 0;
if (head == NULL)
{
    tail = head= new Node(x[0]);
    i = 1;
}

请确保检查字符串是否超过一个字符。

答案 1 :(得分:1)

实际上你需要一个堆栈。您可以使用标准容器std::forward_list以相反的顺序使用列表输出字符串。或者您当然可以使用标准容器适配器std::stack

如果您需要自己编写列表,那么程序可能类似于以下内容

#include <iostream>
#include <string>

class LinkedList
{
private:
    struct Node
    {
        char data;
        Node *next;
    } *head;
public:
    LinkedList() : head( nullptr ) {}
    LinkedList( const std::string &s ) : head( nullptr )
    {
        for ( char c : s ) push_front( c );
    }
    ~LinkedList()
    {
        while ( head )
        {
            Node *current = head;
            head = head->next;
            delete current;
        }
    }        
    void push_front( char c )
    {
        head = new Node { c, head };
    }        
    friend std::ostream & operator <<( std::ostream &, const LinkedList & );
};

std::ostream & operator <<( std::ostream &os, const LinkedList &lst )
{
    for ( LinkedList::Node *current = lst.head; current; current = current->next )
    {
        os << current->data;
    }

    return os;
}

int main()
{
    std::string s( "Hello World!" );
    std::cout << s << std::endl;

    LinkedList lst( s );
    std::cout << lst << std::endl;

    return 0;
}

程序输出

Hello World!
!dlroW olleH

如果需要,您可以将自己的方法添加到列表中。例如,您可以添加clearreset等方法。

以下是添加了frontpop_front方法的同一个类。

#include <iostream>
#include <string>

class LinkedList
{
private:
    struct Node
    {
        char data;
        Node *next;
    } *head;
public:
    LinkedList() : head( nullptr ) {}
    LinkedList( const std::string &s ) : head( nullptr )
    {
        for ( char c : s ) push_front( c );
    }
    ~LinkedList()
    {
        while ( head )
        {
            Node *current = head;
            head = head->next;
            delete current;
        }
    }
    LinkedList( const LinkedList & ) = delete;
    LinkedList & operator =( const LinkedList & ) = delete;
    void push_front( char c )
    {
        head = new Node { c, head };
    } 
    bool empty() const
    {
        return head == nullptr;
    }
    void pop_front()
    {
        if ( !empty() )
        {
            Node *current = head;
            head = head->next;
            delete current;
        }
    }
    char & front()
    {
        return head->data;
    }        
    const char & front() const
    {
        return head->data;
    }        
    friend std::ostream & operator <<( std::ostream &, const LinkedList & );
};

std::ostream & operator <<( std::ostream &os, const LinkedList &lst )
{
    for ( LinkedList::Node *current = lst.head; current; current = current->next )
    {
        os << current->data;
    }

    return os;
}

int main()
{
    std::string s( "Hello World!" );
    std::cout << s << std::endl;

    LinkedList lst( s );
    std::cout << lst << std::endl;

    while ( !lst.empty() )
    {
        std::cout << lst.front();
        lst.pop_front();
    }
    std::cout << std::endl;

    return 0;
}

程序输出

Hello World!
!dlroW olleH
!dlroW olleH