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

Im having some difficulty with linked list, im giving this code #define MAXNUM 1

ID: 3657001 • Letter: I

Question

Im having some difficulty with linked list,

im giving this code

#define MAXNUM 10
#define MAXNAME 30

typedef struct enode *Enodeptr;
typedef struct snode *Snodeptr;
typedef struct cnode *Cnodeptr;

struct studentType {
char sid[MAXNUM];
char sname[MAXNAME];
};

struct courseType {
char cno[MAXNUM];
char title[MAXNAME];
int hours;
};

struct snode {
struct studentType student;
Enodeptr firstE;
Snodeptr next,prior;
};

struct cnode {
struct courseType course;
Enodeptr firstE;
Cnodeptr next,prior;
};

struct enode {
Enodeptr sNext, cNext;
Snodeptr sOwner;
Cnodeptr cOwner;
char grade;
};

//and what we have to do is create a student.c file that does this

Snodeptr sAlloc(void);
/* Allocates storage for a snode and returns pointer to the structure */

void makenullStudents(Snodeptr *students);
/* Creates an empty list of students and returns pointer to dummy node
in the variable students */

int insertStudents(struct studentType x, Snodeptr students);
/* inserts the student structure x in the student list; returns 0
if successful and 1 if failed */

int deleteStudents(struct studentType x, Snodeptr students, Cnodeptr courses);
/* deletes the student structure x in the student list; returns 0
if successful and 1 if failed */

int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s);
/* returns 0 if student structure x is present in students list
and 1 otherwise. It also returns a pointer (in s) to the snode where it
found x; If x is not found it should return a pointer to the snode
where x should be inserted */

void printStudents(Snodeptr students);
/* prints the list of students (sid and names) in pretty format with
-more- like option */
/***********************************8

what i have come up with is

*******************************

#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "types.h"
#include "students.h"
#include "enrolls.h"

Snodeptr sAlloc(void) {
return (Snodeptr) malloc(sizeof(struct snode));
}

void makenullStudents(Snodeptr *students) {
(*students)=sAlloc();
}

int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
return 0;
}

int insertStudents(struct studentType x, Snodeptr students) {
Snodeptr p=students;
while(p->next!=NULL)
"students.c" 65L, 1082C 17,1 Top
#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#include "types.h"
#include "students.h"
#include "enrolls.h"

Snodeptr sAlloc(void) {
return (Snodeptr) malloc(sizeof(struct snode));
}

void makenullStudents(Snodeptr *students) {
(*students)=sAlloc();
}

int memberStudents(struct studentType x, Snodeptr students, Snodeptr *s) {
Snodeptr p=students;
Snodeptr p1=*s;
while(strcmp(p->next->student.sid, x.sid))
{
p=p->next;
}

return 0;
}

int insertStudents(struct studentType x, Snodeptr students) {
Snodeptr p=students;
while(p->next!=NULL)
{
p=p->next;
}
Snodeptr new=malloc(sizeof (struct snode));
new->student=x;

p->next=new;
new->prior=p;
new->next=NULL;


return 0;
}

int deleteStudents(struct studentType x, Snodeptr students, Cnodeptr courses) {
Snodeptr p=students;
Snodeptr p1;

while(!strcmp(p->next->student.sid, x.sid))
{
p=p->next;
}
p1=p->next;
p->next=p->next->next;
p->next->prior=p;


return 0;
}

void printStudents(Snodeptr students) {
int count=0;
Snodeptr p=students;
char ch;
while((p->next)!=NULL)
{
printf("sid=%s, name=%s",p->next->student.sid,p->next->student.sname);
p=p->next;
}
}


****************************************888

Im not really good with linked list and want some guideance on my code especially the memeber students


Explanation / Answer

See the example: #include #include #include struct StudentNode { char name[10]; int age; double balance; struct StudentNode *nextPtr; }; typedef struct StudentNode Student; typedef Student *StudentPtr; void insert( Student **headOfList, char *name, int age, double balance ) { StudentPtr newPtr; StudentPtr previousPtr; StudentPtr currentPtr; newPtr = ( Student * ) malloc( sizeof( Student ) ); if ( newPtr != NULL ) { strcpy(newPtr->name, name); newPtr->age = age; newPtr->balance = balance; newPtr->nextPtr = NULL; previousPtr = NULL; currentPtr = *headOfList; if ( currentPtr != NULL ) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if ( previousPtr == NULL ) { newPtr->nextPtr = *headOfList; *headOfList = newPtr; } else { previousPtr->nextPtr = newPtr; newPtr->nextPtr = currentPtr; } } else { printf( "%s not inserted. No memory available. ", name ); } } void deleteNode( Student **headOfList, char *sname ) { StudentPtr previousPtr; StudentPtr currentPtr; StudentPtr tempPtr; if ( sname == (*headOfList)->name ) { tempPtr = (*headOfList); (*headOfList) = (*headOfList)->nextPtr; free( tempPtr ); } else { previousPtr = (*headOfList); currentPtr = (*headOfList)->nextPtr; while (currentPtr->nextPtr != NULL && currentPtr->name != sname ) { previousPtr = currentPtr; currentPtr = currentPtr->nextPtr; } if ( currentPtr->nextPtr != NULL) { tempPtr = currentPtr; previousPtr->nextPtr = currentPtr->nextPtr; previousPtr->nextPtr = currentPtr->nextPtr; free( tempPtr ); } else { tempPtr = currentPtr; previousPtr->nextPtr = NULL; free( tempPtr ); } } } void printAverage( Student *headOfList ) { double sum = 0.0; double number = 0.0; double average = 0.0; if ( headOfList == NULL ) { printf( "List is empty. " ); } else { printf( "The list is: " ); while ( headOfList != NULL ) { printf( "Name : %s Age : %d G.P.A. : %.2lf --> ", headOfList->name, headOfList->age, headOfList->balance ); sum+=headOfList->balance; number+=1; headOfList = headOfList->nextPtr; } printf( "NULL " ); average = sum / number; printf( "Average G.P.A is %.2lf ", average); } } int main(void) { StudentPtr startptr = NULL; insert( &startptr, "Bill", 18, 2.00); insert( &startptr, "Choo", 19, 3.00); insert( &startptr, "John", 22, 1.50); insert( &startptr, "Tim", 18, 4.00); printAverage( startptr ); insert( &startptr, "Lisa", 19, 2.00); insert( &startptr, "Mary", 21, 1.00); printAverage( startptr ); deleteNode( &startptr, "Choo"); printAverage( startptr ); return 0; }