Sei sulla pagina 1di 42

CO 203 Data Structure

Dipti Rana

What this subject is about ?


Data structures:
conceptual and concrete ways to organize data for efficient storage and efficient manipulation

Why do we need them ?


Computers take on more and more complex tasks
Imagine: index of 8 billion pages ! (Google)

Software implementation and maintenance is difficult Clean conceptual framework allows for more efficient and more correct code

Why do we need them ?


Requirements for a good software:
Clean Design Easy maintenance Reliable (no core dumps) Easy to use Fast algorithms

Efficient
Efficient

data structures

algorithms

Example
A collection of 3,000 texts with avg. of 20 lines each, with avg. 10 words / line
600,000 words

Find all occurrences of the word happy Suppose it takes 1 sec. to check a word for correct matching What to do?

Example (contd)
What to do?
Sol. 1 Sequential matching: 1 sec. x 600,000 words = 166 hours Sol. 2 Binary searching: - order the words - search only half at a time Ex. Search 25 in 5 8 12 15 15 17 23 25 27 25 ? 15 15 17 23 25 27 25 ? 23 23 25 27 25 ? 25 How many steps?

Some example data structures


log 2 600000 = 19 sec. vs .166 hours!

Set

Stack

Tree

Data structure = representation and operations associated with a data type

What will you learn?


What are some of the common data structures What are some ways to implement them How to analyze their efficiency How to use them to solve practical problems

Data Structure
Primitive
Data structures that are directly operated upon by machine level instructions
int, real, character, pointer, logical data item (true/false)

Non Primitive
No direct deletion or insertion operation are performed
Arrays [using predefined data types], lists and files

Data Structure
Linear
A list which displays the relationship of adjacency between elements is said to be linear
Stack, queue, link list

Non Linear
Tree, graphs

Data Structure
Sequential vs. Non Sequential Static vs. Dynamic

Topics
Arrays Stacks Queues Link lists Trees Graphs Hashing / Dictionaries Sorting

What you need


Programming experience

Textbook
Introduction to Data Structures with Applications
Tremblay and Sorenson

C compiler

Array

Arrays
Array: a set of pairs (index and value) data structure For each index, there is a value associated with that index. representation (possible) implemented by using consecutive memory.

Arrays in C
int list[5], *plist[5]; list[5]: five integers list[0], list[1], list[2], list[3], list[4] *plist[5]: five pointers to integers plist[0], plist[1], plist[2], plist[3], plist[4] implementation of 1-D array list[0] base address = list[1] + sizeof(int) list[2] + 2*sizeof(int) list[3] + 3*sizeof(int)

Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: ? Difference: ?

Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 (list2 + i) *(list2 + i) -

Arrays in C (contd)
Compare int *list1 and int list2[5] in C. Same: list1 and list2 are pointers. Difference: list2 reserves five locations. Notations: list2 - a pointer to list2[0] (list2 + i) - a pointer to list2[i] (&list2[i]) *(list2 + i) - list2[i]

Example : print out address and value


Example:
int one[] = {0, 1, 2, 3, 4}; void print1(int *ptr, int rows) { printf(Address Contents\n); for (i=0; i < rows; i++) printf( ____________________ ); printf(\n); }

Address 1228 1230 1232 1234 1236

Contents 0 1 2 3 4

Example : print out address and value


Example:
int one[] = {0, 1, 2, 3, 4}; void print1(int *ptr, int rows) { printf(Address Contents\n); for (i=0; i < rows; i++) printf(%8u%5d\n, ptr+i, *(ptr+i)); printf(\n); }

Address 1228 1230 1232 1234 1236

Contents 0 1 2 3 4

Memory Organization
During run time, variables can be stored in one of three pools
Stack Static heap Dynamic heap

Stack
Maintains memory during function calls
Argument of the function Local variables Call Frame

Variables on the stack have limited life time

Stack - Example
int foo( int a, double f ) { int b; }

<call> a f b

Stack - Example
int foo( int a, double f ) { int b; { int c; } }

<call> a f b

Stack - Example
int foo( int a, double f ) { int b; { int c; } }

<call> a f b c

Stack - Example
int foo( int a, double f ) { int b; { int c; } }

<call> a f b c

Stack - Example
int foo( int a, double f ) { int b; { int c; } }

<call> a f b c

Stack recursive example


void foo( int depth ) { int a; if( depth > 1 ) foo( depth-1 ); } int main() { foo(3);

<call> depth a <call> depth a <call> depth a

Stack errors?
void foo( int depth ) { int a; if( depth > 1 ) foo( depth ); }

Will result in run time error: out of stack space

Static heap
Memory for global variables
#include <stdio.h> const int ListOfNumbersSize = 1000; int ListOfNumbers[ListOfNumbersSize]; int main() {

Static heap
Variables on the static heap are defined throughout the execution of the program Memory on the static heap must be defined at compile time

Static heap: reverse example


Example: program to reverse the order of lines of a file To this task, we need to read the lines into memory Print lines in reverse How do we store the lines in memory?

Static heap: reverse example


const int LineLength = 100; const int NumberOfLines = 10000; char Lines[NumberOfLines][LineLength]; int main() { int n = ReadLines(); for( n-- ; n >= 0; n-- ) printf(%s\n, Lines[n]); }

Static heap: reverse example


This solution is problematic: The program cannot handle files larger than these specified by the compile time choices If we set NumberOfLines to be very large, then the program requires this amount of memory even if we are reversing a short file Want to use memory on as needed basis

Dynamic Heap
Memory that can be allocated and freed by the program during run time The program controls how much is allocated and when Limitations based on run-time situation
Available memory on the computer

Allocating Memory from Heap


void *malloc( size_t Size );

Returns a pointer to a new memory block of size Size Returns NULL if it cannot allocate memory of this size

Example: strdup
Function to duplicate a string:
char * strdup( char const *p ) { int n = strlen(p); char* q =(char*)malloc(sizeof(char)*(n+1)); if( q != NULL ) strcpy( q, p ); return q; }

This function is part of the standard library

Memory Management
void foo( char const* p ) { char *q = strdup( p ); // do something with q }

<call> p q

Heap

The allocated memory remains in use cannot be reused later on

a b \0

De-allocating memory
void free( void *p );

Returns the memory block pointed by p to the pool of unused memory

Example of free
void foo( char const* p ) { char *q = strdup( p ); // do something with q free(q); }

This version frees the allocated memory

Further Knowledge
Read manual page of malloc calloc realloc free

Potrebbero piacerti anche