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



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

#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;

}
}

除了你从不创建一个列表或在其中放置任何东西......

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

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;
}

只需确保检查字符串是否超过一个字符即可。

事实上,你需要一个堆栈。可以使用标准容器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

相关内容

  • 没有找到相关文章

最新更新