Dynamic 2D arrays - PowerPoint PPT Presentation

1 / 22
About This Presentation
Title:

Dynamic 2D arrays

Description:

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 ... Exercises. Implement the Queue as an array. ( Read the concept of a ... – PowerPoint PPT presentation

Number of Views:70
Avg rating:3.0/5.0
Slides: 23
Provided by: facwebIit
Category:
Tags: arrays | dynamic

less

Transcript and Presenter's Notes

Title: Dynamic 2D arrays


1
Dynamic 2D arrays
2
2D Arrays using arrays of pointers
define N 20 define M 10 main() char
wordN, wM int i, n scanf("d",n)
for (i0 iltn i) scanf("s", word)
wi (char ) malloc ((strlen(word)1)sizeof(c
har)) strcpy (wi, word) for (i0
iltn i) printf("wd s \n",i,wi)

3
How it will look like
w
0
1
2
3
9
4
2D Arrays using a pointer to a pointer
define N 20 main() char wordN, w /
w is a pointer to a pointer array / int
i, n scanf("d",n) w (char ) malloc (n
sizeof(char )) for (i0 iltn i)
scanf("s", word) wi (char ) malloc
((strlen(word)1) sizeof(char)) strcpy
(wi, word) for (i0 iltn i)
printf("wd s \n",i,wi)

5
How this will look like
w
0
1
2
3
4
6
2D Array of n rows and constant columnsSingle
pointer memory chunk
int (r)COLS, rows, scanf("d",rows) r
(int ()COLS)malloc(rowsCOLSsizeof(int)) Alt
ernatively r (int ()COLS)malloc(rowssizeof(
(int()COLS) )

Dynamically allocated memory
r
r10
r00
7
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(rowsCOLSsizeof(int)) for
(i0iltrowsi) for(j0 jlt COLS j)
rij 2i3j for (i0iltrowsi)
for(j0 jlt COLS j) printf("d, ",
rij) printf("\n")

scd ./a.out 5 0, 3, 6, 2, 5, 8, 4, 7,
10, 6, 9, 12, 8, 11, 14,
8
The Stack ADT
Pop
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

9
Postfix Evaluation
  • Input format code, value sequence
  • Example 1243 is coded in postfix in the form
    of abc 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

10
Postfix Evaluation Example 1
  • (124)3 12,4,,3,
  • Input 0 12 0 4 1 2 0 3 1 0 2

11
Postfix Evaluation Example 2
  • 24532 2,4,5,,,3,2,,

12
Fibonacci recurrence fib(n) 1 if n 0 or 1
fib(n 2) fib(n 1)
otherwise
Recursion can be implemented as a stack
fib (5)
fib (3)
fib (4)
fib (2)
fib (1)
fib (2)
fib (3)
fib (1)
fib (2)
fib (0)
fib (1)
fib (1)
fib (0)
fib (1)
fib (0)
13
Fibonacci recursion stack
0
0
0
1
1
2
3
3
3

4
5
5
6
6
7
8
14
Tower of Hanoi
A
B
C
15
Tower of Hanoi
A
B
C
16
Tower of Hanoi
A
B
C
17
Tower of Hanoi
A
B
C
18
Towers of Hanoi function
void towers (int n, char from, char to, char aux)
/ Base Condition / if (n1)
printf (Disk 1 c -gt c \n, from, to)
return / Recursive Condition /
towers (n-1, from, aux, to)
printf (Disk d c -gt c\n, n, from, to)
towers (n-1, aux, to, from)
19
TOH recursion stack
20
The Queue ADT
REAR
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
  • isfull determines if the queue is full in case
    of a
  • bounded sized stack

Dequeue
FRONT
21
Possible Implementations
Linear Arrays (static/dynamicaly allocated)

Circular Arrays (static/dynamicaly
allocated) Can be implemented by a one-D
array using modulus operations.
front
rear
front
rear
Linked Lists Use a linear linked list with
insert_rear and delete_front operations
22
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
Write a Comment
User Comments (0)
About PowerShow.com