测试题目五,解答如下:
#include <stdio.h> #include <stdint.h> typedef struct _QueueZoneClass { void *content; struct _QueueZoneClass *next; }QueueZoneClass; typedef struct _QueueIndexClass { QueueZoneClass *head; uint8_t entries; }QueueIndexClass; QueueZoneClass QZone[10] = {0, }; QueueIndexClass *pIndex; void QueueInit(QueueIndexClass *pIn, QueueZoneClass *pQZ) { pIn->head = pQZ; pIn->entries = 0; } int8_t QueueInsert(QueueIndexClass *pIn, QueueZoneClass *pQZ, uint8_t pos) { int8_t err = 0; uint8_t i; QueueZoneClass *p = pIn->head; if(pos > pIn->entries) { pos = pIn->entries; } for(i = 0; i < pos; i++) { p = p->next; } pQZ->next = p->next; p->next = pQZ; pIn->entries += 1; return (err); } char *str[] = { "John", "Peter", "Machel", }; int main() { int8_t err = 0; uint8_t i; QueueZoneClass *p; QZone[1].content = (char *)str[0]; QZone[2].content = (char *)str[2]; QZone[4].content = (char *)str[1]; QueueInit(pIndex, &QZone[0]); err = QueueInsert(pIndex, &QZone[2], 0); err = QueueInsert(pIndex, &QZone[1], 1); err = QueueInsert(pIndex, &QZone[4], 2); p = pIndex->head->next; for(i = 0; i < pIndex->entries; i++) { printf("%s\n", (char *)(p->content)); p = p->next; } while(1) { ; } }
运行结果如下: