// list_64.c these are the functions to implement list_64.asm // a doubly linked list of strings #include // extern printf in list.asm static char heap[20000]; // space to store strings, do not reuse or free // could be gigabytes static char *hp = heap; // pointer to next available heap space static long int list[1000]; // space to store list block (2 index and pointer) // could be millions static long int lp=1; // index to next available list space static char *q; // a variable pointer static long int i; // a variable index // +-----------------+ +-----------------+ +-----------------+ // L[0]-> | index to next----->| index to next----->| 0 means last | // | 0 means first |<-----index to prev |<-----index to prev |<-L[1] // | ptr to heap str | | ptr to heap str | | ptr to heap str | // +-----------------+ +-----------------+ +-----------------+ // The pointers to heap strings are character pointers to terminated strings. // The "index" values could be pointers rather than indices. void clear(long int *L) // initialize front and back pointers to zero { L[0]=0; // later, will be index into "front" of list L[1]=0; // later, will be index into "back" of list } // end clear void print(long int *L) { i=L[0]; // index into front of 'list' if(i==0) return; // list is empty while(i) // keep looping until i==0, meaning end of list { q=(char *)list[i+2]; // get this list items pointer to string printf("%s\n",q); // print this list item's string i=list[i]; // move to next list item } printf("\n"); // blank line after items } // end print void push_front(long int *L, char *s) { if(L[0]==0) // list is empty { L[0]=lp; // front index L[1]=lp; // back index list[lp]=0; // new next index is end list[lp+1]=0; // new prev index is end } else { i=L[0]; // save index to old front L[0]=lp; // new front index list[lp]=i; // new next index is old front list[lp+1]=0; // new prev index is end // old front next is unchanged list[i+1]=lp; // old front prev is now new front } list[lp+2]=(long int)hp; // list pointer to string on heap lp=lp+3; // update to next free space in list q=s; while(*q) // copy string s to heap { *hp=*q; // could be written *hp++=*q++; hp++; q++; } *hp=0; // save the final null and update heap pointer hp++; // we should do range checking, but won't } // end push_front void push_back(long int *L, char *s) { if(L[0]==0) // list is empty { L[0]=lp; // front index L[1]=lp; // back index list[lp]=0; // new next index is end list[lp+1]=0; // new prev index is end } else { i=L[1]; // save index to old back L[1]=lp; // new back index list[lp]=0; // new next is end list[lp+1]=i; // new prev index is old back list[i]=lp; // old back next is new back // old back prev is unchanged } list[lp+2]=(long int)hp; // list pointer to string on heap lp=lp+3; // update to next free space in list q=s; while(*q) // copy string s to heap { *hp=*q; // could be written *hp++=*q++; hp++; q++; } *hp=0; // save the final null and update heap pointer hp++; // we should do range checking, but won't } // end push_back void pop_front(long int *L) { if(L[0]==0) return; // list is already empty if(L[1]==L[0]) // only one item on list { L[0]=0; // delete one item, same as clear L[1]=0; return; } i=L[0]; // get index of old front i=list[i]; // get next from old front L[0]=i; // L[0] is new front // new next is unchanged list[i+1]=0; // new prev is now end } // end pop_front void pop_back(long int *L) { if(L[1]==0) return; // list already empty if(L[1]==L[0]) // only one item on list { L[0]=0; // delete one item, same as clear L[1]=0; return; } i=L[1]; // get index of back i=list[i+1]; // get index of prev to back L[1]=i; // L[1] is new back list[i]=0; // new next is now end // new prev is unchanged } // end pop_back // end list_64.c file