Summary Lecture Exercises Programming Exercises

J.E.D.I int totalSize = 0; double freecells, incr = 0; double alpha, beta, sigma=0, tau=0; Compute for the allocation factors for int j=0; jm; j++{ size[j] = T[j]-B[j]; if T[j]-oldT[j] 0 diff[j] = T[j]-oldT[j]; else diff[j] = 0; totalSize += size[j]; incr += diff[j]; } diff[i]++; size[i]++; totalSize++; incr++; freecells = n - totalSize; alpha = 0.10 freecells m; beta = 0.90 freecells incr; If every stack is full if freecells 1 throw new StackExceptionStack overflow.; Compute for the new bases for int j=1; jm; j++{ tau = sigma + alpha + diff[j-1] beta; B[j] = B[j-1] + size[j-1] + int Math.floortau - int Math.floorsigma; sigma = tau; } Restore size of the overflowed stack to its old value size[i]--; Compute for the new top addresses for int j=0; jm; j++ T[j] = B[j] + size[j]; oldT = T; }

2.8 Summary

• A stack is a linearly ordered set of elements obeying the last-in, first-out LIFO principle • Two basic stack operations are push and pop • Stacks have two possible implementations – a sequentially allocated one- dimensional array vector or a linked linear list • Stacks are used in various applications such as pattern recognition, lists and tree traversals, and evaluation of expressions • Two or more stacks coexisting in a common vector results in better memory utilization • Memory reallocation techniques include the unit-shift method and Garwicks algorithm Data Structures 37 J.E.D.I

2.9 Lecture Exercises

1. Infix to Postfix. Convert the following expressions into their infix form. Show the stack. a a+bc+d-fgh b 12-5738+114+2 2. Convert the following expressions into their postfix form a a+bcde+f-gh b a-bcdefg+h-i c 42+156-3+748-2 d m+nopqrst+u-v Reallocation strategies for stack overflow. For numbers 3 and 4, a draw a diagram showing the current state of the stacks. b draw a diagram showing the state of the stacks after unit-shift policy is implemented. c draw a diagram showing the state of the stacks after the Garwicks algorithm is used. Show how the new base addresses are computed. 3. Five stacks coexist in a vector of size 500. An insertion is attempted at stack 2. The state of computation is defined by: OLDT0:4 = 89, 178, 249, 365, 425 T0:4 = 80, 220, 285, 334, 433 B0:5 = -1, 99, 220, 315, 410, 499 4. Three stacks coexist in a vector of size 300. An insertion is attempted at stack 3. The state of computation is defined by: OLDT0:2 = 65, 180, 245 T0:2 = 80, 140, 299 B0:3 = -1, 101, 215, 299

2.10 Programming Exercises

1. Write a Java program that checks if parentheses and brackets are balanced in an arithmetic expression. 2. Create a Java class that implements the conversion of well-formed infix expression into its postfix equivalent.

3. Implement the conversion from infix to postfix expression using linked

implementation of stack. The program shall ask input from the user and checks if the input is correct. Show the output and contents of the stack at every iteration. 4. Create a Java class definition of a multiple-stack in one dimensional vector. Data Structures 38 J.E.D.I Implement the basic operations on stack push, pop, etc to make them applicable on multiple-stack. Name the class MStack. 5. A book shop has bookshelves with adjustable dividers. When one divider becomes full, the divider could be adjusted to make space. Create a Java program that will reallocate bookshelf space using Garwicks Algorithm. Data Structures 39 J.E.D.I 3 Queues

3.1 Objectives