/* Demonstrate use of a linked list */ #include #include /* Define a pointer to tcb and tcb structure */ typedef struct TCB* TCB_Pointer; typedef struct TCB { TCB_Pointer pNext; /* whatever you need in TCB Goes here */ int ThreadIndex; /* For example */ } Tcb; /* define the global variables head and tail */ TCB_Pointer pHead = NULL; TCB_Pointer pTail = NULL; void AddTail( TCB_Pointer pThis) { /* Add pThis to tail of linked list */ if (pTail != NULL) pTail->pNext = pThis; else pHead = pThis; pTail = pThis; pTail->pNext = NULL; } void SearchList( TCB_Pointer pStart) { /* Search the list starting at the specified starting point */ /* If pStart is NULL, start at head */ /* Search circularly (return to head after checking tail) */ TCB_Pointer pThis; if (pHead== NULL) return; // List is empty if (pStart == NULL) pStart = pHead; // Start from head if no starting point pThis = pStart; while(1) { /* Check whatever you want using pThis-> */ printf("Checking node %d\n", pThis->ThreadIndex); pThis = pThis->pNext; if (pThis == NULL) pThis = pHead; /* Reset to head */ if (pThis == pStart) return; /* Checked them all */ } } int main() { TCB_Pointer pThis; int i; for (i = 0; i < 10; i++) { pThis = (TCB_Pointer)malloc(sizeof(*pThis)); pThis->ThreadIndex = i; // Just debug stuff to demo AddTail(pThis); printf("Created new TCB, index %d\n", pThis->ThreadIndex); SearchList(pThis); // And for demonstration, show that this list is searched } // Now demonstate searching list from the head printf("Searchin from head\n"); SearchList(NULL); return(0); }