How to replace 0's with blank space in Linked List

I have a file with pairs of very large numbers that I'm reading as a string, then converting them into integers and adding them into a linked list. Then, when it's added, I would like to compare the sizes of the pair of large integers and fill the difference with blanks, but it being an integer linked list, I'm only able to add zeroes.
Is there another way to read in these large integers that would allow me to insert spaces instead of zeros?

eg) These are my 2 large ints:
Large Integer 1: 80485080443358
Large Integer 2: 4849850549686868696

This is what I want:
Large Integer 1: ____80485080443358 (Not the underscores but the space)
Large Integer 2: 4849850549686868696

This is what I have:
Large Integer 1: 0000080485080443358
Large Integer 2: 4849850549686868696

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
    struct Node{
    int data;
    Node *next;
    };

    Node *createNode(int n){
        Node *newNode;
        newNode=(Node*)malloc(sizeof(Node));
        newNode->data=n;
        newNode->next=NULL;
    }

    Node *getLast(Node *top){
        Node *curr, *last;
        curr=top;
        if(top==NULL)
            return NULL;
        while(curr!=NULL){
            last=curr;
            curr=curr->next;
        }
    return last;
    }

    Node *InsertAtTail(Node *top, int n){
        Node *newNode, *last;
        newNode = createNode(n);
        last=getLast(top);
            if(last==NULL)
            top=newNode;
        else last->next=newNode;
    return top;
    }

    Node *InsertAtHead(Node *top, int n){
        Node *newNode;
        newNode = createNode(n);
        newNode->next= top;
        top=newNode;
    return top;
    }
    
    int getSize(Node *top){
        Node *curr;
        int num=0;
        curr=top;
        while(curr!=NULL){
            num++;
            curr=curr->next;
        }
    return num;
    }
    
    void printList(Node *top, ofstream& out){
        Node *curr;
        int counter=0;
        if(top==NULL){
            out<<"List is empty"<<endl;
            return;
        }
        curr=top;
            while(curr!=NULL){
                out<<curr->data;
                curr=curr->next;
            }
    }
    
    int converttoInt(char n){
        int num;
        num=n-'0';
    return num;
    }
    
    int main(){
    ifstream in;
    ofstream out;
    in.open("input.txt");
    out.open("output.txt");

    if(!in.is_open()){
        out<<"Error opening file..."<<endl;
        return -1;
    }

    Node *top1, *top2;
    top1=NULL;
    top2=NULL;

    string num1;
    string num2;
    int num, s1, s2, diff;
    num=0;
    s1=0;
    s2=0;
    diff=0;

    in>>num1>>num2;
    while(num1!="-1"){
    num=0;
    for(int i=0; i<num1.length(); i++){
        num=converttoInt(num1[i]);
        top1=InsertAtTail(top1, num);
    }

    for(int j=0; j<num2.length(); j++){
        num=converttoInt(num2[j]);
        top2=InsertAtTail(top2, num);
    }

    s1=getSize(top1);
    s2=getSize(top2);
    if(s1!=s2){
        if(s1<s2){
            diff=s2-s1;
        while(diff>0){
                top1=InsertAtHead(top1,0);
                diff=diff-1;
        }
        }else{
            if(s1>s2){
                diff=s1-s2;
            while(diff>0){
                top2=InsertAtHead(top2,0); //convert int to char = - '0'
                diff=diff-1;
            }
            }
        }
    }
    printList(top1, out);
    out<<"  +"<<endl;
    printList(top2, out);
    out<<"\n";
    out<<"=======================";
    out<<"\n"<<endl;

    while(top1!=NULL)
        top1=freeList(top1);
    while(top2!=NULL)
        top2=freeList(top2);

    in>>num1>>num2;
    }
    
Last edited on
Hello LHJay,

You could use "stew()" from the "<iomanip>" header file. This works on "cout" or a file stream.

Looking at your code I was wondering if this is a C or C++ program?

Last question, more for clarification, does the linked list store one digit or the whole number?

Andy
It's c++ and each node stores 1 digit.
Eventually I plan to add both linked lists, would the setw() mess with that in any way?
Last edited on
Hello LHJay,

The "setw()" only deals with the input or output stream. A simple way of looking at "setw" is that it creates a block to put something in. If you do not fill the block it pads the unused space with the fill character. Usually this is the space, but can be changed if needed.

Since the linked list is a single digit for each node I am thinking that you would have to build a string before you print the number. Then the "setw" would work. Otherwise you would have to determine how many nodes are in each linked list and manually pad the shorter with spaces.

I have not had the chance to go over the whole program or test it yet, but A thought I have is that you could get the size of "num1" and "num2" pass this to the print function and use those numbers to determine which number needs to be padded with spaces and how many spaces before you print the linked list. This would mean that the "setw" would have no real use because you are taking care of the padding.

The reason I asked about the C or C++ is because of the use of "malloc". This is a C function and you should be using the C++ "new" instead.

I thing that "freeList" is freeing up the memory created. If this is a function that you wrote I have not found it yet. If this comes from a header file that you created I am not sure which header file it comes from.

That is my thoughts for now until I have a change to better understand your code and test it.

Andy
If you're planning to do arithmetic with these linked lists of numbers then I urge you to store them so the least significant digit is first rather than last.

Actually, the best way would be to store the digits in a vector rather than a linked list (but still with the least significant digit in vec[0]).
Hello LHJay,

After I went to be last I finally had my epiphany.

I think you realize that you can not store a "char" in an "int". What you can do is store the decimal value of a space, i.e., (32), in your "int". Them in the print function just check for (32) and print a space instead of the value.

To start with in "main":
1
2
3
4
//in >> num1 >> num2; // <--- Not needed. Done in while loop.

while (in >> num1 >> num2) //(num1 != "-1")
{

This is a better way to read a file of unknown length. When you try to read "num1" and it sets eof" the while loop will fail and continue with what is next after the closing brace of while. Also no need for the extra (-1)s in the input file to mark the end of input.

In the if statements:
1
2
3
4
5
6
7
8
9
10
11
if (sizeNum1 != sizeNum2)
{
	if (sizeNum1 < sizeNum2)
	{
		diff = sizeNum2 - sizeNum1;
		while (diff > 0)
		{
			top1 = InsertAtHead(top1, 32);  // <---Changed.
			diff = diff - 1;
		}
	}

Sorry I got tired of loosing track of what "s1" and "s2" were and changed the names. Not required, but a good suggestion and a good idea. It does make the program easier to follow.

In the "printList" function I added to the while loop:
1
2
3
4
5
6
7
8
9
while (curr != nullptr) 
{
	if (curr->data == 32)
		out << ' ';
	else
		out << curr->data;

	curr = curr->next;
}

This produced the output of:

Given the input of:

80485080443358
4849850549686868696

The output is:

     80485080443358  +
4849850549686868696
=======================

And if you reverse the numbers:

4849850549686868696  +
     80485080443358
=======================


I did make one change in "createNode":
1
2
//newNode = (Node*)malloc(sizeof(Node));
newNode = new(Node);  // <--- Changed. 

You should use "new" not "malloc".

Another small change I made in "main":
1
2
3
4
5
6
7
8
9
10
11
12
13
Node *top1{ nullptr }, *top2{ nullptr };

//top1 = nullptr;
//top2 = nullptr;

std::string num1;
std::string num2;
int num{}, sizeNum1{}, sizeNum2{}, diff{};

//num = 0;
//s1 = 0;
//s2 = 0;
//diff = 0; 

If you initialize your variables when they are defined you will not need all those extra lines of code to set the variables to (0) zero.

The end of the while loop and z"main" should look like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
		//while (top1 != nullptr)
		//	top1 = freeList(top1);

		//while (top2 != nullptr)
		//	top2 = freeList(top2);

		//in >> num1 >> num2; // <--- Not needed now. Done in While condition.
	}  //  End while loop

	//while (top1 != nullptr)
	//	top1 = freeList(top1);

	//while (top2 != nullptr)
	//	top2 = freeList(top2);
}

Since I do not know anything about the "freeList" or have access to it I had to comment this bit of code for now. I am only guessing that the while loops will work outside of the first while loop. Having used "new" to create dynamic memory you need "delete" to free that memory when finished with it. DO NOT count on the program ending and the memory being freed. The same concept holds true for using "malloc".

Hope that helps,

Andy
@dhayden, I plan to reverse the lists.
@Handy Andy, the 32 to ' ' method worked, thanks a lot
Topic archived. No new replies allowed.