{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lecture15 - Last time Chapter 11 Dynamic memory allocation...

Info iconThis preview shows pages 1–4. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Low-Level Programming Concepts CMSC 212 Lecture 15 1 CMSC 212 October 20, 2009 Last time circle6 Chapter 11, Dynamic memory allocation dynamic memory allocation and deallocation common errors of different types having to do with dynamically-allocated data – the realloc() function 2 CMSC 212 Chapter 12, Using Structures and Pointers (Pointers and Data Structures) 3 CMSC 212 Linked data structures circle6 Like objects in Java can have references to objects of the same class, structures in C can have pointers to a structure of the same type circle6 Example declaration: typedef struct node { struct node *next; int data; 4 CMSC 212 circle6 next and root are pointers to Node s root 5 10 15 NULL } Node; Node *root;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Finding a value in a singly-linked list Node *find(Node *current, int value) { while (current != NULL && current->data != value) current= current->next; return current; } 5 CMSC 212 int find(Node *current, int value) { while (current != NULL && current->data != value) current= current->next; if (current != NULL) return 1; else return 0; } Tracing insertion in an ordered singly-linked list head 5 10 15 NULL 6 CMSC 212 7 insert 7 2 insert 2 Inserting into an ordered singly-linked list int insert(Node *head, int new_value) { Node *current= head, *prev= NULL, *new_item= NULL; while (current != NULL && current->data < new_value) { prev= current; current= current->next; } new_item= malloc(sizeof(*new_item)); if (new_item == NULL) 7 CMSC 212 return 0; new_item->data= new_value; new_item->next= current; if (prev == NULL) head= new_item; else prev->next= new_item; return 1; } Inserting into an ordered singly-linked list int insert(Node **head, int new_value) { Node *current= *head, *prev= NULL, *new_item= NULL; while (current != NULL && current->data < new_value) { prev= current; current= current->next; } new_item= malloc(sizeof(*new_item)); if (new_item == NULL) 8 CMSC 212 return 0; new_item->data= new_value; new_item->next= current; if (prev == NULL) *head= new_item; else prev->next= new_item; return 1; }
Background image of page 2
Tracing deletion 5 10 15 NULL 9 CMSC 212 delete 7 2 delete 2 7 Deleting from a singly-linked list int delete(Node **head, int value) { Node *prev= NULL, *current= *head; while (current != NULL && current->data != value) { prev= current; current= current->next; } 10 CMSC 212 if (current == NULL) return 0; /* not found */ if (prev != NULL) prev->next= current->next; else *head= current->next; /* deleted first item */ free(current); return 1; } Doubly-linked lists circle6 Each node: contains a value, a pointer to the next element, and a pointer to the previous element circle6
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 4
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}