typedef struct listnode int value struct listnode next

  • Slides: 18
Download presentation

การสรางรายการวาง typedef struct listnode{ // int value; struct listnode *next; } LISTNODE; create list

การสรางรายการวาง typedef struct listnode{ // int value; struct listnode *next; } LISTNODE; create list node type LISTNODE *numlist=NULL; numlist )=LISTNODE (*malloc)sizeof)LISTNODE((; numlist

ทดสอบวารายการวางหรอไม // return 1 if list is empty, else return 0. int islempty(LISTNODE *head)

ทดสอบวารายการวางหรอไม // return 1 if list is empty, else return 0. int islempty(LISTNODE *head) { return (head == NULL); } numlist = NULL

การเพมสมาชกใหมลงในรายการ Insert first element Insert element on head of list Insert element between list

การเพมสมาชกใหมลงในรายการ Insert first element Insert element on head of list Insert element between list Insert element on end of list

Insert first element numlist = getnode(); // head of list numlist->value = 23; numlist->next

Insert first element numlist = getnode(); // head of list numlist->value = 23; numlist->next = NULL; numlist 23 NULL

Insert element on head of list NEW = getnode(); // head of list NEW->value

Insert element on head of list NEW = getnode(); // head of list NEW->value = 20; NEW->next = numlist; numlist = NEW; NEW 20 numlist 23 NULL

Insert element between list NEW = getnode(); NEW->value = 24; NEW->next = numlist->next ;

Insert element between list NEW = getnode(); NEW->value = 24; NEW->next = numlist->next ; numlist->next = NEW; numlist 23 25 NEW 24 NULL

Insert element on end of list NEW = getnode(); NEW->value = 25; NEW->next =

Insert element on end of list NEW = getnode(); NEW->value = 25; NEW->next = NULL; numlist->next = NEW; numlist 23 NULL NEW 25 NULL

การลบสมาชกออกจากรายการ delete element on head of list delete element between list delete element on

การลบสมาชกออกจากรายการ delete element on head of list delete element between list delete element on end of list

delete element on head of list if( numlist->value == 23 ){ // test value

delete element on head of list if( numlist->value == 23 ){ // test value of head node backup = numlist; // backup head node numlist = numlist->next; // move head to next node free(backup); //free backup node } 23 backup 24 25 NULL

delete element between list ptr = ptr->next; // move ptr to next node if(

delete element between list ptr = ptr->next; // move ptr to next node if( (ptr->next)->value == 24 ){ // test value of next node backup = ptr->next; // backup next node ptr->next = (ptr->next)->next; // point next node to skip 1 node free(backup); // free backup node } 23 24 backup 25 NULL

delete element on end of list while(ptr->next!=NULL){ ptr = ptr->next; // move ptr to

delete element on end of list while(ptr->next!=NULL){ ptr = ptr->next; // move ptr to next node if( (ptr->next)->value == 25 ){ // test value of next node backup = ptr->next; // backup next node ptr->next = (ptr->next)->next; // point next node to skip 1 node free(backup); // free backup node break; ptr backup } 24 23 25 } NULL

การเขาถงสมาชกของรายการ void printlst)LISTNODE *numlist( { LISTNODE *ptr=numlist; while)ptr=!NULL({ printf")%3 d", ptr->value(; ptr =ptr->next; //next

การเขาถงสมาชกของรายการ void printlst)LISTNODE *numlist( { LISTNODE *ptr=numlist; while)ptr=!NULL({ printf")%3 d", ptr->value(; ptr =ptr->next; //next node } } ptr numlist 23 24 25 NULL