Intro Dipti Mam
Transcript of Intro Dipti Mam
-
8/6/2019 Intro Dipti Mam
1/42
CO 203
Data Structure
Dipti Rana
-
8/6/2019 Intro Dipti Mam
2/42
What this subject is about ?
Data structures:
conceptual and concrete ways to organize data forefficient storage and efficient manipulation
-
8/6/2019 Intro Dipti Mam
3/42
Why do we need them ?
Computers take on more and more complex tasksImagine: index of 8 billion pages ! (Google)
Software implementation and maintenance is difficult
Clean conceptual framework allows for more efficientand more correct code
-
8/6/2019 Intro Dipti Mam
4/42
Why do we need them ?
Requirements for a good software:Clean Design
Easy maintenanceReliable (no core dumps)
Easy to use
Fast algorithms
Efficient data structuresEfficient algorithms
-
8/6/2019 Intro Dipti Mam
5/42
Example
A collection of 3,000 texts with avg. of 20lines 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 matchingWhat to do?
-
8/6/2019 Intro Dipti Mam
6/42
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?
-
8/6/2019 Intro Dipti Mam
7/42
Some example data structures
log2600000 = 19 sec.vs .166 hours!
Set Stack Tree
Data structure = representation and operations
associated with a data type
-
8/6/2019 Intro Dipti Mam
8/42
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
-
8/6/2019 Intro Dipti Mam
9/42
Data Structure
PrimitiveData structures that are directly operated upon by machinelevel 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
-
8/6/2019 Intro Dipti Mam
10/42
Data Structure
LinearA list which displays the relationship of adjacency betweenelements is said to be linear
Stack, queue, link listNon Linear
Tree, graphs
-
8/6/2019 Intro Dipti Mam
11/42
Data Structure
Sequential vs. Non SequentialStatic vs. Dynamic
-
8/6/2019 Intro Dipti Mam
12/42
Topics
Arrays
Stacks
QueuesLink lists
Trees
GraphsHashing / Dictionaries
Sorting
-
8/6/2019 Intro Dipti Mam
13/42
What you need
Programming experience
Textbook
Introduction to Data Structures with Applications
Tremblay and Sorenson
C compiler
-
8/6/2019 Intro Dipti Mam
14/42
Array
-
8/6/2019 Intro Dipti Mam
15/42
Arrays
Array: a set of pairs (index and value)
data structureFor each index, there is a value associated with
that index.
representation (possible)
implemented by using consecutive memory.
-
8/6/2019 Intro Dipti Mam
16/42
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)
-
8/6/2019 Intro Dipti Mam
17/42
Arrays in C (contd)
Compare int *list1 and int list2[5] in C.
Same: ?
Difference: ?
-
8/6/2019 Intro Dipti Mam
18/42
Arrays in C (contd)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 arepointers.
Difference: list2 reserves five locations.
Notations:
list2 -
(list2 + i) -
*(list2 + i) -
-
8/6/2019 Intro Dipti Mam
19/42
Arrays in C (contd)
Compare int *list1 and int list2[5] in C.
Same: list1 and list2 arepointers.
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]
-
8/6/2019 Intro Dipti Mam
20/42
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
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);
}
Example :print out address and value
-
8/6/2019 Intro Dipti Mam
21/42
Address Contents
1228 0
1230 1
1232 2
1234 3
1236 4
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);
}
Example :print out address and value
-
8/6/2019 Intro Dipti Mam
22/42
Memory Organization
During run time, variables can be stored inone of three pools
Stack
Static heap
Dynamic heap
-
8/6/2019 Intro Dipti Mam
23/42
Stack
Maintains memory during function calls
Argument of the function
Local variables
Call Frame
Variables on the stack have limited life time
-
8/6/2019 Intro Dipti Mam
24/42
Stack - Example
int foo( int a, double f )
{
int b;
}
af
b
-
8/6/2019 Intro Dipti Mam
25/42
Stack - Example
int foo( int a, double f )
{
int b;
{
int c;
}
}
af
b
-
8/6/2019 Intro Dipti Mam
26/42
Stack - Example
int foo( int a, double f )
{
int b;
{
int c;
}
}
af
b
c
-
8/6/2019 Intro Dipti Mam
27/42
Stack - Example
int foo( int a, double f )
{
int b;
{
int c;
}
}
af
b
c
-
8/6/2019 Intro Dipti Mam
28/42
Stack - Example
int foo( int a, double f )
{
int b;
{
int c;
}
}
af
b
c
-
8/6/2019 Intro Dipti Mam
29/42
Stack recursive example
void foo( int depth )
{
int a;if( depth > 1 )
foo( depth-1 );
}
int main()
{
foo(3);
deptha
depth
a
depth
a
-
8/6/2019 Intro Dipti Mam
30/42
Stack errors?
void foo( int depth )
{
int a;if( depth > 1 )
foo( depth );
}
Will result in run time error:
out of stack space
-
8/6/2019 Intro Dipti Mam
31/42
Static heap
Memory for global variables
#include
const int ListOfNumbersSize = 1000;
int ListOfNumbers[ListOfNumbersSize];
int main()
{
-
8/6/2019 Intro Dipti Mam
32/42
Static heap
Variables on the static heap are definedthroughout the execution of the program
Memory on the static heap must be defined atcompile time
-
8/6/2019 Intro Dipti Mam
33/42
Static heap: reverse example
Example: program to reverse the order of linesof 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?
-
8/6/2019 Intro Dipti Mam
34/42
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]);
}
-
8/6/2019 Intro Dipti Mam
35/42
Static heap: reverse example
This solution is problematic:
The program cannot handle files larger than these
specified by the compile time choicesIf we setNumberOfLines to be very large, then theprogram requires this amount of memory even if weare reversing a short file
Want to use memory on as needed basis
-
8/6/2019 Intro Dipti Mam
36/42
Dynamic Heap
Memory that can be allocated and freed by theprogram during run time
The program controls how much is allocatedand when
Limitations based on run-time situation
Available memory on the computer
-
8/6/2019 Intro Dipti Mam
37/42
Allocating Memory from Heap
void *malloc( size_t Size );
Returns a pointer to a newmemory block ofsize Size
ReturnsNULL if it cannot allocate memory ofthis size
-
8/6/2019 Intro Dipti Mam
38/42
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
-
8/6/2019 Intro Dipti Mam
39/42
Memory Management
void
foo( char const* p )
{
char *q = strdup( p );
// do something with q
}
The allocated memory remains in use
cannot be reused later on
pq
Heap
a
b
\0
-
8/6/2019 Intro Dipti Mam
40/42
De-allocating memory
void free( void *p );
Returns the memory block pointed byp to thepool of unused memory
-
8/6/2019 Intro Dipti Mam
41/42
Example of free
void
foo( char const* p )
{
char *q = strdup( p );
// do something with q
free(q);}
This version frees the allocated memory
-
8/6/2019 Intro Dipti Mam
42/42
Further Knowledge
Read manual page of
malloc
calloc
realloc
free