I wrote this code but it doesnt compile. Can you fix my errors. #include <iostre
ID: 3662253 • Letter: I
Question
I wrote this code but it doesnt compile. Can you fix my errors.
#include <iostream>
#include <fstream>
using namespace std;
#define NULL 0
struct entry
{
int value;
struct entry *nextPtr;
};
struct entry *firstPtr = NULL, *lastPtr = NULL, *Prev = NULL;
int main()
{
struct entry *t = new entry[50];
int i = 0, j = 0;
ifstream fin;
char file_name[20];
cout << "Please enter file name:";
cin >> file_name;
fin.open(file_name);
firstPtr = t[0];
while (fin)
{
fin >> t[i].value;
t[i].nextPtr = NULL;
lastPtr-->nextPtr = t[i];
if (i>50)
{
cout << "NO more insertion can be done";
break;
}
else
lastPtr = t[i];
i++;
}
p = firstPtr;
while (p != lastPtr)
{
if (j == 10)
{
j = 0;
cout << endl;
}
cout << p-->value;
p = p-->nextPtr;
j++;
}
cout << lastPtr-->value;
return 0;
}
THIS IS what the code I wrote on.
Write a program, which reads in a linked list of integers using pointers to structures instead of array indices. The integers must be read from an input file determined by prompting the user. Allocate an array of 50 C++ structures defined as follows: struct entry { int value; // the integer read in struct entry *nextPtr; // pointer to the next structure You must also declare a struct entry *firstPtr which points to the first entry in the linked list. You must declare a struct *lastPtr which points to the last entry in the linked list. You must NOT allow the lastPtr to point beyond the end of the array. After reading in the integers in the file print out (using pointers, not array indices) the integers read in the order they were read with no more than 10 entries per line.Explanation / Answer
#include <iostream>
#include <fstream>
using namespace std;
struct entry
{
int value;
struct entry *nextPtr;
};
struct entry *firstPtr = NULL, *lastPtr = NULL, *Prev = NULL;
int main()
{
struct entry *t = new entry[50];
int i = 0, j = 0;
ifstream fin;
char file_name[20];
cout << "Please enter file name:";
cin >> file_name;
fin.open(file_name);
firstPtr = &t[0];
while (fin)
{
fin >> t[i].value;
t[i].nextPtr = NULL;
lastPtr->nextPtr = &t[i];
if (i>50)
{
cout << "NO more insertion can be done";
break;
}
else
lastPtr = &t[i];
i++;
}
struct entry *p = firstPtr;
while (p != lastPtr)
{
if (j == 10)
{
j = 0;
cout << endl;
}
cout << p->value;
p = p->nextPtr;
j++;
}
cout << lastPtr->value;
return 0;
}