Good day,please i have a problem with my program, not only am i not getting the
ID: 3689801 • Letter: G
Question
Good day,please i have a problem with my program, not only am i not getting the right answers but am also receiving error messages.
This is the program;
#include <stdio.h>
#include <stdlib.h>
#define NAME_LENGTH 10
struct studentRecord
{
char name[NAME_LENGTH + 1];
struct studentRecord * nextPtr;
};
typedef struct studentRecord Student;
typedef struct studentRecord * studPtr;
//Function prototype
void insert(studPtr *sPtr, char *newName);
int delete (studPtr *sPtr, Student myStud);
void printStudent(studPtr myPtr);
void printList(studPtr myPtr);
int main(void)
{
char *studentname[] = { "Abbie", "Oakley", "Sylvia", "Uwe", "Ken", "Aaron", "Fabien" };
studPtr startPtr = NULL;
insert(&startPtr, studentname);
printList(startPtr);
system("pause");
}
void insert(studPtr *sPtr, char *newName)
{
int i;
studPtr newPtr = malloc(sizeof(studPtr));
if (sPtr != NULL)
{
for (i = 0; newName[i] != ''; i++)
{
strcpy(newPtr->name, newName);
newPtr->nextPtr = NULL;
studPtr previousPtr = NULL;
studPtr myPtr = *sPtr;
while (myPtr != NULL && strcmp(newPtr->name, newName) > myPtr->name[i])
{
previousPtr = myPtr;
myPtr = myPtr->nextPtr;
}
if (previousPtr == NULL)
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else
{
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = myPtr;
}
}
}
else
{
printf("Not inserted. No memory available. ");
}
}
void printList(studPtr myPtr)
{
if (myPtr = NULL)
{
puts("List is empty. ");
}
else
{
puts("The list is: ");
while (myPtr != NULL)
{
printf("%s --> ", myPtr->name);
myPtr = myPtr->nextPtr;
}
puts("NULL ");
}
}
Explanation / Answer
Corrected the errors :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define NAME_LENGTH 10
using namespace std;
struct studentRecord
{
char name[NAME_LENGTH + 1];
struct studentRecord * nextPtr;
};
typedef struct studentRecord Student;
typedef struct studentRecord * studPtr;
//Function prototype
void insert(studPtr *sPtr, char **newName);
int deleteStudent(studPtr *sPtr, Student myStud);
void printStudent(studPtr myPtr);
void printList(studPtr myPtr);
int main(void)
{
char *studentname[] = { "Abbie", "Oakley", "Sylvia", "Uwe", "Ken", "Aaron", "Fabien" };
studPtr startPtr = NULL;
insert(&startPtr, studentname);
printList(startPtr);
system("pause");
}
void insert(studPtr *sPtr, char **newName)
{
int i;
studPtr newPtr = (studPtr)malloc(sizeof(studPtr));
if (sPtr != NULL)
{
for (i = 0; newName[i] != ''; i++)
{
strcpy(newPtr->name, newName[i]);
newPtr->nextPtr = NULL;
studPtr previousPtr = NULL;
studPtr myPtr = *sPtr;
while (myPtr != NULL && strcmp(newPtr->name, newName[i]) > myPtr->name[i])
{
previousPtr = myPtr;
myPtr = myPtr->nextPtr;
}
if (previousPtr == NULL)
{
newPtr->nextPtr = *sPtr;
*sPtr = newPtr;
}
else
{
previousPtr->nextPtr = newPtr;
newPtr->nextPtr = myPtr;
}
}
}
else
{
printf("Not inserted. No memory available. ");
}
}
void printList(studPtr myPtr)
{
if (myPtr = NULL)
{
puts("List is empty. ");
}
else
{
puts("The list is: ");
while (myPtr != NULL)
{
printf("%s --> ", myPtr->name);
myPtr = myPtr->nextPtr;
}
puts("NULL ");
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.