0

I have two problems where I'm having a hard time to understand.

1) I'm having a hard time to understand how to pass my L1 doublyLinkedList into an array so that I can save each list of numbers my txt file reads

2) If I have an uneven negative number my break_into_nodes() method is reading an error as stoi is creating 1 node for a negative sign, how would I create an if statement to continue to breaking it into a node

#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;
}
Onur A.
  • 3,007
  • 3
  • 22
  • 37
David Gil
  • 9
  • 1
  • I was able to figure out 1), I just added a clause to include the negative sign on the last iteration, but can anyone point me in the right direction for how to include the l1 into an array to save my numbers an ultimately sort them. – David Gil Feb 22 '17 at 18:36
  • Highly recommend reading through [What is the Rule of Three?](http://stackoverflow.com/questions/4172722/what-is-the-rule-of-three) because `doubleLinkedList` violates it and could put you up for some rather hellish debugging. – user4581301 Feb 22 '17 at 19:48
  • Also highly recommend reading [Why is iostream::eof inside a loop condition considered wrong?](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) because `while (!ifs.eof())` can get you at least two different ways. – user4581301 Feb 22 '17 at 19:49
  • @user4581301 OK, I'm going to have to read both of those articles to understand what you mean, Thank you. In the mean time do you mind sharing a little knowledge about an array of doubly linked list or an article regarding the topic? – David Gil Feb 22 '17 at 21:29
  • Linked List is more a rite of passage than anything widely useful. What you learn from it is useful. Best way to deal with a linked list is drawing boxes on a piece of paper. You build the linked list on paper, drawing in all of the links one by one taking careful notes of what you did because the notes become your code. If you need to insert a link, draw each step of separating the existing links and attaching them to the inserted node. Removing is the same deal. Draw pictures. – user4581301 Feb 22 '17 at 22:27

0 Answers0