Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Reading Assignment
Secion 6.13: Array of Pointers From the book A Book on C
. . .
. . .
TOP
. . .
TOP
9 6 5
. . .
TOP
IS_ENPTY FALSE
6 5
6 5
IS_EMPTY() : true iff stack is empty IS_FULL() : true iff stack is full
6 5
Some implementations
TOP: location where the next element will be stored in stack So, initially TOP=0; PUSH: push one element to stack if stack is not full POP : take out one element in stack provided stack is non-empty
TOP
6 5
IS_EMPTY() : true iff stack is empty IS_FULL() : true iff stack is full
Stack Implementation
-- using Arrays -- using linked lists
Arrays
Benefits
Faster access (constant time access) Easy to to declare and use
All languages provide array data structures
Problems
May not be easy to declare very large arrays Size is fixed, can not be expanded Insertion/ deletion (in between) is costly
Prefix notation
a + ( b - c) * d ( a + ( ( b c )) * d ) ) // infix notation
+a*-bcd
// prefix notation
Postfix notation
a + ( b - c) * d ( a + ( ( b c )) * d ) ) // infix notation
abcd*+
// postfix notation
abcd*+
// let a = 5, b = 6, c = 7, d =8
Read the expression from L R push if you see a variable when you see an operator, pop arguments, compute and push
abcd*+
// let a = 5, b = 6, c = 7, d =8
when you see an operator, pop arguments, compute and push push (a) push (b) push (c)
c b a
c b a
-1 a
abcd*+
d -1 a
abcd*+
// let a = 5, b = 6, c = 7, d =8 see +
compute a + (-8)
-8 a
abcd*+
// let a = 5, b = 6, c = 7, d =8
-3
uses of a stack
To show that a program statement is correct (recognizing program statements) When the program runs, there is a runtime stack to store function locals and arguments Expression evaluator
Algorithm: binary_search
Input:
An array A[0..max] of integers; elements are already sorted and in ascending order. A number num
Output:
Array index i, if num is present in A[0..max] -1 if num is not in the array
Algorithm: binary_search
Step 1: Step 2: Step 3: Step 4: Step 5: begin := 0; end := max; if (begin > end) output (-1); goto Step 7 mid := (begin + end)/2 if A[mid] = num output (mid); go to Step 7 if (num > A[mid]) begin := mid + 1; go to Step 2 endif Step 6: if (num < A[mid]) end := mid 1 go to step 2 endif Step 7: STOP
Algorithm: binary_search
Step 0: Step 1: Step 2: Step 3: Step 4: Step 5: if (max = 0) output(-1); goto Step 7 begin := 0; end := max; if (begin > end) output (-1); goto Step 7 mid := (begin + end)/2 if A[mid] = num output (mid); go to Step 7 if (num > A[mid]) begin := mid + 1; go to Step 2 endif Step 6: if (num < A[mid]) end := mid 1 go to step 2 endif Step 7: STOP
Structures
Complex numbers.
c1 = a + jb
where j = square_root(-1);
a is called the real part, b is called the imaginary part. c2 = c + jd c1 + c2 = (a+c) + j(b +d) c1 c2 = (a c) + j(b d) c1*c2 = ac bd + j(ad +bc)
Complex numbers.
c1 = a + jb otherwise written as ( a , b ) real part imaginary part
Implementing complex-numbers
int c_real, c_imag, d_real, d_imag, x_real, x_imag; c_real = 10; c_imag = 20; d_real = 30; d_imag = 40
Problems
The fact that c_real and c_imag are parts of the same entity is lost. Also the problem of returning a complex number
Simarly
A persons
Or a students
(*a).real = 10; (*a).imag = 20; // bracketing required; . Has higher precedence than *
xxxx
a = &b;
xx xx
a = &b;
10 xx
10 20
Structure in heap
struct complex *a;
Structure in heap
struct complex *a; a = (struct complex *) calloc(1, sizeof(struct complex); a-> real = 10; a->imag = 20;
Linked Lists
struct list { int data; struct list *next; }
data list
Linked Lists
struct list { int data; struct list *next; } struct list a, b;
data list
Linked Lists
struct list { int data; struct list *next; } struct list a, b; a.data = 10; a.next = &b; b.data = 20;
10
list
20
list
Linked Lists
struct list { int data; struct list *next; } struct list a, b; a.data = 10; a.next = &b; b.data = 20; b.next = NULL
10
next
20
next
Linked Lists
struct list { int data; struct list *next; } struct list a, b; a.data = 10; a.next = &b; b.data = 20; b.next = NULL
10
20
a data = 10; a next = b; b data = 20; b next = c; c data = 30; c next = NULL; a
10
20
30
a data = 10; a next = b; a next data = 20; a next next = c; a next next data = 30; a next next next = NULL; a
10
20
30
struct list *create_node(){ struct list *nn; nn = (struct list *) malloc(sizeof(struct list)); return(nn); }
struct list *make_list(){ struct list *p,*q; int dd; q = create_node(); q next = NULL: scanf(%d, &dd); while (dd != 9999){ p = create_node(); p next = q; p data = dd; q = p; scanf(%d, &dd); } return(q); }