如何更改单链表成双向链表 [英] How to change a single linked list into doubly linked list

查看:184
本文介绍了如何更改单链表成双向链表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我无法改变这个单链表成双向链表,这样我可以打印字符的字符串(char中[]变量)倒退。我不想扭转链表,只是打印出来的反向,同时使用一个双向链表。那么,如何实现与下面code双向链表?

 的#include<&string.h中GT;
#包括LT&;&iostream的GT;使用命名空间std;
类节点
{
  上市:
  char数据;
  节点*接下来的;
  节点* preV;
};INT _tmain(INT ARGC,_TCHAR *的argv [])
{
    个char [] =ABCDEFGHIJKLMNOPQRSTUVWXYZ;    节点*头; //启动列表
    节点*温度;
    节点*电流;    头=新节点; //创建一个链表头
    流浆>数据= S [0];
    流浆>接着= NULL;
    TEMP =头; //准备好循环 - 保存温度头上 - 你要改变温度的循环    用于(为size_t I = 1; I<的strlen(S);我++)//创建链接列表的其余部分
    {
        电流=新节点; //创建一个新节点
        电流 - >数据= S [I] //设置它的数据成员
        电流 - >接着= NULL;
        TEMP->接下来=电流; //指向新节点
        TEMP =电流; //使温度点当前节点(下一次通过)
    }    节点* PTR =头; //设置一个PTR头,然后递增指针    而(PTR!= NULL)
    {
        COUT<< ptr->数据; //打印出该链接的表
        PTR = ptr->接下来, //递增链表
    }    COUT<< ENDL;
    系统(暂停);
    返回0;
}


解决方案

 的#include<&string.h中GT;
#包括LT&;&iostream的GT;
使用命名空间std;类节点
{
上市:
   char数据;
   节点*接下来的;
   节点* preV;
   节点(char数据,节点* preV,节点*下一页);
};节点::节点(char数据,节点* preV,节点*下一页)
    :数据(数据),preV(preV),下一个(下一页)
{
    如果(preV)preV->接下来=这一点;
    如果(下)下一页 - > preV =这一点;
}INT _tmain(INT ARGC,_TCHAR *的argv [])
{
    个char [] =ABCDEFGHIJKLMNOPQRSTUVWXYZ;    节点*温度;
    节点*电流;    节点*头; // PTR以列表的头
    节点*尾; // PTR到列表尾部    //调用构造函数和初始化第一个节点
    头= =尾新节点(S [0],NULL,NULL);    用于(为size_t I = 1; I<的strlen(S);我++)//创建链接列表的其余部分
    {
        尾=新节点(S [I]中,尾,NULL);
    }    节点* PTR =头; //设置一个PTR头,那么你要增量的指针    而(PTR!= NULL)
    {
        COUT<< ptr->数据; //打印出该链接的表
        PTR = ptr->接下来, //递增链表
    }    节点* ptr1的尾巴=; //设置一个PTR到尾部,然后在减量指针形式Z到A
    COUT<< ENDL;    而(ptr1的!= NULL)
    {
        COUT<< ptr1->数据; //打印出该链接的表
        ptr1的= ptr1-> preV; //递减链表
    }    COUT<< ENDL<< ENDL;
    系统(暂停);
    返回0;
}

I'm having trouble changing this single linked list into a doubly linked list so that I can print a string of characters (the char s[] variable) backwards. I don't want to reverse the linked list, just print it in reverse, while using a doubly linked list. So how do I implement a doubly linked list with the following code?

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

这篇关于如何更改单链表成双向链表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆