【问题标题】:Doubly Linked list instream - C++双向链表流内 - C++
【发布时间】:2016-02-24 05:19:04
【问题描述】:

我正在做一个项目,它从输入文件中读取并将数字放在两个单独的双向链表中,并在两者之间进行操作(加法、乘法、)并将它们输出到第三个双向链表。

我被困在如何让循环逐个字符读取并为每个数字创建一个新节点。

最终的目的是为每个节点更改不同大小的数字。

文件看起来类似于:

0*0

890+0

0*400

7650.4+100.26

160008800999008800+4

976976*863586589

代码如下:

#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>

using namespace std;

struct node// original node
{
    int data;
    node* next;
    node* prev;
};

struct node *newNode(int data)// add new node with int data input
{
    struct node *new_node = (struct node *) malloc(sizeof(struct node));
    new_node->data = data;
    new_node->next = NULL;
    new_node->prev = NULL;
    return new_node;
};

// This is supposed to add a new node to a doubly linked list at the end
void insert(node** head_ref,int n)// NOT WORKING PLEASE HELP
{
    node* temp = newNode(n);
    temp->data = n;
    temp->next = NULL;
    temp->prev = (*head_ref);
    (*head_ref)->next = temp;
    temp = (*head_ref);

    /*last->next = temp;
    last = temp;*/

}

void push(struct node** head_ref, int new_data)// insert at the beginning of the list
{
    /* allocate node */
    struct node* new_node = newNode(new_data);

    /* link the old list off the new node */
    new_node->next = (*head_ref);

    /* move the head to point to the new node */
    (*head_ref) = new_node;
}

/* Adds contents of two linked lists and return the head node of resultant list */
struct node* addTwoLists(struct node* first, struct node* second)
{
    struct node* res = NULL; // res is head node of the resultant list
    struct node *temp, *prev = NULL;
    int carry = 0, sum;

    while (first != NULL || second != NULL) //while both lists exist
    {
        // Calculate value of next digit in resultant list. 
        // The next digit is sum of following things
        // (i)  Carry
        // (ii) Next digit of first list (if there is a next digit)
        // (ii) Next digit of second list (if there is a next digit)
        sum = carry + (first ? first->data : 0) + (second ? second->data : 0);

        // update carry for next calulation
        carry = (sum >= 10) ? 1 : 0;

        // update sum if it is greater than 10
        sum = sum % 10;

        // Create a new node with sum as data
        temp = newNode(sum);

        // if this is the first node then set it as head of the resultant list
        if (res == NULL)
            res = temp;
        else // If this is not the first node then connect it to the rest.
            prev->next = temp;

        // Set prev for next insertion
        prev = temp;

        // Move first and second pointers to next nodes
        if (first) first = first->next;
        if (second) second = second->next;
    }

    if (carry > 0)
        temp->next = newNode(carry);

    // return head of the resultant list
    return res;
}

void printList(node* list)
{
    node* temp = list;
    while (temp != NULL)
    {
        cout << temp->data << "";
        temp = temp->next;
    }

}

int main(int argc, char* argv[])

{



    struct node* res = NULL;
    struct node* first = NULL;
    struct node* second = NULL;

    // create 2 lists for testing, not the final intention
    push(&first, 6);
    push(&first, 4);
    push(&first, 9);
    push(&first, 5);
    push(&first, 7);



    push(&second, 4);
    push(&second, 8);



    //Set input
    string filename = argv[1];
    filename.erase(0, 14);

    std::string s = filename;
    std::string delimiter = "=";

    size_t pos = 0;// Get digits per node
    std::string token;
    while ((pos = s.find(delimiter)) != std::string::npos) {
        token = s.substr(0, pos);
        s.erase(0, pos + delimiter.length());
    }

    int digitsPerNode = std::stoi(s);//obtain DigitsPerNode

    std::string f = filename;//get correct filename
    std::string cutoff = ";";
    std::string reducedname = f.substr(0, f.find(cutoff)); // token is "scott"

    ifstream instream(reducedname);//Obtain Input Stream

    string operands;

    char currentchar;
    string group;





    //Char solution
    bool isfirstoperation= true;
    char operation = '+';
    while (instream.get(currentchar))
    {
            for (int i = 0; i < digitsPerNode; i++)
            {
                if (currentchar == '\n')
                {
                    cout << "piripitiflautica" << endl;
                }
                cout << currentchar << endl;
                if (isdigit(currentchar))
                {


                }
                if (currentchar == '+')
                {
                    isfirstoperation = false;
                    operation = '+';
                }
                if (i != digitsPerNode)
                {
                    instream.get(currentchar);
                }
            }

            if (currentchar == '\n')
            {
                cout << "newline" << endl;
            }



        /*cout <<endl << "Final: "<< group;
        group = "";
        cout <<endl;*/
    }

    //Operate

    printList(first);
    cout << operation;// display operation

    printList(second);
    cout << "=" ;//Display answer
    if (operation = '+')
    {
        res = addTwoLists(first, second);
        //printf("Resultant list is ");
        printList(res);
    }



    cout << endl;
    return 0;
}

【问题讨论】:

  • 请同时发布输入文件的格式。

标签: c++ loops add doubly-linked-list


【解决方案1】:

让你开始的两个问题:在insert(你没有打电话)中,temp = (*head_ref); 是倒退的;应该是*head_ref = temp;

push 中,您不会更新现有列表中的prev 节点(处理此问题时请注意空列表)。

【讨论】:

  • (*head_ref)->next = temp;这是正确的顺序(在编辑的行上方)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多