Presentation is loading. Please wait.

Presentation is loading. Please wait.

Philip Bernhard, PhD Spring 2018

Similar presentations


Presentation on theme: "Philip Bernhard, PhD Spring 2018"— Presentation transcript:

1 Philip Bernhard, PhD Spring 2018
CSE 1002 Fundamentals of Software Development 2 More Linked-Lists! (not on Friday’s exam) Philip Bernhard, PhD Spring 2018

2 Linked-List Declarations
struct INTNODE { int value; struct INTNODE *next; }; struct INTNODE *headNode = NULL; struct INTNODE *tailNode = NULL;

3 Insert After Value Previously we discussed inserting at the beginning, and inserting at the end. With both functions, care had to be taken to update the headNode and tailNode pointers. Suppose we want to insert a new value after a given value that is already in the list. What are all the special cases?

4 Lets Start Simple (whats missing?)
/* Assume the given value is in the linked list */ void addNodeAfterValue(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; tempNode = headNode; while(tempNode->value != value) { tempNode = tempNode->next; } newNode->next = tempNode->next; tempNode->next = newNode; return;

5 Need To Update the tailNode
void addNodeAfterValue(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; tempNode = headNode; while(tempNode->value != value) { tempNode = tempNode->next; } newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode; return;

6 Any Other Special Cases?
Do we have to worry about the case where the list is empty? How about if the list isn’t empty, but the given value isn’t in the list? (this case subsumes the first) Such cases may or may not be important, depending on the broader context of the program.

7 What if the List is Empty?
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; if (headNode != NULL) { tempNode = headNode; while(tempNode->value != value) { tempNode = tempNode->next; } newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode; return;

8 What if the Value Isn’t in the List?
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; if (headNode != NULL) { tempNode = headNode; while (tempNode->value != value) { tempNode = tempNode->next; if (tempNode == NULL) { return; } newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode;

9 Another Way – Short Circuiting
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; if (headNode != NULL) { tempNode = headNode; while ((tempNode != NULL) && (tempNode->value != value)) { tempNode = tempNode->next; } if (tempNode == NULL) return; /* Note the lack of an “else” */ newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode; return;

10 A Logical Difference Notice the lack of an “else” on the inner “if” statement. This results in what are called “multiple exit points” from the function. Some programmers and standards recommend against this, others do not.

11 Eliminating the Multiple returns
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; if (headNode != NULL) { tempNode = headNode; while ((tempNode != NULL) && (tempNode->value != value)) { tempNode = tempNode->next; } if (tempNode != NULL) { newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode; return;

12 If Multiple returns Are Acceptable
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = NULL; if (headNode == NULL) return; tempNode = headNode; while ((tempNode != NULL) && (tempNode->value != value)) { tempNode = tempNode->next; } if (tempNode == NULL) newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode;

13 One More Observation In all of the previous examples, the first few lines could be moved, so that they aren’t executed unless necessary.

14 Without Multiple returns
void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; if (headNode != NULL) { tempNode = headNode; while ((tempNode != NULL) && (tempNode->value != value)) { tempNode = tempNode->next; } if (tempNode != NULL) { newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode; return;

15 With Multiple returns void addNodeAfterValue1(int value, int newData) { struct INTNODE *newNode, *tempNode; if (headNode == NULL) return; tempNode = headNode; while ((tempNode != NULL) && (tempNode->value != value)) { tempNode = tempNode->next; } if (tempNode == NULL) newNode = (struct INTNODE *) malloc(sizeof(struct INTNODE)); newNode->value = newData; newNode->next = tempNode->next; tempNode->next = newNode; if (newNode->next == NULL) tailNode = newNode;


Download ppt "Philip Bernhard, PhD Spring 2018"

Similar presentations


Ads by Google