Hello,
I have this code here that I would like to use to construct two linked lists. one for students and one for courses..
I had it working for just students, that is, before I tried to pass the head and end of the different lists to the init function. Now that I have two different lists, notice the head and end pointers for courses and students, I cannot get this to work. hints?
Code:
#include <stdio.h>
#include <alloc.h>
#include <stdlib.h>
#include <conio.h>
#include <ctype.h>
#include <string.h>
struct node * initnode( char *, int );
void printnode( struct node * );
void printlist( struct node * );
struct node {
char name[20];
int id;
struct node *next;
// struct course *myCourse;
};
//struct course {
// char courseID[7];
// struct course *next;
//};
struct node *studentHead = (struct node *) NULL;
struct node *studentEnd = (struct node *) NULL;
struct node *courseHead = (struct node *) NULL;
struct node *courseEnd = (struct node *) NULL;
struct node * initnode( char *name, int id )
{
printf("START INIT NODE......");
struct node *ptr;
ptr = (struct node *) calloc( 1, sizeof(struct node ) );
if( ptr == NULL ) { /* error allocating node? */
printf("INIT NODE ERROR\n");
return (struct node *) NULL; /* then return NULL, else */
}
else { /* allocated node successfully */
strcpy( ptr->name, name ); /* fill in name details */
ptr->id = id; /* copy id details */
printf("INIT NODE COMPLETE\n");
return ptr; /* return pointer to new node */
}
}
void printlist( struct node *ptr )
{
printf("printing list...");
while( ptr != NULL ) /* continue whilst there are nodes left */
{
printnode( ptr ); /* print out the current node */
ptr = ptr->next; /* goto the next node in the list */
}
}
void printnode( struct node *ptr )
{
printf("\nName ->%s\n", ptr->name );
printf("ID ->%d\n", ptr->id );
}
void insertnode( struct node *newNode, struct node *phead, struct node *pend )
{
struct node *temp, *prev; /* similar to deletenode */
if( phead == NULL ) { /* if an empty list, */
printf("set head and end... ");
phead = newNode; /* set 'head' to it */
pend = newNode;
phead->next = NULL; /* set end of list to NULL */
printf("done.\n");
return; /* and finish */
}
temp = phead; /* start at beginning of list */
/* whilst currentname < newname to be inserted then */
while( strcmp( temp->name, newNode->name) < 0 ) {
temp = temp->next; /* goto the next node in list */
if( temp == NULL ) /* dont go past end of list */
break;
}
if( temp == phead ) {
newNode->next = phead; /* link next field to original list */
phead = newNode; /* head adjusted to new node */
}
else { /* okay, so its not the first node, a different approach */
prev = phead; /* start of the list, will cycle to node before temp */
while( prev->next != temp ) {
prev = prev->next;
}
prev->next = newNode; /* insert node between prev and next */
newNode->next = temp;
if( pend == prev ) /* if the new node is inserted at the */
pend = newNode; /* end of the list the adjust 'end' */
}
}
main()
{
char name[20];
char cname[10];
int id;
struct node *sPtr;
struct node *crsPtr;
printf("Enter student name ");
scanf("%s", name );
printf("Enter in id -- ");
scanf("%d", &id );
sPtr = initnode( name, id );
insertnode( sPtr, studentHead, studentEnd);
printf("Enter course name ");
scanf("%s", cname );
sPtr = initnode( cname, -1 );
insertnode( sPtr, courseHead, courseEnd);
// now print
printlist(studentHead);
printlist(courseHead);
system("pause");
}