1 / 31

DBS Program Presentation

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

vui
Download Presentation

DBS Program Presentation

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. DBS Program Presentation Chen Sing Tiong (KL003676) Cheng Chin Tat (KL003832) Oon Thiam Teck (KL003833) Tan Wee Khoon (KL003844)

  2. 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.

  3. 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;

  4. Before createList() Function … LIST *mylist; int i, res; int value; mylist = createList();

  5. 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.

  6. After createList() Function • mylist holds the address of the returned list which had the address of 0x00430170

  7. 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);

  8. 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 */

  9. 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.

  10. 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.

  11. 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.

  12. 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.

  13. 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.

  14. 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.

  15. 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.

  16. 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.

  17. 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.

  18. 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.

  19. 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 */

  20. Local Declaration *pPre = NULL; *pLoc = pList->head; /* this 2 lines of code initialize the *pPre and *pLoc */ This graphic is animated.

  21. 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.

  22. 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.

  23. 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.

  24. 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.

  25. 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

  26. 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

  27. 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

  28. 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

  29. 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 */

  30. 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.

  31. 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.

More Related