C in a Nutshell, Pointers and Basic Data Structures
Transcript of C in a Nutshell, Pointers and Basic Data Structures
![Page 2: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/2.jpg)
2
C in a Nutshell
![Page 3: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/3.jpg)
3
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
![Page 4: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/4.jpg)
4
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
include header for the definition of printf
![Page 5: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/5.jpg)
5
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
main->int ; argc: #arguments ; argv: arguments
![Page 6: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/6.jpg)
6
array of pointers to char= array of strings
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
![Page 7: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/7.jpg)
7
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
printf function, argument = string= char array terminated by 0
![Page 8: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/8.jpg)
8
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;}
printf function, argument = string= char array terminated by 0
string length given byint strlen(char *)declared in string.h
![Page 9: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/9.jpg)
9
Hello World#include <stdio.h>
int main(int argc, char *argv[]){
printf(“Hello world!\n”);
return 0;} end of program, return to caller program
![Page 10: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/10.jpg)
10
Syntax Reminder● if statements:
if (cond) statement else statementif (cond) { statements } else { statements }Advice: use { ... }
● for loops:for(statements;cond; progress) { statements }Advice: use { ... }
● while loops/do – while loops:while(cond) { statements }do { statements } while(cond);Advice: use { ... }
![Page 11: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/11.jpg)
11
Types● Scalars:int, char, short, long,float, double,unsigned int, unsigned char,..
● Records:struct name { types };
● Type definitions:typedef some_type my_type;
![Page 12: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/12.jpg)
12
Types: Examples Custom type for unsigned integers:
typedef unsigned int uint;
Custom type for a pair of unsigned integers:typedef struct { uint first, second; } pair_t;
Using your pair:pair_t p;p.first = 0;p.second = 1;
Similar to Java: a struct is like a class with public fields and without method.
![Page 13: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/13.jpg)
13
Pointers in a Nutshell
![Page 14: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/14.jpg)
14
Pointers in a Nutshell● Pointers and “Pointees”: a pointers stores
a reference to something.
x 42pointer xpoints to
the value 42int *x
![Page 15: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/15.jpg)
15
Pointers in a Nutshell● Pointers and “Pointees”: a pointers stores
a reference to something.
● Dereference operation: starts from the pointer and follows its arrow to access its content (“pointee”).
int *x x 42pointer xpoints to
the value 42
x 42
*x
==
![Page 16: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/16.jpg)
16
Pointers in a Nutshell● Pointer assignment between 2 pointers
makes them point to the same thing.● The “pointee” becomes shared between
the 2 pointers.
x 42
yy = x
x 42
y
y points NOWHERE x and y point to 42
![Page 17: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/17.jpg)
17
Binky's Code Example
int *x, *y;x
yAllocate 2 pointers. This does notallocate the pointee.
NO
WH
ERE
![Page 18: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/18.jpg)
18
Binky's Code Example
int *x, *y;x
yAllocate 2 pointers. This does notallocate the pointee.
x = (int*) malloc(sizeof(int))x
y
Allocate a pointeeand set x to pointto it.
![Page 19: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/19.jpg)
19
Binky's Code Example
int *x, *y;x
yAllocate 2 pointers. This does notallocate the pointee.
int v;x = &v;
x
y
v Allocate a pointeeand set x to pointto it.
![Page 20: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/20.jpg)
20
Binky's Code Example
int *x, *y;x
yAllocate 2 pointers. This does notallocate the pointee.
NO
WH
ERE
x = (int*) malloc(sizeof(int))x
y
Allocate a pointeeand set x to pointto it.
Dereference x tostore 42 in its pointee.
*x = 42x
y
42
![Page 21: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/21.jpg)
21
Binky's Code Example
*y = 13x
y
42
POW!
Try to dereference yby storing 13 in its pointee:there is no pointee!
![Page 22: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/22.jpg)
22
Binky's Code Example
*y = 13x
y
42
POW!
Try to dereference yby storing 13 in its pointee:there is no pointee!
y = xx
y
42 Assign y to x. Now thepointers share the samepointee.
![Page 23: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/23.jpg)
23
Binky's Code Example
*y = 13x
y
42
POW!
Try to dereference yby storing 13 in its pointee:there is no pointee!
y = xx
y
42 Assign y to x. Now thepointers share the samepointee.
*y = 13x
y
13 Dereference y and store 13in its pointee. Note: *x wouldreturn 13 now.
![Page 24: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/24.jpg)
24
More Pointer Fun!
int table[4];int *t = table;
int int int inttint * : points to oneor more ints (tableof ints).
![Page 25: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/25.jpg)
25
More Pointer Fun!
int table[4];int *t = table;
int int int inttint * : points to oneor more ints (tableof ints).
int **pt = &t; pt int ** : points to oneor more int* (tableof int*).
![Page 26: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/26.jpg)
26
More Pointer Fun!
int table[4];int *t = table;
int int int inttint * : points to oneor more ints (tableof ints).
int **pt = &t; pt int ** : points to oneor more int* (tableof int*).
pptint ***ppt = &pt; int *** : ... you get it now!
![Page 27: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/27.jpg)
27
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=1
![Page 28: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/28.jpg)
28
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=1b=2
![Page 29: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/29.jpg)
29
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=1b=2
x y
![Page 30: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/30.jpg)
30
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=1b=2
x y
z=1*x
![Page 31: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/31.jpg)
31
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=2b=2
x y
z=1
*y
![Page 32: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/32.jpg)
32
Pointers: Examplevoid swap(int *x, int *y){
int z = *x;*x = *y;*y = z;
}
...int a = 1;int b = 2;...swap(&a, &b);...
a=2b=1
x y
z=1
![Page 33: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/33.jpg)
33
Pointer Arithmetics● Addition: pointer + index -> pointer
int *p; “p+3” same as “&p[3]” -> int*● Subtraction: pointer – pointer -> index
int a[4], *p = &a[1], *q = &a[3];“q-p” == 2 -> int
![Page 34: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/34.jpg)
34
Pointer Arithmetics● Addition: pointer + index -> pointer
int *p; “p+3” same as “&p[3]” -> int*● Subtraction: pointer – pointer -> index
int a[4], *p = &a[1], *q = &a[3];“q-p” == 2 -> int
● Pointers are typed!int *p; “p+1” points to next integer!char *c; “c+1” points to next character!
![Page 35: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/35.jpg)
35
Big-O Notation in a Nutshell
![Page 36: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/36.jpg)
36
Big-O Notation in a Nutshell
T n∈O f nmeans T(N) is upper bounded by f(n)(at a multiplicative constant) for n“big enough”.
![Page 37: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/37.jpg)
37
Big-O Notation in a Nutshell
Characterizes the way thecomplexity of the computationgrows depending on the size of the problem.
T n∈O f nmeans T(N) is upper bounded by f(n)(at a multiplicative constant) for n“big enough”.
![Page 38: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/38.jpg)
38
Big-O Notation Example
void copy(int *a, int *b, int n){
int i;for(i = 0; i < n; ++i)
b[i] = a[i];}
Complexity: O(n)
![Page 39: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/39.jpg)
39
Big-O Notation Summary
NotationO(1)
O(log n)O(n)
O(n.log n)O(n2)O(n3)O(nc)O(cn)O(n!)
NameConstant
LogarithmicLinear
Pseudo-linearQuadratic
CubicPolynomial
ExponentialFactorial
![Page 40: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/40.jpg)
40
Notation Name
Big-O Notation Summary
O(1)O(log n)
O(n)O(n.log n)
O(n2)
O(nc)O(cn)O(n!)
ConstantLogarithmic
LinearPseudo-linear
Quadratic
PolynomialExponential
Factorial
O(n3) Cubic
Very Easy
Easy
Hard
Very Hard
![Page 41: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/41.jpg)
41
ElementaryData Structures
![Page 42: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/42.jpg)
42
Choosing a Data Structure
● Time (CPU Power)● Space (Memory Space)
● How fast can we access/modify the data ?● How small can we code/compress the data ?● ...
Algorithms consume resources:
Choosing a data structure is a trade-off between time and space complexity:
![Page 43: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/43.jpg)
43
Common Operators
● search(set, key)● insert(set, key)● delete(set, element)
● sort(set)● min(set), max(set)● succ(set,elt), pred(set,elt)● empty(set), count(set)
Basic Operators: Other Operators:
Choose your data-structure depending on what operations
will be performed the most !
![Page 44: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/44.jpg)
44
Arrays
![Page 45: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/45.jpg)
45
One-dimension Arrays
Declaration: int a[12];
a[i] = *(a+i)
baseaddress
index
17 5Array “a” 49 18 86 3 52 6 8 78 2 5
a[0] a[11]
![Page 46: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/46.jpg)
46
Two-dimensions Arrays
Declaration: int a[4][12];
a[i][j] = *(a[i]+j) = *(*(a+i)+j)
Array “a”
••••
1 5 49 5 86 5 52 5 86 5 52 57 5 49 5 86 5 52 5 86 5 52 517 5 49 5 86 5 52 5 86 5 52 525 5 49 5 86 5 52 5 86 5 52 5
a[0][0]
a[3][11]
![Page 47: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/47.jpg)
47
Complexity (Arrays)
n is the number of elements in the arraykey is the index of an element
Operator Timesearch(set, key) O(1)insert(set, key) O(n)delete(set, key) O(n)
min(set) / max(set) O(n)succ(set,elt)/pred(set,etl) O(n)
isempty(set) O(1)count(set) O(1)
SpaceO(1)O(n)O(n)O(1)O(1)O(1)O(1)
![Page 48: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/48.jpg)
48
Matrix Copy(why locality is good...)
![Page 49: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/49.jpg)
49
copy1
int copy1(float src[SIZE_X][SIZE_Y], float dest[SIZE_X][SIZE_Y]) { int i, j;
for (j=0; j<SIZE_Y; j++) for (i=0; i<SIZE_X; i++) dest[i][j] = src[i][j];
return 0;}
#define SIZE_X 20#define SIZE_Y 20
![Page 50: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/50.jpg)
50
copy2
int copy2(float src[SIZE_X][SIZE_Y], float dest[SIZE_X][SIZE_Y]) { int i, j;
for (i=0; i<SIZE_X; i++) for (j=0; j<SIZE_Y; j++) dest[i][j] = src[i][j];
return 0;}
![Page 51: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/51.jpg)
51
copy3
int copy3(float* src, float* dest) { int size;
for (size=(SIZE_X*SIZE_Y); size; size) *dest++ = *src++;
return 0;}
![Page 52: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/52.jpg)
52
int copy4(float* src, float* dest) {
memcpy(dest, src, (SIZE_X*SIZE_Y)*sizeof(float));
return 0;}
copy4
![Page 53: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/53.jpg)
53
Performance
1000 2500 5000 7500 100000
25
50
75
100
125
150
175
200
225
250
275
300
325
350
SIZE_X = 75
copy1
copy2
copy3
copy4
SIZE_Y
ms/call
![Page 54: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/54.jpg)
54
Linked-lists
![Page 55: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/55.jpg)
55
Singly Linked-lists
Declaration:struct list { int value; struct list* next;};
Memory Overhead: One extra pointer for each element
49 • 86 • 52 • NULL17 •Head
node
element link
![Page 56: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/56.jpg)
56
Doubly Linked-lists
Declaration:struct list { struct list* pred; int value; struct list* next;};
Memory Overhead: Two extra pointers for each element
NULLHead
• 49 • • 86 •
NULL
• 52 •• 17 •
![Page 57: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/57.jpg)
57
Search
● Return a pointer to the right node● Return NULL if not found
Requirements:
struct list* search(struct list *head, const int key)
![Page 58: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/58.jpg)
58
Search
struct list* search(struct list *head, const int key) {
while ((head != NULL)&&(head>value != key)) head = head>next;
return head;}
![Page 59: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/59.jpg)
59
Insert
● Return a pointer to the inserted node● Return NULL in case the malloc fail● Can handle the case where the list is NULL
Requirements:
struct list* insert(struct list *head, const int value)
![Page 60: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/60.jpg)
60
Insert
struct list* insert(struct list *head, const int value) { struct list *tmp, *new;
new = (struct list*) malloc(sizeof(struct list));
if (new != NULL) { new>value = value; new>next = head; }
return new;}
also fine if head is null
![Page 61: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/61.jpg)
61
Delete
● Return a pointer to the head node● Return NULL when deleting a singleton list● Can handle the case where the list is NULL
Requirements:
struct list* delete(struct list *head, const int value)
![Page 62: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/62.jpg)
62
Deletestruct list* delete(struct list *head, const int value) { struct list *current = head, *previous = head;
if (head != NULL) { if (head>value != value) { current = current>next;
while ((current != NULL) && (current>value != value)) { previous = current; current = current>next; } if (current != NULL) previous>next = current>next; } else head = head>next;
free(current); } return head;}
49 86 • 52 cont.17headcurrent
previous
![Page 63: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/63.jpg)
63
Deletestruct list* delete(struct list *head, const int value) { struct list *current = head, *previous = head;
if (head != NULL) { if (head>value != value) { current = current>next;
while ((current != NULL) && (current>value != value)) { previous = current; current = current>next; } if (current != NULL) previous>next = current>next; } else head = head>next;
free(current); } return head;}
49 86 • 52
cont.
17headcurrent
previous
![Page 64: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/64.jpg)
64
Complexity (Linked-lists)
n is the number of elements in the linked-listkey is a value
TimeO(n)O(1)O(n)O(n)O(n)O(1)O(n)
SpaceO(1)O(1)O(1)O(1)O(1)O(1)O(1)
Operatorsearch(set, key)insert(set, key)delete(set, key)
min(set) / max(set)succ(set,elt)/pred(set,etl)
isempty(set)count(set)
![Page 65: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/65.jpg)
65
Trees
![Page 66: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/66.jpg)
66
Trees
● Each node has zero or more children● A node with a child is a parent● A node without a child is a leaf● A node without a parent is a root
Definitions:
struct tree { int value; struct tree *left; struct tree *right;};
2
7 5
962
5 11 4
root
leaf
parent
child
![Page 67: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/67.jpg)
67
Pre-order
void traverse(struct tree *node) {
printf("Value is: %i\n", node>value);
if (node>left != NULL) traverse(node>left);
if (node>right != NULL) traverse(node>right);
return;}
![Page 68: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/68.jpg)
68
In-order
void traverse(struct tree *node) {
if (node>left != NULL) traverse(node>left);
printf("Value is: %i\n", node>value);
if (node>right != NULL) traverse(node>right);
return;}
![Page 69: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/69.jpg)
69
Post-order
void traverse(struct tree *node) {
if (node>left != NULL) traverse(node>left);
if (node>right != NULL) traverse(node>right);
printf("Value is: %i\n", node>value);
return;}
![Page 70: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/70.jpg)
70
Tree Traversal
● Pre-order:2, 7, 2, 6, 5, 11, 5, 9, 4
● Post-order:2, 5, 11, 6, 7, 4, 9, 5, 2
● In-order:2, 7, 5, 6, 11, 2, 5, 4, 9
2
7 5
962
5 11 4
![Page 71: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/71.jpg)
71
Operators on Trees
● Breadth-first Operators:The operation on the node is applied before exploring the children
● Depth-first Operators:The operation on the node is applied after exploring the children
![Page 72: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/72.jpg)
72
Complexity (Trees)
n is the number of elements in the tree
TimeO(n)O(n)O(n)O(n)O(n)O(1)O(n)
SpaceO(1)O(1)O(1)O(1)O(1)O(1)O(1)
Operatorsearch(set, key)insert(set, key)delete(set, key)
min(set) / max(set)succ(set,elt)/pred(set,etl)
isempty(set)count(set)
![Page 73: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/73.jpg)
73
Some UsualData Structures
![Page 74: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/74.jpg)
74
Queues(FIFO)
![Page 75: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/75.jpg)
75
Queues (FIFO)
First In First Out● Implemented via:
– Array– Linked-list
● Applications:– Buffers and Spoolers
(networks, printer, scheduler, ...)
13
215
89281114
78
![Page 76: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/76.jpg)
76
Complexity (Queues)
n is the number of elements in the queuekey is a value
TimeO(n)O(1)O(n)O(n)O(n)O(1)O(n)
SpaceO(n)O(1)O(n)O(n)O(n)O(1)O(n)
Operatorsearch(set, key)insert(set, key)delete(set, key)
min(set) / max(set)succ(set,elt)/pred(set,etl)
isempty(set)count(set)
![Page 77: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/77.jpg)
77
Stacks(FILO)
![Page 78: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/78.jpg)
78
Stacks (FILO)
17
6457789028
push17
6457789028
pop
First In Last Out● Implemented via:
– Array– Linked-list
● Applications:– Stack based calculus
(CPU, parser, ...)
![Page 79: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/79.jpg)
79
Complexity (Stacks)
n is the number of elements in the stackkey is a value
TimeO(n)O(1)O(n)O(n)O(n)O(1)O(n)
SpaceO(n)O(1)O(n)O(n)O(n)O(1)O(n)
Operatorsearch(set, key)insert(set, key)delete(set, key)
min(set) / max(set)succ(set,elt)/pred(set,etl)
isempty(set)count(set)
![Page 80: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/80.jpg)
80
Binary Heaps
![Page 81: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/81.jpg)
81
Binary Heaps
A heap is a tree structure such that,if A and B are nodes of a heap andB is a child of A, then:
key A≥key B
A binary heap is a heap based on a binary tree
![Page 82: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/82.jpg)
82
Binary Heaps
11
7
941
2 3
6
10
● Implemented via:– Array
(a[i] has two children a[2i+1],a[2i+2])
– Tree● Applications:
– Quick access to data( database, ...)
![Page 83: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/83.jpg)
83
Complexity (Binary Heaps)
n is the number of elements in the heapkey is a value
TimeO(log n)O(log n)O(log n)O(log n)O(log n)
O(1)O(n)
SpaceO(1)O(1)O(1)O(1)O(1)O(1)O(1)
Operatorsearch(set, key)insert(set, key)delete(set, key)
min(set) / max(set)succ(set,elt)/pred(set,etl)
isempty(set)count(set)
![Page 84: C in a Nutshell, Pointers and Basic Data Structures](https://reader031.fdocuments.net/reader031/viewer/2022013009/61ce6c67809fcb354a170394/html5/thumbnails/84.jpg)
84
Questions ?