You are on page 1of 22

Dynamic 2D arrays, Stacks, Queues

P. P. Chakrabarti

27-03-03

P.P.Chakrabarti, IIT Kharagpur

2D Arrays using arrays of pointers


#define N 20 #define M 10 main() { char word[N], *w[M]; int i, n; scanf("%d",&n); for (i=0; i<n; ++i) { scanf("%s", word); w[i] = (char *) malloc ((strlen(word)+1)*sizeof(char)); strcpy (w[i], word) ; } for (i=0; i<n; i++) printf("w[%d] = %s \n",i,w[i]); }

27-03-03

P.P.Chakrabarti, IIT Kharagpur

How it will look like


w 0 1 2 3
T e n u a d d r u a l k a r \0 S o K h I n v \0

n \0 i a \0

27-03-03

P.P.Chakrabarti, IIT Kharagpur

2D Arrays using a pointer to a pointer


#define N 20 main() { char word[N], **w; /* **w is a pointer to a pointer array */ int i, n; scanf("%d",&n); w = (char **) malloc (n * sizeof(char *)); for (i=0; i<n; ++i) { scanf("%s", word); w[i] = (char *) malloc ((strlen(word)+1)* sizeof(char)); strcpy (w[i], word) ; } for (i=0; i<n; i++) printf("w[%d] = %s \n",i,w[i]); }

27-03-03

P.P.Chakrabarti, IIT Kharagpur

How this will look like


w 0 1 2 3 4
I n d s n i t y a \0 r a l i a \0 A u K e

a \0 e a a n l k a n d \0

N e w Z S r i L

a \0

27-03-03

P.P.Chakrabarti, IIT Kharagpur

2D Array of n rows and constant columns: Single pointer + memory chunk


int (*r)[COLS], rows, scanf("%d",&rows); r = (int (*)[COLS])malloc(rows*COLS*sizeof(int));

r r[0][0]

Dynamically allocated memory

27-03-03

P.P.Chakrabarti, IIT Kharagpur

2D Arrays of n rows and 3 columns


#define COLS 3 main() { int (*r)[COLS], rows, i,j; scanf("%d",&rows); r = (int (*)[COLS])malloc(rows*COLS*sizeof(int)); for (i=0;i<rows;i++) for(j=0; j< COLS; j++) r[i][j] = 2*i+3*j; for (i=0;i<rows;i++) { for(j=0; j< COLS; j++) printf("%d, ", r[i][j]); printf("\n"); } }

[ppchak]$ ./a.out 5 0, 3, 6, 2, 5, 8, 4, 7, 10, 6, 9, 12, 8, 11, 14,

27-03-03

P.P.Chakrabarti, IIT Kharagpur

The Stack ADT


A Stack is a Last-in-First-Out (LIFO) list with all or some of the following interface functions: create: makes a new stack (of a given size) dispose: destroys a stack make_empty: makes the stack empty push: inserts an element into the stack pop: removes the top element from the stack isempty: determines if the stack has no elements isfull: determines if the stack is full in case of a bounded sized stack push is like inserting at the front of the list pop is like deleting from the front of the list Push

Pop

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Postfix Evaluation
Code Meaning 0 1 2 Operand Operator End

Op- Operation code 0 + 1 2 3 * /

Input format: [code, value] sequence Example: 12*4+3 is coded in postfix in the form of ab*c+ as 0 12 0 4 1 2 0 3 1 0 2 For evaluation, we define a stack of element-type (say integers/float, etc). The overall algorithm is as follows: (a) Read next input; (b) If input is end then top of stack contains result; (c) If input is operand, push value into stack; (d) If Input is operator, pop out (remove) the top two elements from the stack, perform the operation and push the resultant value into the stack;

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Postfix Evaluation: Example 1


Code Meaning 0 1 2 Operand Operator End

(12*4)+3 == 12,4,*,3,+ Input: 0 12 0 4 1 2 0 3 1 0 2

Op- Operation code 0 + 1 2 3 * /

12

4 12

48

3 48

51

10

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Postfix Evaluation: Example 2


2+4*5+3*2 == 2,4,5,*,+,3,2,*,+

4 2

5 4 2

20 2

22

3 22

2 3 22

6 22

28

11

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Recursion can be implemented as a stack

Fibonacci recurrence: fib(n) = 1 if n =0 or 1; = fib(n 2) + fib(n 1) otherwise;

fib (5) fib (3) fib (1) fib (0) fib (2) fib (1) fib (2) fib (0) fib (4) fib (3) fib (2) fib (1)

fib (1) fib (1) fib (0)

12

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Fibonacci recursion stack


3 4 1 2 4 2 4 0 1 4 1 4 2 3 0 1 3

1 3

1 2

0 1

4
13

6
27-03-03

8
P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

14

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

15

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

16

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Tower of Hanoi

17

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Towers of Hanoi function


void towers (int n, char from, char to, char aux) { /* Base Condition */ if (n==1) { printf (Disk 1 : %c -> %c \n, from, to) ; return ; } /* Recursive Condition */ towers (n-1, from, aux, to) ; printf (Disk %d : %c -> %c\n, n, from, to) ; towers (n-1, aux, to, from) ; }

18

27-03-03

P.P.Chakrabarti, IIT Kharagpur

TOH recursion stack


1,A,B,C A to C 1,B,C,A A to B 2,C,B,A A to B A to C 1,B,C,A A to B 2,C,B,A A to C 1,B,C,A A to B 2,C,B,A

3,A,B,C

2,A,C,B A to B 2,C,B,A

1,B,C,A A to B 2,C,B,A

B to C A to B 2,C,B,A

A to B 2,C,B,A
27-03-03

2,C,B,A

1,C,A,B C to B 1,A,B,C
P.P.Chakrabarti, IIT Kharagpur

19

The Queue ADT


A Stack is a First-in-First-Out (FIFO) list with the following interface functions: create: makes a new queue of a given size in case of a bounded queue dispose: destroys a queue make_empty: makes queue empty enqueue: inserts an element at the rear dequeue: removes the element in front isempty: determines if the queue has no elements isfull: determines if the queue is full in case of a bounded sized stack

REAR
Enqueue

Dequeue

FRONT

20

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Possible Implementations
Linear Arrays:
(static/dynamicaly allocated)

Circular Arrays:
(static/dynamicaly allocated)

front

rear front rear

Linked Lists: Use a linear


linked list with insert_rear and delete_front operations Can be implemented by a one-D array using modulus operations.

21

27-03-03

P.P.Chakrabarti, IIT Kharagpur

Exercises
Implement the Queue as an array. (Read the concept of a circular queue). Write down the data definition and all the interface functions. Implement the Queue as a linked list. Implement a Priority Queue which maintains the items in an order (ascending/ descending) and has additional functions like remove_max and remove_min. Maintain a Doctors appointment list
22
27-03-03 P.P.Chakrabarti, IIT Kharagpur

You might also like