Вы находитесь на странице: 1из 22

Dynamic 2D arrays,

Stacks, Queues

P. P. Chakrabarti

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

2 27-03-03 P.P.Chakrabarti, IIT Kharagpur


How it will look like
w
0 T e n d u l k a r \0
1 S o u r a v \0

2 K h a n \0

3 I n d i a \0

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

4 27-03-03 P.P.Chakrabarti, IIT Kharagpur


How this will look like
w
0 I n d i a \0
1 A u s t r a l i a \0
2 K e n y a \0
3 N e w Z e a l a n d \0
4 S r i L a n k a \0

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

Dynamically allocated memory


r

r[0][0]

6 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++) [ppchak]$ ./a.out
for(j=0; j< COLS; j++) r[i][j] = 2*i+3*j; 5
for (i=0;i<rows;i++) 0, 3, 6,
{ for(j=0; j< COLS; j++) printf("%d, ", r[i][j]); 2, 5, 8,
4, 7, 10,
printf("\n");
6, 9, 12,
} 8, 11, 14,
}

7 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Pop
The Stack ADT
Push
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

8 27-03-03 P.P.Chakrabarti, IIT Kharagpur


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

9 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Postfix Evaluation: Example 1
(12*4)+3 == 12,4,*,3,+
Code Meaning Input: 0 12 0 4 1 2 0 3 1 0 2
0 Operand
1 Operator
2 End

Op- Operation
code
4 3
0 + 12 12 48 48 51
1 -
2 *
3 /

10 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Postfix Evaluation: Example 2

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

5 2
4 4 20 3 3 6
2 2 2 2 22 22 22 22 28

11 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Fibonacci recurrence:
Recursion can be fib(n) = 1 if n =0 or 1;
implemented as a stack
= fib(n – 2) + fib(n – 1)
otherwise;
fib (5)

fib (3) fib (4)

fib (1) fib (2) fib (2) fib (3)

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

fib (0) fib (1)

12 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Fibonacci recursion stack

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

1 1 0
3 3 2 2 1 1
4 5 5 6 6 7 8

13 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Tower of Hanoi

A B C

14 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Tower of Hanoi

A B C

15 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Tower of Hanoi

A B C

16 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Tower of Hanoi

A B C

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 B
A to C A to C A to C
2,A,C,B 1,B,C,A 1,B,C,A 1,B,C,A
A to B A to B A to B A to B
3,A,B,C 2,C,B,A 2,C,B,A 2,C,B,A 2,C,B,A

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

19 27-03-03 P.P.Chakrabarti, IIT Kharagpur


REAR
The Queue ADT Enqueue
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
Dequeue
• isfull: determines if the queue is full in case of a
bounded sized stack FRONT

20 27-03-03 P.P.Chakrabarti, IIT Kharagpur


Possible Implementations
Linear Arrays: Circular Arrays:
(static/dynamicaly allocated) (static/dynamicaly allocated)

front rear
front
rear
Linked Lists: Use a linear
linked list with insert_rear Can be implemented by a one-D
and delete_front operations 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 Doctor’s appointment list
22 27-03-03 P.P.Chakrabarti, IIT Kharagpur

Вам также может понравиться