Please help me with the following assignment problem,please.Program should be in
ID: 3681044 • Letter: P
Question
Please help me with the following assignment problem,please.Program should be in C++. Your help will be greatly appreciated.
Write a program to accomplish each of the following. Assume that all the manipulations occur in main.(therefore, no addresses of pointer variables are needed), and assume the following definitions:
struct gradeNode {
char lastName[20];
double grade;
struct gradeNode *nextPtr;
};
typedef struct gradeNode GradeNode;
typedef GradeNode *GradeNodePtr;
a) Create a pointer to the start of the list called startPtr. The list is empty.
b) Create a new node of type GradeNode that is pointed to by pointer newPtr of type Grade-NodePtr. Assign the string "Jones" to member lastName and the value 91.5 to membergrade (use strcpy). Provide any necessary declarations and statements.
c) Assume that the list pointed to by startPtr currently consists of 2 nodes—one containing"Jones"and one containing "Smith". The nodes are in alphabetical order. Providethe statements necessary to insert in order nodes containing the following data forlastName and grade:
"Adams" 85.0
"Thompson" 73.5
"Pritchard" 66.5
Use pointers previousPtr, currentPtr and newPtr to perform the insertions. Write a comment statingwhatpreviousPtr and currentPtr point to before each insertion. Assume that newPtr alwayspoints to the new node, and that the new node has already been assigned the data.
d) Write a while loop that prints the data in each node of the list. Use pointer currentPtrto move along the list.
Explanation / Answer
#include < string.h >
#include < stdlib.h >
#include < stdio.h >
int main ( )
{
struct gradeNode
{
char lastName[20] ;
double grade ;
struct gradeNode *nextPtr ;
} ;
typedef struct gradeNode GradeNode ;
typedef GradeNode *GradeNodePtr ;
GradeNodePtr beginPointr = NULL ;
GradeNodePtr currentPointr = NULL ;
GradeNodePtr lastPointr = NULL ;
GradeNodePtr latestPointr = NULL ;
GradeNodePtr virtulPointr = NULL ;
// Node-1
currentPointr = malloc ( sizeof ( GradeNode ) ) ;
strcpy ( currentPointr -> lastName , " Jones " ) ;
currentPointr -> grade = 91.5 ;
currentPointr -> nextPtr = NULL ;
beginPointr = currentPointr ;
// Node-2
currentPointr = malloc ( sizeof ( GradeNode ) ) ;
strcpy ( currentPointr -> lastName , " Smith " ) ;
currentPointr -> grade = 0.00 ;
beginPointr -> nextPtr = currentPointr ;
// Node-3
currentPointr = malloc ( sizeof ( GradeNode ) ) ;
strcpy ( currentPointr -> lastName , " Adams " ) ;
currentPointr -> grade = 85.0 ;
lastPointr = NULL ;
latestPointr = beginPointr ;
currentPointr -> nextPtr = latestPointr ;
beginPointr = currentPointr ;
// Node-4
currentPointr = malloc ( sizeof ( GradeNode ) ) ;
strcpy ( currentPointr -> lastName , " Thompson " ) ;
currentPointr -> grade = 73.5 ;
lastPointr = ( beginPointr -> nextPtr ) -> nextPtr ;
latestPointr = NULL ;
lastPointr -> nextPtr = currentPointr ;
currentPointr -> nextPtr = latestPointr ;
// Node-5
currentPointr = malloc ( sizeof ( GradeNode ) ) ;
strcpy ( currentPointr -> lastName , " Pritchard " ) ;
currentPointr -> grade = 66.5 ;
lastPointr = beginPointr -> nextPtr ;
latestPointr = ( beginPointr -> nextPtr ) -> nextPtr ;
lastPointr -> nextPtr = currentPointr ;
currentPointr -> nextPtr = latestPointr ;
// Printing the nodes.
latestPointr = beginPointr ;
while(latestPointr!=NULL)
{
printf ( " Lastname = %s Grade = %6.2f " , latestPointr -> lastName , latestPointr -> grade ) ;
latestPointr = latestPointr -> nextPtr ;
}
// Deleting the nodes.
latestPointr = beginPointr ;
while ( latestPointr != NULL )
{
virtulPointr = latestPointr ;
latestPointr = latestPointr -> nextPtr ;
free ( virtulPointr ) ;
}
// beginPointr = NULL ;
// Check out the list if deleted or not .
printf ( " The list was deleted ? " ) ;
latestPointr = beginPointr ;
while ( latestPointr != NULL )
{
printf ( " Lastname = %s Grade = %6.2f " , latestPointr -> lastName , latestPointr -> grade ) ;
latestPointr = latestPointr -> nextPtr ;
}
return 0 ;
}