Link-List | DreamsCity 2000 |
![]() Return to Download |
The Link-list service routine can be divided into 7 main category. There are Initialisation, Insertion of object, Deletion of object, Movement of node, Information retrieval, Searching activities and Displaying of object. 1. InitialisationBefore a link-list can be implemented, it need to be declared and initialised. 1.1 Declaration of link-listPrototype : Rlist *<list_name> 1.2 Initialisation of link-listPrototype: Rlist * rl_new ( void ( *ptrTofn ) ( void *obj ), ...
) In the above example, del_print is the function that print out the content of the del_object. The function, del_match_ids, is use for comparsion purposes when an enquiry is done on the list. The parameter for the rl_new need to terminate with '0'. 2. Insertion of objectAn object can be inserted into the existing list of object. There are 4 ways to insert the object into the list. 2.1 Put the object at the end of the list.Prototype: void rl_put ( Rlist *rl, void *obj ) 2.2 Put the object at the front of the listPrototype: void rl_putfront ( Rlist *rl, void *obj ) 2.3 Put the object before the current positionPrototype: void rl_put_before ( Rlist *rl, void *obj ) 2.4 Put the object after the current postionPrototype: void rl_put_after ( Rlist *rl, void *obj ) 3. Deletion of objectAn object in the list can be removed, or all the object in the list can be deleted. The entire Rlist structure can also be deleted. 3.1 Removing a node from the listPrototype: void rl_remove ( Rlist *rl ) The current node will be deleted. 3.2 Emptying the entire listPrototype: void rl_dump ( Rlist *rl ) All the node in the list will be deleted, only the dummy header node is left. 3.3 Destroying the entire listPrototype: void rl_delete ( Rlist *rl ) All the node in the list will be deleted first and follow by the Rlist pointer. 4. Movement of NodesEvery time when a node is accessed, the current pointer will postion itself to that particular node. The current pointer can be shifted with the following commands. 4.1 Set current pointer to the beginning of listPrototype: void rl_reset ( Rlist *rl ) 4.2 Set current pointer to the next nodePrototype: void rl_next ( Rlist *rl ) 4.3 Set current pointer to the previous nodePrototype: void rl_prev ( Rlist *rl ) 5. Information RetrievalObject in the list can be retrieved. It can be retrieved from the front or from the rear. 5.1 Dequeue the objectPrototype: void *rl_get ( Rlist *rl ) The first object in the list will be removed and store in the variable del_obj. Note: this is a destructive retrieval. The object being retrieved will no longer be in the list. 5.2 Retrieve the first objectPrototype: void *rl_front ( Rlist *rl ) The first object in the list wll be copy to the variable del_obj. At the same time, the first object will still be in the list, it will not be removed. 5.3 Retrieve the last objectPrototype: void *rl_rear ( Rlist *rl ) The last object in the list will be copy to the variable del_obj. This last object will still be in the list. 5.4 Retrieve the current objectPrototype: void *rl_retrieve ( Rlist *rl ) The object pointer by the current pointer will be copy to the variable del_obj. This is to retrieve out the current object. 5.5 Retrieve the next objectPrototype: void *rl_retrieve_next ( Rlist *rl ) The object next to the current pointer will be retrieved. After retrieving the object, the current pointer will shift to the object being retrieved. 5.6 Retrieve the previous objectPrototype: void *rl_retrieve_prev ( Rlist *rl ) The object previous to the current pointer will be retrieved. After retrieving the object, the current pointer will shift to the object being retrieved. 5.7 Size of the listPrototype: int rl_size ( Rlist *rl ) This function will return the number of object (nodes) in the list. 5.8 Check if list is emptyPrototype: int rl_empty ( Rlist *rl ) This function will return 0 if there are more than 1 object in the list, return 1 if there is no object in the list. 5.9 Check if list is fullPrototype: int rl_full ( Rlist *rl ) This funciton will return 1 if the list is full, return 0 if the list is not full. 5.10 Check is the current pointer at the front of the listPrototype: int rl_isHead ( Rlist *rl ) This function will return 1 if the current pointer points to the front of the list. 5.11 Check is the current pointer at the rear of the listPrototype: int rl_isTail ( Rlist *rl ) This function will return 1 if the current pointer points to the rear of the list. 6. Searching ActivitiesEnquiry can be done through the list to look for a particular object. To use the following function, the comparsion function need to be pass in as a parameter when initialising the list. A list of comparsion functions ca be passed in when initialising the list. The first comparsion function will have index known as index 0 and subsequent function will have index increase by 1. 6.1 Query of object existsPrototype: int rl_find ( Rlist *rl, void *key, int idx ) In the above example, it will search through the del_list. The key word to look for is "00386386" and it will use the first comparsion function passed in during the initialisation of del_list. 6.2 Query forwardPrototype: int rl_findnext ( Rlist *rl, void *key, int idx ) It will look for the forward next occurrence of the object with the key "00386386". 6.3 Query backwardPrototype: int rl_findprev ( Rlist *rl, void *key, int idx ) It will look for the previous occurrence of the object with the key "00386386". 7. Displaying of objectThe content of the object can be displayed. The field to be displayed is specified in the print function that passed in as a parameter when initialising the list. 7.1 Display the current objectPrototype: void rl_show_curr ( Rlist *rl ) 7.2 Display the front objectPrototype: void rl_show_front ( Rlist *rl ) 7.3 Display the rear objectPrototype: void rl_show_rear ( Rlist *rl ) 7.4 Display all the object in the FIFO orderPrototype: void rl_display_fifo ( Rlist *rl ) |
Write to: richard@dreamscity.net Download source code |
|