Write a program, which reads in a linked list of integers using pointers to stru
ID: 3662207 • Letter: W
Question
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 no more than 10 entries per line.Explanation / Answer
Good Wishes,
Firstly let me share the program.
PROGRAM:
#include <iostream>
#include <fstream>
#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;
}
EXPLAINATION:
Initially I created a structure as described in the question
struct entry
{
int value;
struct entry *nextPtr;
};
Then I declared 2 pointers *firstPtr and *lastPtr of struct entry type and are intially made to point NULL
struct entry *firstPtr=NULL, *lastPtr=NULL,*Prev=NULL;
In the main function I created array of 50 structures of type struct entry
struct entry *t= new entry[50];
Then i created an object fin of ifstream class
ifstream fin;
next I accepted the filename from which values are to be read , from the user and stored it in 'file_name'
char file_name[20];
cout<<"Please enter file name:";
cin>>file_name;
Then i opened the mentioned file
fin.open(file_name);
I made firstPtr to point to first structure in array t
firstPtr=t[0];
Next I used a while loop to read the values from file
while(fin)
Then I placed the read value into 'value' part of structure t for each value of 'i' and then placed NULL inits nextPtr part and placed the new nodes address in lastPtr i.e. previous node
fin>> t[i].value;
t[i].nextPtr= NULL;
lastPtr-->nextPtr=t[i];
If i value reach 51 i.e. >50 then did not assign lastPtr to it, else I assigned
if(i>50)
{
cout<<"NO more insertion can be done";
break;
}
else
lastPtr=t[i];
Then incremented i
i++;
Now I read all the values in the file, next I want to display them>
For that I took pointer 'p' of type struct node and made it to point firstPtr
p=firstPtr;
Then i used a while loop to read the values until lastPtr is reached
while(p!=lastPtr)
I check, if to see that only 10 values are printed in a line, if 10 values are displayed then I dispay the next values in the next line
if(j==10)
{
j=0;
cout<<endl;
}
I displayed the value using
cout<<p-->value;
I moved to the next node by doing
p=p-->nextPtr;
When lastPtr is reached I terminated the while loop and displayed the last node
cout<<lastPtr-->value;
Thus our required output is displayed.
Hope it is clear.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.