Question
Look at the orderhw.c code and draw and explain what happens to the linked list containing the dummy node and nodes for 1 through 5
Look at the orderhw.c code and draw and explain what happens to the
linked list containing the dummy node and nodes for 1 through 5 as
the node containing 3 is deleted from the linked list.
ls->Dummy->1->2->3->4->5NULL
NB. please use pencil and paper to drawing ..
................................
orderhw.c
// USE THIS PROGRAM AND INCLUDE A DELETE FUNCTION. // RUN THEM ALL OFF A MENU LIKE IN STACK.C or final5.c from c1 // MENU ITEMS
// 1 LOAD THE LIST FROM DATA.BIN //buildlist // 2 DISPLAY THE LIST //prntlist // 3 ADD TO THE LIST // listinsert // 4 DELETE FROM THE LIST // listdelete // 5 EXIT
//INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include
typedef struct ENTRY { char name[81]; }ENTRY;
typedef struct LISTREC /* LISTREC is a user-defined type, defined */ { /* before main so it will be global */ struct ENTRY info; /* info nests all fields contained in */ struct LISTREC *link; /* ENTRY (above). */ }LISTREC; /* link is a pointer variable to another */ /* type LISTREC. NOTE: Name of type MUST */ /* be entered at end of typedef */ FILE *stream; ENTRY part;
//prototypes !!!! void listdelete(ENTRY part,LISTREC *liststart); LISTREC *buildlist(); void prntlist(LISTREC *); void listinsert(ENTRY,LISTREC *);
void main() { LISTREC *liststart; /*holds a pointer to beginning of list*/ stream=fopen("data.bin","r+b");
liststart = buildlist(); /*buildlist is a function which will*/ // clrscr(); /*return the beginning of the list*/ 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);
}//main
/*****************FUNCTION LISTINSERT**********************/ 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; } /*end while*/
if (strcmp(newentry.name,next->info.name) == 0) /*if both are same*/ next->info = newentry; /*updates*/ else if (strcmp(newentry.name,next->info.name) < 0) { last->link = (LISTREC*)malloc(sizeof(LISTREC)); /*creates new node*/ last->link->info=newentry; last->link->link = next; } else { next->link = (LISTREC*)malloc(sizeof(LISTREC)); next->link->info = newentry; next->link->link = NULL; } /* end else */ printf(" After insertion, linked list: "); prntlist(liststart); } /* end function listinsert*/
/**************FUNCTION BUILDLIST************************/ LISTREC *buildlist() /* Function buildlist returns type LISTREC.*/ { /* It takes no arguments */ LISTREC *liststart = NULL; /*of type LISTREC */ int n; /*Needed to test for end of file*/
//CREATE A DUMMY NODE !!!!
liststart=(LISTREC *)malloc(sizeof(LISTREC)); liststart->info.name[0]='\0'; liststart->link=NULL;
for (;;) { /*begin for*/ /*for will keep looping until breaks at end of file*/ n= fread(&part,sizeof(part),1,stream); if (n==0) break;
listinsert(part,liststart);
} /*end for*/
return(liststart);
} /*end Function buildlist*/
/****************FUNCTION PRNTLIST********************/ void prntlist(LISTREC *liststart) /*argument is a pointer to type LISTREC*/ { /*begin function*/
while (liststart != NULL) { /*begin while*/ printf("%s ",liststart->info.name); liststart = liststart->link; } /*end while*/ } /* end function prntlist*/
/*****************FUNCTION LISTDELETE**********************/ 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; } /*end while*/
if (strcmp(part.name,next->info.name) == 0) /*if both are same*/ { last->link=next->link; /*updates*/ free(next); } printf(" After deletion, linked list: "); prntlist(liststart); } /* end function listdelete*/
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started