09-linked_structures

09-linked_structures - Computer Science 136: David R....

Info iconThis preview shows pages 1–8. 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

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: Computer Science 136: David R. Cheriton School of Computer Science Elementary Algorithm Design and Data Abstraction Winter 2012 Lecture 9 Linked Data Structures The C language does not provide the convenient growing data structures that are given by many other languages, such as Racket, Python, etc. Instead you must build them from scratch, and manage the memory yourself. This is tricky, and fraught with many dangers. We examine the tools C provides and techniques to use them here. CS 136 Winter 2012 09 Linked Data Structures 1/30 Moving forward Last time we saw C: Variable scope, structs, memory model, pointers Plan for today: Linked data structures ADTs in C CS 136 Winter 2012 09 Linked Data Structures 2/30 The Heap: malloc and free [Ch. 17] You can ask C to borrow some memory, and then give it back later. malloc() to borrow memory free() to give it back // Declare a pointer to hold an address int * ptr; // ' ' Please give me an address where there is // enough room to store an int . ' ' ptr = malloc( sizeof ( int )); // ...do stuff to *ptr - it ' s just an int! *ptr = 0; // initialize... ... // Okay I am done with that memory // --> you can give it to somebody else. free(ptr); CS 136 Winter 2012 09 Linked Data Structures 3/30 Back to the Passport Office Imagine the types you might want for a passport office: // Knows how to give out tickets TicketMachine tm = makeTicketMachine(); Ticket t1 = nextTicket(tm); // Knows number Ticket t2 = nextTicket(tm); // Knows number int eq = ticketsEqual(t1,t2); // 1 if equal, else 0 // Return used memory using free(); recycleTicketMachine(tm); CS 136 Winter 2012 09 Linked Data Structures 4/30 typedef for type naming typedef is a C language feature which lets you give less complicated, mnemonic names for types It can be used to give program-specific names for types: typedef int Ticket; Ticket reminds the code reader what it is for Can change the actual type for Ticket later It can be used to avoid the annoying struct keyword. struct tm_struct { int next; }; typedef struct tm_struct TM_s; Now we can declare variables using TM_s t , instead of the more awkward struct tm_struct t Can be used for pointer types: typedef struct tm_struct* TicketMachine; TicketMachine is a synonym for a pointer to a struct tm_struct CS 136 Winter 2012 09 Linked Data Structures 5/30 Passport Office File ticketmachine.h : struct tm_struct { int next; } typedef tm_struct* TicketMachine; typedef int Ticket; TicketMachine makeTicketMachine(); Ticket nextTicket(TicketMachine tm); int ticketsEqual(Ticket t1, Ticket t2); void recycleTicketMachine(TicketMachine tm); CS 136 Winter 2012 09 Linked Data Structures 6/30 Passport Office File: ticketmachine.c : #include <stdlib.h> #include "ticketmachine.h" TicketMachine makeTicketMachine() { TicketMachine newTM; newTM = malloc( sizeof ( struct tm_struct)); newTM->next = 1; // First ticket to give out is 1 return newTM;...
View Full Document

Page1 / 30

09-linked_structures - Computer Science 136: David R....

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

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