Question: Take a look at the orderhw.c code and draw what happens to the linked list containing the dummy and nodes for 1 through 5 as
Take a look at the orderhw.c code and draw what happens to the linked list containing the dummy and nodes for 1 through 5 as it is being printed out. ls->Dummy->1->2->3->4->5NULL
orderhw.c code below (THIS IS FOR C++):
#include
typedef struct ENTRY { char name[81]; }ENTRY;
typedef struct LISTREC { struct ENTRY info; struct LISTREC *link; }LISTREC;
FILE *stream; ENTRY part;
void listdelete(ENTRY part,LISTREC *liststart); LISTREC *buildlist(); void prntlist(LISTREC *); void listinsert(ENTRY,LISTREC *);
void main() { LISTREC *liststart; stream=fopen("data.bin","r+b");
liststart = buildlist(); printf("Before insertion linked list: "); prntlist(liststart); printf("Enter name to insert:"); scanf(" %[^ ]",part.name); printf("part.name = %s ",part.name); listinsert(part,liststart); prntlist(liststart); printf("Before deletion linked list: "); prntlist(liststart); printf("Enter name to delete:"); scanf(" %[^ ]",part.name); printf("part.name = %s ",part.name); listdelete(part,liststart); prntlist(liststart);
}
void listinsert(ENTRY newentry,LISTREC *liststart) { LISTREC *last,*next; next = liststart;
while ((strcmp(newentry.name,next->info.name)> 0) && (next->link != NULL)) { last = next; next = next->link; }
if (strcmp(newentry.name,next->info.name) == 0) next->info = newentry; else if (strcmp(newentry.name,next->info.name) < 0) { last->link = (LISTREC*)malloc(sizeof(LISTREC)); last->link->info=newentry; last->link->link = next; } else { next->link = (LISTREC*)malloc(sizeof(LISTREC)); next->link->info = newentry; next->link->link = NULL; } printf(" After insertion, linked list: "); prntlist(liststart); }
LISTREC *buildlist() { LISTREC *liststart = NULL; int n;
liststart=(LISTREC *)malloc(sizeof(LISTREC)); liststart->info.name[0]='\0'; liststart->link=NULL;
for (;;) { n= fread(&part,sizeof(part),1,stream); if (n==0) break;
listinsert(part,liststart);
}
return(liststart);
}
void prntlist(LISTREC *liststart) {
while (liststart != NULL) { printf("%s ",liststart->info.name); liststart = liststart->link; } }
void listdelete(ENTRY part,LISTREC *liststart) { LISTREC *last,*next; next = liststart;
while ((strcmp(part.name,next->info.name)!=0) && (next->link != NULL)) { last = next; next = next->link; }
if (strcmp(part.name,next->info.name) == 0) { last->link=next->link; free(next); } printf(" After deletion, linked list: "); prntlist(liststart); }
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
