16.216 ECE Application Programming
Download
Report
Transcript 16.216 ECE Application Programming
16.216
ECE Application Programming
Instructor: Dr. Michael Geiger
Fall 2013
Lecture 34:
Dynamically allocated data structures (continued)
Lecture outline
Announcements/reminders
Review
Program 10 due 12/11
Dynamically allocated data structures
Today’s class
7/17/2015
Continue discussion of linked lists
ECE Application Programming: Lecture 34
2
Review: pointer-based data structures
Data structures to optimize data organization
Structure containing pointer(s) to other structure
Adding data: allocate space for new node, then adjust
pointers
Deleting data: adjust pointers, then free space for node
Example: linked list
typedef struct node {
int value;
struct node *next;
// Data
// Pointer to
// next node
} LLnode;
Image source: http://en.wikipedia.org/wiki/Linked_list
7/17/2015
ECE Application Programming: Lecture 11
3
Review: Adding to list
Simplest form (unordered list): add new item to beginning of list
LLnode *addNode(LLnode *list, int v) {
LLnode *newNode;
// Allocate space for new node; exit if error
newNode = (LLnode *)malloc(sizeof(LLnode));
if (newNode == NULL) {
fprintf(stderr,
"Error: could not allocate new node\n");
exit(0);
}
newNode->value = v;
newNode->next = list;
return newNode;
// Copy value to new node
// next points to old list
}
7/17/2015
ECE Application Programming: Lecture 11
4
Examples
Write functions for
Finding item in list:
LLnode *findNode(LLnode *list, int v);
Function should return pointer to node if found
Return NULL otherwise
Removing item from list
LLnode *delNode(LLnode *list, int v);
7/17/2015
Must deallocate space for deleted node
Function should return pointer to start of list after it has
been modified
Note: removing first element in list is special case
ECE Application Programming: Lecture 34
5
Solution: findNode()
LLnode *findNode(LLnode *list, int v) {
LLnode *n;
n = list;
// Start with first node
while (n != NULL) {
// Search until after
//
last node
if (n->value == v)
// Data found--return n
return n;
n = n->next;
}
return NULL; // If you get here, data wasn't found
}
7/17/2015
ECE Application Programming: Lecture 34
6
Solution: delNode() (slide 1 of 2)
LLnode *delNode(LLnode *list, int v) {
LLnode *cur = list;
LLnode *prev = NULL;
// Pointer to current node—
//
initially start of list
// Pointer to node before cur—
//
initially NULL
// Loop will search list, stopping either
//
when list ends or value is found
while ((cur != NULL) && (cur->value != v)) {
prev = cur;
cur = cur->next;
}
// Data wasn't found--return unmodified list
if (cur == NULL)
return list;
7/17/2015
ECE Application Programming: Lecture 34
7
Solution: delNode() (slide 2 of 2)
// Data is in first node--must change pointer
//
to start of list
if (prev == NULL)
list = list->next;
// Data is in some other node
// Set next pointer in prev (node before one being
//
removed) to point past node being removed
else
prev->next = cur->next;
free(cur);
return list;
// Deallocate deleted node
}
7/17/2015
ECE Application Programming: Lecture 34
8
Sorted linked list
Can ensure each item is sorted as it’s added
Slower item insertion, but faster search
Not easy with arrays: must move existing data
Keeping linked list sorted
Find appropriate location
Often done by going “past” appropriate spot
Modify pointers
Node before correct spot points to new node
New node points to node after correct spot
Image source: http://en.wikipedia.org/wiki/Linked_list
7/17/2015
ECE Application Programming: Lecture 34
9
Examples
Write functions for:
Adding item to sorted list:
LLnode *addSortedNode(LLnode *list, int v);
Use addNode() as a starting point
Instead of adding node at beginning, find appropriate place in
list and then add
Function should return pointer to start of list after it has been
modified
Finding item in sorted list:
LLnode *findSortedNode(LLnode *list, int v);
7/17/2015
Use findNode() as starting point—should perform same
operation, but more efficiently
Function should return pointer to node if found
Return NULL otherwise
ECE Application Programming: Lecture 34
10
Next time
If we finish linked lists today, no lecture
Friday/Monday
Office hours in Ball 301—feel free to bring your
laptops and work on your code
If we don’t finish linked lists today, will finish on
Friday and hold office hours Monday
Wednesday, 12/11: Exam 3 Preview lecture
Exam 3: Wednesday, 12/18, 8:00-11:00 AM
Reminders:
7/17/2015
Program 10 due 12/11
ECE Application Programming: Lecture 34
11