Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Write a program in C++ which reads in a linked list of integers using pointers t

ID: 638213 • Letter: W

Question

Write a program in C++ 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.

This is what i have so far. Now i'm stuck on what to do. It would be nice if you can add comments in the program so i understand whats going on. Thank You!

#include <iostream>
#include <fstream>
using namespace std;

struct entry{
   int value;
   struct entry *nextPtr;
};
struct entry *getEntry() {

int main(){

   // prompt user for file name and open file
   // read first entry
   struct entry *prevPtr. *firstPtr, *lastPtr;
   struct entry *newptr = getEntry();
   while (not EOF){

   }
}

struct entry *getEntry() {
   static int nextEntry = 0;
   static struct entry array[50];

   if (nextEntry >= 50) return NULL;
   array[nextEntry].value = 0;
   nextEntry++;
   return &array[nextEntry - 1];

}

Explanation / Answer

#include <iostream>
#include <fstream>
using namespace std;

struct entry{
int value;
struct entry *nextPtr;
}array[50];
struct entry *getEntry(int);

int main(){

// prompt user for file name and open file
// read first entry
ifstream file;
int a;
file.open("input.txt");

file>>a;
entry *lastPtr;
entry *firstPtr=getEntry(a); // assigning address of first value to firstPtr
while (!file.eof()){
    file>>a;
entry *lastPtr=getEntry(a); // everytime the value of lastPtr changes until the last value is read;
entry l=*lastPtr;
cout<<" value of last pointer is: "<<l.value;
}
entry f=*firstPtr;
  
cout<<" value of first pointer is: "<<f.value;
  
return 0;
}

entry* getEntry(int a) {
static int nextEntry=0;
if (nextEntry >= 50)
return NULL;
else
{
    array[nextEntry].value = a;
nextEntry++;
return &array[nextEntry - 1];
}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote