/* listp.c using more pointers for 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 (3 pointers) */ static int *lp= list; /* pointer to next available list space */ static char *q; /* a variable char pointer */ static int *i; /* a variable int pointer */ /* +-----------------+ +-----------------+ +-----------------+ *L-> |pointer to next----->|pointer to next----->| 0 | | 0 |<----pointer to prev |<----pointer 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. */ void clear(int *L) /* initialize front and back pointers to zero */ { *L=0; /* later, will be pointer into "front" of list */ *(L+1)=0; /* later, will be pointer into "back" of list */ } void print(int *L) { i=(int *) *L; /* pointer into "front" of list */ while(i) /* keep looping until i==0, meaning end of list */ { q=(char *) *(i+2); /* get this list items pointer to string */ printf("%s\n",q); /* print this list item's string */ i=(int *) *i; /* move to next list item */ } printf("\n"); /* blank line after items */ } void push_front(int *L, char *s) { if(*L==0) /* list is empty */ { *L=(int)lp; /* front pointer */ *(L+1)=(int)lp; /* back pointer */ *lp=0; /* new next pointer is end */ *(lp+1)=0; /* new prev pointer is end */ } else { i=(int *) *L; /* save pointer to old front */ *L=(int)lp; /* new front pointer */ *lp=(int)i; /* new next pointer is old front */ *(lp+1)=0; /* new prev pointer is end */ /* old front next is unchanged */ *(i+1)=(int)lp; /* old front prev is now new front */ } *(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++; } *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) /* list is empty */ { *L=(int)lp; /* front pointer */ *(L+1)=(int)lp; /* back pointer */ *lp=0; /* new next pointer is end */ *(lp+1)=0; /* new prev pointer is end */ } else { i=(int *) *(L+1); /* save pointer to old back */ *(L+1)=(int)lp; /* new back pointer */ *lp=0; /* new next is end */ *(lp+1)=(int)i; /* new prev pointer is old back */ *i=(int)lp; /* old back next is new back */ /* old back prev is unchanged */ } *(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++; } *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) return; /* list is already empty */ if(*(L+1)==*L) /* only one item on list */ { *L=0; /* delete one item, same as clear */ *(L+1)=0; return; } i=(int *) *L; /* get pointer of old front */ i=(int *) *i; /* get next from old front */ *L=(int)i; /* L is new front */ /* new next is unchanged */ *(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) /* only one item on list */ { *L=0; /* delete one item, same as clear */ *(L+1)=0; return; } i=(int *) *(L+1); /* get pointer to back */ i=(int *) *(i+1); /* get pointer of prev to back */ *(L+1)=(int)i; /* L+1 is new back */ *i=0; /* new next is now end */ /* new prev is unchanged */ } /* end list.c file */