//------------------- list2.cpp ------------------------- //------------ source file for unit list ---------------- #include #include #include #include "list2.h" //--------------------------- Iplementstion ----------------------------- //--------------------------------------------------------------- // returns an initialized list list_type list_init (void) { list_type L; L.anchor = (pos_type) malloc (sizeof(struct link_type)); L.anchor->next = NULL; return L; } //--------------------------------------------------------------- // return place: anchor // assumption: L is initialized pos_type list_anchor (list_type L) { return L.anchor; } //--------------------------------------------------------------- // return place: list_end // assumption: L is initialized pos_type list_end (list_type L) { return NULL; } //--------------------------------------------------------------- // return place: next link in list // assumption: L is initialized pos_type list_next (list_type L, pos_type pos) { return (pos->next); } //--------------------------------------------------------------- // return place: previous link in list // assumption: L is initialized pos_type list_prev (list_type L, pos_type pos) { pos_type p; p = list_anchor(L); while (list_next (L,p) != pos) p = list_next (L,p); return p; } //--------------------------------------------------------------- // inserting x after pos in L // assumption: L is initialized void list_insert (list_type &L, pos_type pos, list_info_type x) { pos_type temp; temp = (pos_type) malloc (sizeof (struct link_type)); temp->info = x; temp->next = pos->next; pos->next = temp; } //--------------------------------------------------------------- // delets link in place pos in L // after deletation, pos points to next item before deletation // assumption: L is initialized, pos points to place in list // which is neither anchor nor list-end. void list_delete (list_type &L, pos_type &pos) { pos_type temp; temp = list_prev (L,pos); temp->next = pos->next; free (pos); pos = temp->next; } //--------------------------------------------------------------- // returns data from place pos in L // assumption: L is initialized, pos points to place in list // which is neither anchor nor list-end. list_info_type list_retrieve (list_type L, pos_type pos) { return pos->info; } //--------------------------------------------------------------- // return TRUE if list is empty, atherwise return FALSE // assumption: L is initialized int list_is_empty (list_type L) { return L.anchor->next == NULL; } //--------------------------------------------------------------- // updats data in place pos in L // assumption: L is initialized, pos points to place in list // which is neither anchor nor list-end. void list_update (list_type &L, pos_type pos, list_info_type x) { pos->info = x; }