无法创建双向链表列表

时间:2017-02-22 17:01:35

标签: c++ arrays

我有两个问题,我很难理解。

1)我很难理解如何将我的L1 doublyLinkedList传递给一个数组,以便我可以保存我的txt文件读取的每个数字列表

2)如果我的负数不均匀,我的break_into_nodes()方法正在读取错误,因为stoi正在为负号创建1个节点,我将如何创建if语句以继续破坏它进入节点

#include "stdafx.h"
#include <iostream>
#include <iterator>
#include <fstream>
#include <string>
#include <cstdlib>
#include "ArgumentManager.h"
using namespace std;

struct Node
{
    long long value;
    Node *next, *prev;
    Node(long long y)
    {
        value = y;
        next = prev = NULL;
    }
};

class doubleLinkedList
{
    Node *back;

public:
    Node *front;
    doubleLinkedList() { front = NULL; back = NULL; }
    ~doubleLinkedList() { destroyList(); }
    doubleLinkedList(const string& num, int digitsPerNode) {
        appendNodeFront(stoi(num, 0, 10));
    }

    void appendNodeFront(long int x);
    void dispNodesForward(int digits);
    void destroyList();
    void clean();

};

void doubleLinkedList::clean()
{
    destroyList();
}

void doubleLinkedList::appendNodeFront(long int x)
{
    Node *n = new Node(x);
    if (front == NULL)
    {
        front = n;
        //back = n;
    }
    else
    {
        front->prev = n;
        n->next = front;
        front = n;
    }
}
void doubleLinkedList::dispNodesForward(int digits)
{
    Node *temp = front;
    int temp_val;

    if (temp != NULL)
    {
        /* First node does not get Zero padding */
        temp_val = (int)temp->value;
        printf("%d", temp_val);

        temp = temp->next;
        while (temp != NULL)
        {
            temp_val = (int)temp->value;
            printf("%0*d", digits, temp_val);
            temp = temp->next;
        }
    }
}
void doubleLinkedList::destroyList()
{
    Node *T = back;
    while (T != NULL)
    {
        Node *T2 = T;
        T = T->prev;
        delete T2;
    }
    front = NULL;
    back = NULL;
}

void break_into_nodes(doubleLinkedList *list, string number, int digits) {

    string  node_value;
    int     num_index, num_iterations;
    int     i, j;

    num_index = number.length();

    if (num_index < digits)
    {
        node_value = number;
        list->appendNodeFront(stoi(node_value));
    }
    else {

        /* adjust for incomplete nodes */
        if ((number.length() % digits) == 0)
            num_iterations = (number.length() / digits);
        else
            num_iterations = (number.length() / digits) + 1;

        for (j = 0; j < num_iterations; j++) {
            node_value.clear();
            for (i = 0; i < digits; i++) {
                num_index--;
                if (num_index < 0)
                    break;
                node_value = node_value.insert(0, number.substr(num_index, 1));
            }
            list->appendNodeFront(stoi(node_value));
        }
    }
}

// Driver program
int main(int argc, char* argv[]) {

    doubleLinkedList l1;

    if (argc < 2) {
        cerr << "Usage: infinitearithmetic \"input=xyz.txt;digitsPerNode=    <number>\"\n";
    }
    ArgumentManager am(argc, argv);
    string filename = am.get("input");

    /* Digits per Node ar from 1 to 8 */
    int digitsPerNode = stoi(am.get("digitsPerNode"));
    ifstream ifs(filename.c_str());
    string line;
    string num1;
    int i = 0;

    while (!ifs.eof())
    {
        getline(ifs, line);
        //cout << "" << line << endl;
        num1 = line;
        break_into_nodes(&l1, num1, digitsPerNode);
        l1.dispNodesForward(digitsPerNode);
        cout << endl;
        l1.clean();
        i++;
    }
    return 0;
}

0 个答案:

没有答案