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

DBS Program Presentation

  • Upload
    vui

  • View
    40

  • Download
    0

Embed Size (px)

DESCRIPTION

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 - PowerPoint PPT Presentation

Citation preview

Page 1: DBS Program Presentation

1

DBS Program Presentation

Chen Sing Tiong (KL003676)Cheng Chin Tat (KL003832)Oon Thiam Teck (KL003833)Tan Wee Khoon (KL003844)

Page 2: DBS Program Presentation

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.

Page 3: DBS Program Presentation

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;

Page 4: DBS Program Presentation

4

Before createList() Function

LIST *mylist;

int i, res;

int value;

mylist = createList();

Page 5: DBS Program Presentation

This graphic is animated.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 */

Page 6: DBS Program Presentation

6

After createList() Function

mylist holds the address of the returned list which had the address of 0x00430170

Page 7: DBS Program Presentation

7

Add Function

int addNode(LIST *pList, int newdata )/* Local Declarations */int found;int success;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);

Page 8: DBS Program Presentation

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

Page 9: DBS Program Presentation

This graphic is animated.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;

Page 10: DBS Program Presentation

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

Page 11: DBS Program Presentation

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

Page 12: DBS Program Presentation

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;

Page 13: DBS Program Presentation

This graphic is animated.13

Add at the end

} else { /* Adding in middle or atend */ pNew->link = pPre->link; pPre->link = pNew;/* Now check for add atend of list */ if (pNew->link == NULL)

pList->rear = pNew;

Page 14: DBS Program Presentation

This graphic is animated.14

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;

}

Remove Node

Page 15: DBS Program Presentation

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;

}

Page 16: DBS Program Presentation

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;}

Page 17: DBS Program Presentation

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;}

Page 18: DBS Program Presentation

This graphic is animated.18

Retrieve Node Function

static int retrieveNode

(LIST *pList, int key)

{

/* Local Declarations */

NODE *pPre;

NODE *pLoc;

/* Statements */

return _search (pList, &pPre, &pLoc, key);

} /* retrieveNode */

Page 19: DBS Program Presentation

19

Search Function

int _search(LIST *pList, NODE **pPre, NODE **pLoc, int s_data){ /* Statements */

*pPre = NULL;*pLoc = pList->head;if (pList->count == 0)

return 0;

/* Test for argument > last node in list */if ( s_data > pList->rear->data) {

*pPre = pList->rear;*pLoc = NULL;return 0;

} /* 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 */return 1;

else /* i.e., s_data < (*pLoc)->data */return 0;

} /* _search */

Page 20: DBS Program Presentation

This graphic is animated.20

Local Declaration

*pPre = NULL;

*pLoc = pList->head;

/* this 2 lines of code initialize the *pPre and *pLoc

*/

Page 21: DBS Program Presentation

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

*/

Page 22: DBS Program Presentation

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;

return 0;

} /* 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

*/

Page 23: DBS Program Presentation

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 */return 1;

else /* i.e., s_data < (*pLoc)->data */return 0;

Page 24: DBS Program Presentation

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 */return 1;

else /* i.e., s_data < (*pLoc)->data */return 0;

Page 25: DBS Program Presentation

25

emptyList() Function

int emptyList (LIST *pList){/* Statements */

return (pList->count == 0);

} /* emptyList */

return (pList->count == 0);Code ExplanationIf pList->count is 0, meaningreturn (0 == 0); whichevaluates to true then thefunction will return 1(true). True in thefollowing code means list is empty &vice versa.

If pList->count is more than 0, meaning return (pList->count !=0); which evaluates to falsethen the function will return0(false).

Page 26: DBS Program Presentation

26

listCount() Function

int listCount(LIST *pList)

{/* Statements */

return pList->count;} /* listCount */

This function returned thenumber of nodes in thelinked list.Do you know that ?Whenever, a node isadded/removed thepList->count isincremented/decrementedrespectively.

Page 27: DBS Program Presentation

27

traverse() 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.

Page 28: DBS Program Presentation

28

traverse() Function 1st Scenario

if (fromWhere == 0) {

/*Start from first node */

if (pList->count == 0)

success = 0;

Let’s recall that we assumed we have 3nodes: 2, 3 & 8. Hence,these conditions will not be satisfied.

It will only be satisfiedwhen there’s no node in the list.

Page 29: DBS Program Presentation

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

Page 30: DBS Program Presentation

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.

Page 31: DBS Program Presentation

This graphics is animated.31

traverse() Function 4th Scenario

else {

/* Start from current position */

else {

pList->pos = pList->pos->link;

*t_data = pList->pos->data;

success = 1;