This preview shows pages 1–4. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Introduction to LowLevel Programming Concepts CMSC 212 ecture 15 1 CMSC 212 Lecture 15 October 20, 2009 Last time c Chapter 11, Dynamic memory allocation – dynamic memory allocation and deallocation – common errors of different types having to do with dynamicallyallocated data – the realloc() function 2 CMSC 212 Chapter 12, Using Structures and Pointers (Pointers and Data 3 CMSC 212 Structures) Linked data structures c 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 c Example declaration: typedef struct node { struct node *next; 4 CMSC 212 c next and root are pointers to Node s root 5 10 15 NULL int data; } Node; Node *root; Finding a value in a singlylinked 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 singlylinked list head 5 10 15 NULL 6 CMSC 212 7 insert 7 2 insert 2 Inserting into an ordered singlylinked 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)); 7 CMSC 212 if (new_item == NULL) 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 singlylinked 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)); 8 CMSC 212 if (new_item == NULL) return 0; new_item>data= new_value; new_item>next= current; if (prev == NULL) *head= new_item; else prev>next= new_item; return 1; } Tracing deletion head 5 10 15 NULL 9 CMSC 212 delete 7 2 delete 2 7 Deleting from a singlylinked 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; } Doublylinked lists c Each node: – contains a value, – a pointer to the next element, and...
View
Full
Document
This note was uploaded on 11/23/2009 for the course CMSC 212 taught by Professor Staff during the Spring '08 term at Maryland.
 Spring '08
 staff

Click to edit the document details