310 likes | 458 Views
DBS Program Presentation. Chen Sing Tiong (KL003676) Cheng Chin Tat (KL003832) Oon Thiam Teck (KL003833) Tan Wee Khoon (KL003844). Credits. Chen Sing Tiong Slides: 14-17 Cheng Chin Tat Slides: 7-13 Oon Thiam Teck Slides: 18-24 Tan Wee Khoon Slides: 4-6, 25-31
E N D
DBS Program Presentation Chen Sing Tiong (KL003676) Cheng Chin Tat (KL003832) Oon Thiam Teck (KL003833) Tan Wee Khoon (KL003844)
Credits Chen Sing Tiong Slides: 14-17 Cheng Chin Tat Slides: 7-13 Oon Thiam Teck Slides: 18-24 Tan Wee Khoon Slides: 4-6, 25-31 Note: Some graphics of the slides are animated so please be patient.
Structure of NODE and LIST typedef struct node { int data; struct node *link; } NODE; typedef struct{ int count; NODE *pos; NODE *head; NODE *rear; } LIST;
Before createList() Function … LIST *mylist; int i, res; int value; mylist = createList();
During createList() Function LIST *createList(){ /* Local Declarations */ LIST *list; /* Statements */ list = (LIST *) malloc (sizeof(LIST)); if (list){ list->head = NULL; list->pos = NULL; list->rear = NULL; list->count = 0; } /* if */ return list; } /* createList */ This graphic is animated.
After createList() Function • mylist holds the address of the returned list which had the address of 0x00430170
Add Function intaddNode(LIST *pList,intnewdata ) /* Local Declarations */ intfound; intsuccess; NODE *pPre; NODE *pLoc; /* Statements */ found = _search (pList, &pPre, &pLoc, newdata); if(found == 1) /* Duplicate keys not allowed */ return(+1); success = _insert (pList, pPre, newdata); if(!success) /* Overflow */ return(-1); return(0);
Continue… static int _insert (LIST *pList, NODE *pPre, int newdata) { /* Local Declarations */ NODE *pNew; /* Statements */ if (!(pNew = (NODE *) malloc(sizeof(NODE)))) return 0; pNew->data = newdata; pNew->link = NULL; if (pPre == NULL) { /* Adding before first node or to empty list. */ pNew->link = pList->head; pList->head = pNew; if (pList->count == 0) /* Adding to empty list. Set rear */ pList->rear = pNew; } else { /* Adding in middle or at end */ pNew->link = pPre->link; pPre->link = pNew; /* Now check for add at end of list */ if (pNew->link == NULL) pList->rear = pNew; } /* if else */ (pList->count)++; return 1; } /* _insert */
Assign new data to new node pNew->data = newdata; pNew->link = NULL; if (pPre == NULL) { /* Adding before first node or to empty list. */ pNew->link = pList->head; pList->head = pNew; if (pList->count == 0) pList->rear = pNew; This graphic is animated.
Add before the first node or empty list pNew->data = newdata; pNew->link = NULL; if (pPre == NULL) { /* Adding before first node or to empty list. */ pNew->link = pList->head; pList->head = pNew; …… (pList->count)++; This graphic is animated.
Add to empty node if (pPre == NULL) { pNew->link = pList->head; pList->head = pNew; if (pList->count == 0) /* Adding to empty list. Set rear */ pList->rear = pNew; …… (pList->count)++; This graphic is animated.
Add in the middle } else { /* Adding in middle or at end */ pNew->link = pPre->link; pPre->link = pNew; /* Now check for add at end of list */ if (pNew->link == NULL) pList->rear = pNew; This graphic is animated.
Add at the end } else { /* Adding in middle or at end */ pNew->link = pPre->link; pPre->link = pNew; /* Now check for add at end of list */ if (pNew->link == NULL) pList->rear = pNew; This graphic is animated.
Remove Node int removeNode(LIST *pList, int d_data) { int found; NODE *pPre; NODE *pLoc; found = _search (pList, &pPre, &pLoc, d_data); if (found) _delete (pList, pPre, pLoc); return found; } This graphic is animated.
Delete - Condition 1 First Node void _delete (LIST *pList, NODE *pPre, NODE *pLoc) { if (pPre == NULL) pList->head = pLoc->link; else pPre->link = pLoc->link; if (pLoc->link == NULL) pList->rear = pPre; (pList->count)--; free (pLoc); return; } This graphic is animated.
Delete - Condition 2 Second Node void _delete (LIST *pList, NODE *pPre, NODE *pLoc) { if (pPre == NULL) pList->head = pLoc->link; else pPre->link = pLoc->link; if (pLoc->link == NULL) pList->rear = pPre; (pList->count)--; free (pLoc); return; } This graphic is animated.
Delete - Condition 3 Last Node void _delete (LIST *pList, NODE *pPre, NODE *pLoc) { if (pPre == NULL) pList->head = pLoc->link; else pPre->link = pLoc->link; if (pLoc->link == NULL) pList->rear = pPre; (pList->count)--; free (pLoc); return; } This graphic is animated.
Retrieve Node Function static intretrieveNode (LIST *pList,intkey) { /* Local Declarations */ NODE *pPre; NODE *pLoc; /* Statements */ return_search (pList, &pPre, &pLoc, key); }/* retrieveNode */ This graphic is animated.
Search Function int_search(LIST *pList, NODE **pPre, NODE **pLoc,ints_data) {/* Statements */ *pPre = NULL; *pLoc = pList->head; if(pList->count == 0) return0; /* Test for argument > last node in list */ if( s_data > pList->rear->data) { *pPre = pList->rear; *pLoc = NULL; return0; }/* if */ while( s_data > (*pLoc)->data ){ /* Have not found search argument location */ *pPre = *pLoc; *pLoc = (*pLoc)->link; }/* while */ if(s_data == (*pLoc)->data) /* argument found--success */ return1; else/* i.e., s_data < (*pLoc)->data */ return0; }/* _search */
Local Declaration *pPre = NULL; *pLoc = pList->head; /* this 2 lines of code initialize the *pPre and *pLoc */ This graphic is animated.
Condition 1: empty list *pPre = NULL; *pLoc = pList->head; if(pList->count == 0) return 0; /* when the list is empty, both *pPre and *pLoc will assign to null and zero(0 )is return to indicate not found */ This graphic is animated.
Condition 2: Argument > Last Node /* Test for argument > last node in list */ if( s_data > pList->rear->data) { *pPre = pList->rear; *pLoc = NULL; return0; }/* if */ /* when the last node data is lesser than the s_data, it indicate node not found and the process of looping to find node is skip */ This graphic is animated.
Condition 3: node not found while( s_data > (*pLoc)->data ){ /* Have not found search argument location */ *pPre = *pLoc; *pLoc = (*pLoc)->link; }/* while */ if(s_data == (*pLoc)->data) /* argument found--success */ return1; else /* i.e., s_data < (*pLoc)->data */ return0; This graphic is animated.
Condition 4: node found while( s_data > (*pLoc)->data ){ /* Have not found search argument location */ *pPre = *pLoc; *pLoc = (*pLoc)->link; }/* while */ if(s_data == (*pLoc)->data) /* argument found--success */ return1; else /* i.e., s_data < (*pLoc)->data */ return0; This graphic is animated.
intemptyList (LIST *pList) { /* Statements */ return(pList->count == 0); }/* emptyList */ return (pList->count == 0); Code Explanation If pList->countis 0, meaning return (0 == 0); which evaluates to true then the function will return 1(true). True in the following code means list is empty & vice versa. If pList->count is more than 0, meaning return (pList->count != 0); which evaluates to false then the function will return 0(false). emptyList() Function
intlistCount(LIST *pList) { /* Statements */ returnpList->count; }/* listCount */ This function returned the number of nodes in the linked list. Do you know that ? Whenever, a node is added/removed the pList->count is incremented/decremented respectively. listCount() Function
int traverse (LIST *pList, int fromWhere, int *t_data){ /* Local Declarations */ int success; /* Statements */ if (fromWhere == 0) { /*Start from first node */ if (pList->count == 0) success = 0; else { pList->pos = pList->head; *t_data = pList->pos->data; success = 1; } /* if else */ } else {/* Start from current position */ if (pList->pos->link == NULL) success = 0; else { pList->pos = pList->pos->link; *t_data = pList->pos->data; success = 1; } /* if else */ } /* if fromwhere else */ return success; } /* traverse */ Logic Explanation If the success flag is equal to 1, meaning there is still node(s) in the list that have not been traversed to be printed & vice versa (success flag is equal to 0). Note: Let’s assume there are 3 nodes in the list: 2, 3 & 8 and the pList->pos pointer is pointing at the first node. traverse() Function
… if (fromWhere == 0) { /*Start from first node */ if (pList->count == 0) success = 0; Let’s recall that we assumed we have 3 nodes: 2, 3 & 8. Hence, these conditions will not be satisfied. It will only be satisfied when there’s no node in the list. traverse() Function 1st Scenario
traverse() Function 2nd Scenario … if (fromWhere == 0) { /*Start from first node */ … else { pList->pos = pList->head; *t_data = pList->pos->data; success = 1; } /* if else */
traverse() Function 3rd Scenario … else { /* Start from current position */ if (pList->pos->link == NULL) success = 0; Let’s recall that we had traversed 1 node: 2. Nodes: 3 & 8 remaining. Hence, this condition will not be satisfied. It will only be satisfied as illustrated in the picture to the left.
traverse() Function 4th Scenario … else { /* Start from current position */ … else { pList->pos = pList->pos->link; *t_data = pList->pos->data; success = 1; This graphics is animated.