| Linked List Implementation in ASIC - ANSI C |
|
|
|
| Written by SVTechie | ||||||||
Page 2 of 6
Following functions can be/should be implemented for each Linked List. Search
The simplest operation on a linked list is a search. This function looks for the specified number and tells if that number is in database. A pointer is initialized to the list root. Then on each iteration of the loop the pointer is set to the link field in the node currently being examined. When the link field NULL, all elements in the list have been examined and the loop terminates. I this case, this function will return pointer to the node, which holds the specified value. If the value is not in Linked List, it will return NULL. node *search (node* start, int value) { node* cur; for (cur = start; cur != NULL; cur = cur->next) { if (cur->number == value) return cur; } return NULL; } Delete Node
The next operation on a linked list is element deletion. The function returns updated start pointer. If the first element is deleted from the linked list, the root must be modified to point to the second element, or be set to NULL if there is no second element. The for loop that searches for the matching record is identical to the code in search, except that an additional pointer, prevptr, is being kept to the list. This ponter points to the list element before ptr. Node can be deleted by connecting prevptr to the next field of the element pointed to by current ptr. This has the result of removing the element from the list. The node pointed by node2bfree is not being used by any section of the program so it should be returned to operating system.
Code section looks like following
node* prevptr = NULL;
node *cur; for (node* cur = start; cur != NULL; cur = cur->next) { if (cur->number == value) { node2bfree = cur; break; }
prevptr = cur; } if (node2bfree == NULL) { // Do Nothing
} else if (prevptr == NULL) { } else { prevptr->next = node2bfree->next; free (node2bfree);
} return start; }
|
||||||||
| Last Updated ( Saturday, 06 May 2006 ) | ||||||||
| < Prev | Next > |
|---|


Articles 

