Sei sulla pagina 1di 54

A Conceptual Approach to C Programming

M.Satpathy IIT BHUBANESWAR

Reading Assignment
Secion 6.13: Array of Pointers From the book A Book on C

. . .

Stack data structure


In theory, stack is open-ended. TOP: location where the topmost element is stored in stack PUSH: push one element to stack

POP : take out one element in stack provided stack is non-empty


TOP

IS_EMPTY() : true iff stack is empty

. . .

Stack data structure


In theory, stack is open-ended. TOP: location where the topmost element is stored in stack Initially TOP has some special value say, TOP = EMPTY (EMPTY = -1)
IS_EMPTY() : true iff TOP== EMPTY

TOP

. . .

Stack data structure


PUSH(5); PUSH(6); PUSH(9)

TOP

9 6 5

. . .

Stack data structure


PUSH(5); PUSH(6); PUSH(9); POP();

TOP

IS_ENPTY FALSE

6 5

Stack data structure


In practice, stack is bounded.

Integer char float string


TOP

stack stack stack stack

6 5

Stack data structure


TOP: location where the topmost element is stored in stack PUSH: push one element to stack if stack is not full POP : take out one element in stack provided stack is non-empty
TOP

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

Faster searching (binary search) and sorting

Problems
May not be easy to declare very large arrays Size is fixed, can not be expanded Insertion/ deletion (in between) is costly

Implementation using Arrays


int stack[100]; int TOP,BOTTOM; // global variables void initialize_stack(){ TOP = 0; } int push(int elem){ if (!stackfull()){ stack[TOP] = elem; TOP++; return(1); } return(-1) }

Implementation using Arrays


int stack[100]; int TOP,BOTTOM; // global variables void initialize_stack(){ TOP = 0; } int pop( ){ if (!stackempty()){ TOP --; return(stack[TOP]); } return(-1) }

Implementation using Arrays


int stack[100]; int TOP,BOTTOM; // global variables void initialize_stack(){ TOP = 0; } int stackempty( ){ return (TOP == 0); }

int stackfull(){ return(TOP == 100); }

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

Expression evaluation using a stack

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

Expression evaluation using a stack

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

Expression evaluation using a stack 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) // now see

c b a

// pop two arguments, compute, and push

Expression evaluation using a stack 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) // now see


// pop two arguments, compute, and push

-1 a

Expression evaluation using a stack

abcd*+

// let a = 5, b = 6, c = 7, d =8 push d // see *

compute (-1) * 8 and push to stack

d -1 a

Expression evaluation using a stack

abcd*+

// let a = 5, b = 6, c = 7, d =8 see +

compute a + (-8)

-8 a

Expression evaluation using a stack

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

x_real = c_real + d_real . .

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

Name Address Telephone number Occupation


Name, Roll no Discipline Year of study Grades

Or a students

. This is where come Structures

struct complex { int real; int imag; }; struct complex c,d,r;

c.real = 10; c.imag = 20;

d.real = 30; d.imag = 40;

e.real = c.real + d.real; e.imag = c.imag + d.imag;

struct complex { int real; int imag; };

struct complex carray[40]; carray[5].real = 10; carray[5].imag = 20;

struct complex { int real; int imag; };

struct complex *a; // pointer to a structure struct complex b;


a = &b;

(*a).real = 10; (*a).imag = 20; // bracketing required; . Has higher precedence than *

struct complex { int real; int imag; };

struct complex *a; // pointer to a structure struct complex b;


a = &b; (*a).real = 10; (*a).imag = 20;

a ->real = 10; a ->imag = 30;

struct complex *a; struct complex b;


a

xxxx

struct complex *a; struct complex b;


a

a = &b;

xx xx

struct complex *a; struct complex b;


a

a = &b;

10 xx

a -> real = 10;

struct complex *a; struct complex b;


a

a = &b; a -> real = 10; a -> imag = 20;

10 20

Structure in heap
struct complex *a;

a = (struct complex *) calloc(1, sizeof(struct complex);

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

struct list { int data; struct list *next; }


struct list *a, *b, *c; a = (struct list *) malloc(sizeof (struct list)); b = (struct list *) malloc(sizeof (struct list)); c = (struct list *) malloc(sizeof (struct list));

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); }

Potrebbero piacerti anche