双向链表内流 - C++

Doubly Linked list instream - C++

本文关键字:C++ 双向链表      更新时间:2023-10-16

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

我陷入了如何使循环逐个字符读取并为每个数字创建一个新节点的困扰。

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

该文件将如下所示:

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

两个问题让你入门:在insert(你没有调用),temp = (*head_ref);是倒退的;它应该是*head_ref = temp;的。

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