/* list.c this or however you wish, is the functions of list.asm */ #include /* extern printf in list.asm */ static char heap[20000]; /* space to store strings, do not reuse or free */ static char *hp = heap; /* pointer to next available heap space */ static int list[1000]; /* space to store list block (2 index+ pointer) */ static int lp=1; /* index to next available list space */ static char *q; /* a variable pointer */ static int i; /* a variable index */ /* +-----------------+ +-----------------+ +-----------------+ L[0]-> | index to next----->| index to next----->| 0 | | 0 |<-----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 pointes to terminated strings. The "index" values could be pointers rather than indices. */ void clear(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 */ } void print(int *L) { i=L[0]; /* index into front of 'list' */ 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 */ } void push_front(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]=(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 */ } void push_back(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]=(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 */ } void pop_front(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 */ } void pop_back(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 list.c file */