c++ - 如何将单链表变成双向链表

标签 c++ arrays visual-studio linked-list doubly-linked-list

我无法将这个单链表更改为双向链表,以便我可以向后打印一串字符(char s[] 变量)。我不想反转链表,只是反向打印它,同时使用双向链表。那么如何用下面的代码实现一个双向链表呢?

#include <string.h>
#include <iostream>

using namespace std;


class node
{
  public:
  char data;
  node *next;
  node *prev;
};

int _tmain(int argc, _TCHAR* argv[])
{
    char s[] = "abcdefghijklmnopqrstuvwxyz";

    node *head; //start of list
    node *temp;
    node *current;

    head = new node;          // create the head of the linked list
    head->data = s[0];
    head->next = NULL;
    temp = head;   // get ready for the loop - save the head in temp - you are going to change temp in the loop

    for(size_t i=1; i < strlen(s); i++)      // create the rest of the linked list
    {
        current = new node;    // make a new node
        current->data = s[i];  // set it's data member
        current->next = NULL;
        temp->next = current;  // point to the new node
        temp = current;        // make temp point to current node (for next time through)
    }

    node *ptr = head;    // set a ptr to head, then increment the pointer

    while (ptr != NULL)
    {
        cout << ptr->data; // print out the linked list
        ptr = ptr->next;   // increment the linked list
    }

    cout << endl;
    system("pause");
    return 0;
}

最佳答案

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

class node
{
public:
   char data;
   node *next;
   node *prev;
   node(char Data, node* Prev, node* Next);
};

node::node(char Data, node* Prev, node* Next)
    : data(Data), prev(Prev), next(Next)
{
    if (prev)prev->next = this;
    if (next)next->prev = this;
}

int _tmain(int argc, _TCHAR* argv[])
{
    char s[] = "abcdefghijklmnopqrstuvwxyz";

    node *temp;
    node *current;

    node *head; // ptr to head of list
    node *tail; // ptr to tail of list

    // call constructor and initialize first node 
    head = tail = new node(s[0], NULL, NULL);

    for (size_t i = 1; i < strlen(s); i++) // create the rest of the linked list
    {
        tail = new node(s[i], tail, NULL);
    }

    node *ptr = head; // set a ptr to head, then you are going to "increment" the pointer

    while (ptr != NULL)
    {
        cout << ptr->data; // print out the linked list
        ptr = ptr->next;   // increment the linked list
    }

    node *ptr1 = tail; // set a ptr to tail, then "decrement" the pointer form z to a
    cout << endl;

    while (ptr1 != NULL)
    {
        cout << ptr1->data; // print out the linked list
        ptr1 = ptr1->prev; // decrement the linked list
    }

    cout << endl << endl;
    system("pause");
    return 0;
}

关于c++ - 如何将单链表变成双向链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28579244/

相关文章:

c++ - Visual Studio 2012 上的 WildMagic 5.9

C++11 - 单独 move 数组(原始数组、std::array、std::vector)的每个元素?

c++ - 如何使用模板递归检查硬编码 int 数组是否在编译时排序?

git - 覆盖 Git 中的本地更改

c++ - 返回类型与自动的模板特化和显式规范

c++ - 过滤器列表 - C++

c - 如何用文件中的整数填充数组?

c - Fwrite() 将结构数组写入 c 中的文件

c++ - Visual Studio c++ 仅在 Debug模式下 LNK 1104 错误

c++ - 使用 refprop.dll 调用在访问冲突读取位置出现未处理的异常