lecture15 - Introduction to Low-Level Programming Concepts...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: Introduction to Low-Level 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 dynamically-allocated 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 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)); 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 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)); 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 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 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.

Page1 / 6

lecture15 - Introduction to Low-Level Programming Concepts...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online