Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Size: px
Start display at page:

Download "Data Structures. Chapter 06. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU"

Transcription

1 Data Structures Chapter 06 1

2 Stacks A stack is a list of elements in which an element may be inserted or deleted only at one end, called the top of the stack. This means that elements are removed from a stack in the reverse order of that in which they were inserted into the stack. Two basic operations associated with stacks: Push is the term used to insert an element into a stack. Pop is the term used to delete an element from a stack. 2

3 Diagrams of Stacks 3

4 Procedure 6.1: Write a procedure that pushes an ITEM onto a stack. PUSH (STACK, TOP, MAXSTK, ITEM) This procedure pushes an ITEM onto a stack. 1. If TOP = MAXSTK, then: Print: OVERFLOW and Return. 2. Set TOP = TOP Set STACK[TOP] = ITEM. 4. Return. 4

5 Procedure 6.2: Write a procedure that deletes the top element of a stack. POP (STACK, TOP, ITEM) This procedure deletes the top element of STACK and assigns it to the variable ITEM. 1. If TOP = 0, then: Print: UNDERFLOW, and Return. 2. Set ITEM = STACK[TOP]. 3. Set TOP = TOP Return. 5

6 Solved Problem 6.1: Consider the following stack of characters, where STACK is allocated N=8 memory cells: STACK: A, C, D, F, K, -, -, - Describe the stack as the following operations take place: i) POP (STACK, ITEM) v) POP (STACK, ITEM) ii) POP (STACK, ITEM) vi) PUSH(STACK, R) iii) PUSH(STACK, L) vii) PUSH(STACK, S) iv) PUSH(STACK, P) Solution: viii) POP (STACK, ITEM) i) STACK: A, C, D, F, -, -, -, - v) STACK: A, C, D, L, -, -, -, - ii) STACK: A, C, D, -, -, -, -, - vi) STACK: A, C, D, L, R, -, -, - iii) STACK: A, C, D, L, -, -, -, - vii) STACK: A, C, D, L, R, S, -, - iv) STACK: A, C, D, L, P, -, -, - viii) STACK: A, C, D, L, R, -, -, - 6

7 ARITHMETIC EXPRESSIONS; POLISH NOTATION Infix notation: The operator symbol is placed between its two operands. Example: A+B, A-B, A*B, A/B Prefix notation: The operator symbol is placed before its two operands. Example: +AB, -AB, *AB, /AB This notation is also known as Polish notation, named after the Polish mathematician Jan Lukasiewicz. The fundamental property of Polish notation is that the order in which the operations are to be performed is completely determined by the positions of the operators and operands in the expression. Accordingly, one never needs parentheses when writing expressions in Polish notation. 7

8 ARITHMETIC EXPRESSIONS; POLISH NOTATION Postfix notation: The operator symbol is placed after its two operands. Example: AB+, AB-, AB*, AB/ This notation is also known as Reverse Polish notation. One never needs parentheses to determine the order of the operations in any arithmetic expression written in reverse Polish notation. The computer usually evaluates an arithmetic expression written in infix notation in two steps. First, it converts the expression to postfix notation, and then it evaluates the postfix expression. In each step, the stack is the main tool that is used to accomplish the given task. 8

9 Example 6.7: Transform the following arithmetic infix expression Q into its equivalent postfix expression P: Q: A + (B*C - (D /E F)*G)*H Solution: Push ( onto stack and then add ) to the end of Q. Thus, Q becomes Q: A + ( B * C - ( D / E F ) * G ) * H ) Start scanning. The following table shows the status of STACK and of the string P as each element of Q is scanned. 9

10 Example 6.7: Transform the following arithmetic infix expression Q into its equivalent postfix expression P: Q: A + (B*C - (D /E F)*G)*H) Solution: Symbol Scanned STACK Expression, P A ( A + ( + A ( ( + ( A B ( + ( A B * ( + ( * A B C ( + ( * A B C - ( + ( - A B C * ( ( + ( - ( A B C * 10

11 Example 6.7: Transf orm..... postfix expression P: Q: A + (B*C - (D /E F)*G)*H) Symbol Scanned STACK Expression, P D ( + ( - ( A B C * D / ( + ( - ( / A B C * D E ( + ( - ( / A B C * D E ( + ( - ( / A B C * D E F ( + ( - ( / A B C * D E F ) ( + ( - A B C * D E F / * ( + ( - * A B C * D E F / G ( + ( - * A B C * D E F / G ) ( + A B C * D E F / G * - * ( + * A B C * D E F / G * - H ( + * A B C * D E F / G * - H ) A B C * D E F / G * - H * + 11

12 Solved Problem 6.10: Transform the following arithmetic infix expression Q into its equivalent postfix expression P: Q: ((A + B)*D) (E-F) Solution: Push ( onto stack and then add ) to the end of Q. Thus, Q becomes Q: ((A + B) * D) (E-F) ) Start scanning. The following table shows the status of STACK and of the string P as each element of Q is scanned. 12

13 Solved Problem 6.10: Transform... postfix expression P: Q: ((A + B)*D) (E-F)) Symbol Scanned STACK Expression, P ( ( ( ( ( ( ( A ( ( ( A + ( ( ( + A B ( ( ( + A B ) ( ( A B + * ( ( * A B + D ( ( * A B + D ) ( A B + D * ( A B + D * ( ( ( A B + D * E ( ( A B + D * E - ( ( - A B + D * E F ( ( - A B + D * E F ) ( A B + D * E F - ) A B + D * E F - 13

14 Algorithm 6.6: Write an algorithm that transforms the infix expression into its equivalent postfix expression. POLISH (Q, P) Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent postfix expression P. 1. Push ( onto STACK and add ) to the end of Q. 2. Scan Q from left to right and repeat steps 3 to 6 for each element of Q until the STACK is empty. 3. If an operand is encountered, add it to P. 4. If a left parenthesis is encountered, push it onto STACK. 14

15 Algorithm 6.6: Write an algorithm that transforms the infix expression into its equivalent postfix expression. 5. If an operator is encountered, then (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) which has the same precedence as or higher precedence than. (b) Add to STACK. [End of if structure] 6. If a right parenthesis is encountered, then (a) Repeatedly pop from STACK and add to P each operator (on the top of STACK) until a left parenthesis is encountered. (b) Remove the left parenthesis. [End of if structure] [End of Step 2 loop] 3/10/ Exit. Md. Golam Moazzam, Dept. of CSE, JU 15

16 Example 6.6: Find the value of the following arithmetic expression P written in postfix notation: P: 5, 6, 2, +, *, 12, 4, /, - Solution: - First we add a sentinel right parenthesis at the end of P: P: 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) - Start scanning from left to right. The following table shows the contents of STACK as each element of P is scanned. The final number in STACK, 37, which is assigned to VALUE when the sentinel ")" is scanned, is the value of P. 16

17 Example 6.6: Find the value of the following arithmetic expression P written in postfix notation: P: 5, 6, 2, +, *, 12, 4, /, -, ) (1) (2) (3) (4) (5) (6) (7) (8) (9) (10) Solution: Symbol Scanned (1) 5 (2) 6 (3) 2 (4) + (5) * (6) 12 (7) 4 (8) / (9) - (10) ) 5 5, 6 5, 6, 2 5, , 12 40, 12, 4 40, 3 37 STACK 17

18 Algorithm 6.5: Write an algorithm that finds the value of an arithmetic expression written in postfix notation. This algorithm finds the VALUE of an arithmetic expression P written in postfix notation. 1. Add a right parenthesis ) at the end of P. 2. Scan P from left to right and repeat Steps 3 and 4 for each element of P until the sentinel ")" is encountered. 3. If an operand is encountered, put it on STACK. 4. If an operator is encountered, then: a) Remove the two top elements of STACK, where A is the top element and B is the next-to-top element. b) Evaluate B A. c) Place the result of (b) back on STACK. [End of If structure] [End of Step 2 loop.] 5. Set VALUE equal to the top element on STACK. 6. Exit. 18

19 Quicksort, An Application of Stacks The following numbers are stored in an array A: A: 44, 33, 11, 55, 77, 90, 40, 60, 99, 22, 88, 66 Now apply Quick sort algorithm to find the final position of the first number. Solution: A: Two Stacks for keeping array boundary values LOWER UPPER 19

20 Quicksort, An Application of Stacks Consider the list: Lower boundary=1 and Upper boundary= 12 Start scanning from right to left. Stop at the first number less than 44 and interchange LOWER UPPER LOWER UPPER 20

21 Quicksort, An Application of Stacks Start scanning from left to right. Stop at the first number greater than 44 and interchange LOWER UPPER LOWER UPPER 21

22 Quicksort, An Application of Stacks Sublist 1 44 is in its final Sublist 2 position. LOWER UPPER Push the boundary values of each sub-list onto the stacks. 6 1 LOWER 12 4 UPPER 22

23 Quicksort, An Application of Stacks Consider the sub-list: LOWER=6 and UPPER= 12 Start scanning from right to left. Stop at the first number less than 90 and interchange LOWER UPPER Scan from left to right. Scan from right to left. Scan from left to right is in its Sublist final position. 23

24 Quicksort, An Application of Stacks Sublist 90 is in its final position. 1 4 LOWER UPPER Push the boundary values of the sub-list onto the stacks. 6 1 LOWER 10 4 UPPER 24

25 Quicksort, An Application of Stacks Sublist Consider the sub-list: LOWER=6 and UPPER= 10 Repeat the above steps is in its final position LOWER 10 4 UPPER LOWER UPPER

26 Quicksort, An Application of Stacks Sublist1 66 is in its final position. Sublist2 1 4 LOWER UPPER Push the boundary values of each sub-list onto the stacks LOWER UPPER 26

27 Quicksort, An Application of Stacks Sublist1 66 is in its final position. Sublist2 6 1 LOWER UPPER Consider the sub-list: LOWER=9 and UPPER= 10 Repeat the above steps LOWER 7 4 UPPER 88 is in its final position. 27

28 Quicksort, An Application of Stacks Consider the sub-list: LOWER=6 and UPPER=7 Repeat the above steps LOWER 7 4 UPPER LOWER UPPER 55 is in its final position. 28

29 Quicksort, An Application of Stacks LOWER UPPER Consider the sub-list: LOWER=1 and UPPER=4 Repeat the above steps LOWER UPPER 29

30 Quicksort, An Application of Stacks is in its final position. Sublist LOWER UPPER Push the boundary values of the sub-list onto the stacks. 3 4 LOWER UPPER 30

31 Quicksort, An Application of Stacks LOWER UPPER Consider the sub-list: LOWER=3 and UPPER=4 Repeat the above steps is in its final position. LOWER UPPER 31

32 Quicksort, An Application of Stacks Now the stacks are empty. That is, no sub-lists exist. LOWER UPPER Finally, the list becomes: A:

33 Complexity of the Quicksort Algorithm i) Average case: O(n log n) ii) Worst case: O(n log n) f(n) = n + (n-1) = n (n+1) / 2 = O(n 2 ) 33

34 Solved Problem 6.12: Suppose S is the following list of 14 alphabetic characters: S: D A T A S T R U C T U R E S Use the Quick sort algorithm to find the final position of the first character D D A T A S T R U C T U R E S C A T A S T R U D T U R E S C A D A S T R U T T U R E S C A A D S T R U T T U R E S D is in its final position. 34

35 Recursion A procedure is called a recursive procedure if it contains a Call statement to itself. A recursive procedure must have the following two properties: There must be certain criteria, called base criteria, for which the procedure does not call itself. Each time the procedure does call itself, it must be closer to the base criteria. 35

36 Recursion Example 6.9: Calculate 4! using the recursive definition. Solution: This calculation requires the following nine steps: 1) 4! = 4. 3! 2) 3! = 3. 2! 3) 2! = 2. 1! 4) 1! = 1. 0! 5) 0! = 1 6) 1! = 1. 1 = 1 7) 2! = 2. 1 =2 8) 3! = 3. 2 = 6 9) 4!=4. 6 = 24 36

37 Procedure 6.9A: Write a procedure that calculates N! FACTORIAL(FACT, N) This procedure calculates N! and returns the value in the variable FACT. 1. If N = 0, then: Set FACT := 1, and Return. 2. Set FACT := Repeat for K = 1 to N. Set FACT := K*FACT. [End of loop.] 4. Return. 37

38 Procedure 6.9B: Write a recursive procedure that calculates N! FACTORIAL(FACT, N) This procedure calculates N! and returns the value in the variable FACT. 1. If N = 0, then: Set FACT := 1, and Return. 2. Call FACTORIAL(FACT, N 1). 3. Set FACT := N*FACT. 4. Return. 38

39 Fibonacci Sequence The Fibonacci sequence (usually denoted by F 0, F 1, F 2,..) is as follows: 0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55,... That is, F 0 = 0 and F 1 = 1 and each succeeding term is the sum of the two preceding terms. Definition: (Fibonacci Sequence) a) If n = 0 or n = 1, then F n = n. b) If n > 1, then F n = F n-2 + F n-1 39

40 Fibonacci Sequence Procedure 6.10: Write a recursive procedure that finds the nth Fibonacci number. FIBONACCI(F1B, N) This procedure calculates F N and returns the value in the first parameter FIB. 1. If N = 0 or N = 1, then: Set FIB := N, and Return. 2. Call FIBONACCI(F1BA, N - 2). 3. Call FIBONACCI(FIBB, N - 1). 4. Set FIB := F1BA + FIBB. 5. Return. 40

41 Ackermann Function The Ackermann function is a function with two arguments each of which can be assigned any nonnegative integer: 0, 1, 2,... This function is defined as follows: Definition 6.3: (Ackermann Function) a) If m = 0, then A(m, n) = n + 1. b) If m 0 but n = 0, then A(m, n) = A(m - 1, 1). c) If m 0 and n 0, then A(m, n) = A(m - 1, A(m, n - 1)) 41

42 Solved Problem 6.18: Define Ackermann function. Find the value of A(1, 3). 1) A(1, 3) = A(0, A(1, 2)) 2) A(1, 2) = A(0, A(1, 1)) 3) A(1, 1) = A(0, A(1, 0)) 4) A(1, 0) = A(0, 1) 5) A(0, 1) = 1+1 = 2 6) A(1, 0) = 2 7) A(1, 1) = A(0, 2) 8) A(0, 2) = =3 9) A(1, 1) = 3 10) A(1, 2) = A(0, 3) 11) A(0, 3) = = 4 12) A(1, 2) = 4 13) A(1, 3) = A(0, 4) 14) A(0, 4) = = 5 15) A(1, 3) = 5 42

43 Solved Problem 6.15: Let a and b denote positive integers. Suppose a function Q is defined recursively as follows: Solution: 0 Q( a, b) Q( a b, b) 1 i) Find the value of Q(2, 3) and Q(14, 3). ii) What does this function do? Find Q(5861, 7). i) Q(2, 3) = 0, Since 2 < 3. Q(14, 3) = Q(11, 3) + 1 = [Q(8, 3) + 1] + 1 = Q(8, 3) + 2 = [Q(5, 3) + 1] + 2 = Q(5, 3) + 3 = [Q(2, 3) + 1] + 3 = Q(2, 3) + 4 = = 4 if a b if b a 43

44 Solved Problem 6.15: Let a and b denote positive integers. Suppose a function Q is defined recursively as follows: 0 Q( a, b) Q( a b, b) 1 if a b if b a i) Find the value of Q(2, 3) and Q(14, 3). ii) What does this function do? Find Q(5861, 7). Solution: ii) Each time b is subtracted from a and the values of Q is increased by 1. Hence Q(a, b) finds the quotient when a is divided by b. Thus, Q(5861, 7) =

45 Solved Problem 6.16: Let n denote a positive integer. Suppose a function L is defined recursively as follows: 0 L( n) L( i) Find L(25). n / 2 ii) What does this function do? 1) if if n 1 n 1 Solution: i) L(25) = L(12) + 1 = [L(6) + 1] + 1 = L(6) + 2 = [L(3) + 1] + 2 = L(3) + 3 = [L(1) + 1] + 3 = L(1) + 4 = = 4 45

46 Solved Problem 6.16: Let n denote a positive integer. Suppose a function L is defined recursively as follows: 0 L( n) L( i) Find L(25). n / 2 ii) What does this function do? 1) if if n 1 n 1 Solution: ii) Each time n is divided by 2 and the value of L is increased by 1. Hence L is the greatest integer such that 2 L n Accordingly, the function finds L log 2 n 46

47 Towers of Hanoi Consider three pegs labeled A, B, and C. Suppose on peg A, there are placed a finite number n of disks with decreasing size. The object of the game is to move the disks from peg A to peg C using peg B as an auxiliary. The rules of the game are as follows: Only one disk may be moved at a time. Specially, only the top disk on any peg may be moved to any other peg. At no time can a larger disk be placed on a smaller disk. 47

48 Draw a schematic diagram of the recursive solution to Towers of Hanoi problem for 1 disk. OR, How many moves will it take to transfer 1 disk from the left post to the right post? Solution: 1 disk: 1 move Move 1: A C 48

49 Draw a schematic diagram of the recursive solution to Towers of Hanoi problem for 2 disks. OR, How many moves will it take to transfer 2 disks from the left post to the right post? Solution: No. of moves= 2 n -1 = = 3 Moves: A B A C and B C 49

50 Draw a schematic diagram of the recursive solution to Towers of Hanoi problem for 3 disks. OR, How many moves will it take to transfer 3 disks from the left post to the right post? Solution: No. of moves= 2 n -1 = = 7 Moves: A C, A B, C B, A C, B A, B C, and A C 50

51 Towers of Hanoi problem: Recursive Solution Procedure 6.11: Write a recursive procedure that gives a solution to the Tower of Hanoi problem for N disks. TOWER(N, BEG, AUX, END) This procedure gives a recursive solution to the Towers of Hanoi problem for N disks. 1. If N = 1, then: a) Write: BEG END. b) Return. [End of If structure.] 2. Call TOWER(N - 1, BEG, END, AUX). 3. Write: BEG END. 4. Call TOWER(N - 1, AUX, BEG, END). 5. Return. 51

52 Draw a schematic diagram of the recursive solution to Towers of Hanoi problem for n = 4 disks. 52

53 Draw a schematic diagram of the recursive solution to Towers of Hanoi problem for n = 4 disks. Solution: No. of moves= 2 n -1 = = 15 Moves: A B A C B C A B C A C B A B A C B C B A C A B C A B A C B C 53

54 Towers of Hanoi: Applications Frequently used in psychological research on problem solving. There also exists a variant of this task called Tower of London for neuropsychological diagnosis and treatment of executive functions. Also used as Backup rotation scheme when performing computer data Backups where multiple tapes/media are involved. Tower of Hanoi is popular for teaching recursive algorithms to beginning programming students. Tower of Hanoi is also used as a test by neuropsychologists trying to evaluate frontal lobe (The frontal lobes are considered our emotional control center and home to our personality. )deficits. 54

55 Queues A queue is a linear list of elements in which deletions can take place only at one end, called the front, and insertions can take place only at the other end, called the rear. Queues are also called first-in first-out (FIFO) lists, since the first element in a queue will be the first element out of the queue. In other words, the order in which elements enter a queue is the order in which they leave. Example: A line of people waiting at a bus stop form a queue, where the first person in line is the first person to board the bus. A line of people waiting to be served by an ATM machine. Collection of documents sent to a shared printer. 55

56 Representation of Queues Queues may be represented in the computer by a linear array QUEUE and two pointer variables: FRONT, containing the location of the front element of the queue; and REAR, containing.the location of the rear element of the queue. FRONT = NULL indicates that the queue is empty. The following Figure indicates the way elements will be deleted from the queue and the way new elements will be added to the queue. Whenever an element is deleted from the queue, the value of FRONT is increased by 1. FRONT := FRONT + 1 Similarly, whenever an element is added to the queue, the value of REAR is increased by 1. REAR := REAR

57 Representation of Queues 57

58 Procedure 6.13: Write a procedure that inserts an ITEM onto a queue. QINSERT (QUEUE, N, FRONT, REAR, ITEM) This procedure inserts an element ITEM into a queue. 1. If FRONT = 1 and REAR = N, or if FRONT = REAR + 1, then Write: OVERFLOW, and Return. 2. If FRONT = NULL, then Set FRONT = 1 and REAR = 1 Else if REAR = N, then Set REAR = 1 Else Set REAR = REAR + 1 [End of If structure.] 3. Set QUEUE [REAR] = ITEM 4. Return. 58

59 Procedure 6.14: Write a procedure that deletes an element from a queue. QDELETE(QUEUE, N, FRONT, REAR, ITEM) This procedure deletes an element from a queue and assigns it to the variable ITEM. 1. If FRONT:= NULL, then: Write: UNDERFLOW, and Return. 2. Set ITEM:= QUEUE[FRONT]. 3. If FRONT= REAR, then: Set FRONT := NULL and REAR := NULL. Else if FRONT = N, then: Else: Set FRONT := 1. Set FRONT := FRONT + 1 [End of If structure.] 4. Return. 59

60 Solved Problem 6.22: Consider the following queue of characters, where QUEUE is a circular array which is allocated six memory cells: FRONT=2, REAR=4 QUEUE: -, A, C, D, -, - Describe the queue as the following operations take place: i) F is added to the queue. vi) Two letters are deleted. ii) Two letters are deleted. iii) K, L, and M are added to the queue. iv) Two letters are deleted. vii) S is added to the queue. viii) Two letters are deleted. ix) One letter is deleted. v) R is added to the queue. x) One letter is deleted. Solution: i) FRONT=2, REAR=5 QUEUE: -, A, C, D, F, - ii) FRONT=4, REAR=5 QUEUE: -, -, -, D, F, - iii) FRONT=4, REAR=2 QUEUE: L, M, -, D, F, K 60

61 Solved Problem 6.22: Consider the following queue of characters, where QUEUE is a circular array which is allocated six memory cells: FRONT=2, REAR=4 QUEUE: -, A, C, D, -, - Describe the queue as the following operations take place: i) F is added to the queue. vi) Two letters are deleted. ii) Two letters are deleted. iii) K, L, and M are added to the queue. iv) Two letters are deleted. vii) S is added to the queue. viii) Two letters are deleted. ix) One letter is deleted. v) R is added to the queue. x) One letter is deleted. Solution: iv) FRONT=6, REAR=2 QUEUE: L, M, -, -, -, K v) FRONT=6, REAR=3 QUEUE: L, M, R, -, -, K vi) FRONT=2, REAR=3 QUEUE: -, M, R, -, -, - 61

62 Solved Problem 6.22: Consider the following queue of characters, where QUEUE is a circular array which is allocated six memory cells: FRONT=2, REAR=4 QUEUE: -, A, C, D, -, - Describe the queue as the following operations take place: ii) Two letters are deleted. iii) K, L, and M are added to the queue. iv) Two letters are deleted. vii) S is added to the queue. viii) Two letters are deleted. ix) One letter is deleted. v) R is added to the queue. x) One letter is deleted. Solution: vii) FRONT=2, REAR=4 QUEUE: -, M, R, S, -, - viii) FRONT=4, REAR=4 QUEUE: -, -, -, S, -, - ix) FRONT=0, REAR=0 QUEUE: -, -, -, -, -, - x) Queue is empty. Underflow has occurred. 62

63 DEQUES A deque is a linear list in which elements can be added or removed at either end but not in the middle. The term deque is a contraction of the name double-ended queue. Deque is maintained in memory by a circular array DEQUE with pointers LEFT and RIGHT, which point to the two ends of the deque. Elements extend from the left end to the right end in the array. Figure pictures two deques, each with 4 elements maintained in an array with N = 8 memory locations. LEFT = NULL indicates that a deque is empty. 63

64 DEQUES 64

65 DEQUES There are two variations of a deque: An input-restricted deque An input-restricted deque is a deque which allows insertions at only one end of the list but allows deletions at both ends of the list. An output-restricted deque An output-restricted deque is a deque which allows deletions at only one end of the list but allows insertions at both ends of the list. 65

66 Solved Problem 6.24: Consider the following deque of characters, where DEQUE is a circular array which is allocated six memory cells: LEFT=2, RIGHT=4 DEQUE: -, A, C, D, -, - Describe the deque as the following operations take place: i) F is added to the right of the deque. ii) Two letters on the right are deleted. iii) K, L, and M are added to the left of the deque. iv) One letter on the left is deleted. v) R is added to the left of the deque. vi) S is added to the right of the deque. vii) T is added to the right of the deque. 66

67 Solved Problem 6.24:... LEFT=2, RIGHT=4 DEQUE: -, A, C, D, -, - i) F is added to the right of the deque. ii) Two letters on the right are deleted. iii) K, L, and M are added to the left of the deque. iv) One letter on the left is deleted. v) R is added to the left of the deque. Solution: i) LEFT=2, RIGHT=5 DEQUE: -, A, C, D, F, - ii) LEFT=2, RIGHT=3 DEQUE: -, A, C, -, -, - iii) LEFT=5, RIGHT=3 DEQUE: K, A, C, -, M, L iv) LEFT=6, RIGHT=3 DEQUE: K, A, C, -, -, L v) LEFT=5, RIGHT=3 DEQUE: K, A, C, -, R, L 67

68 Solved Problem 6.24:... LEFT=5, RIGHT=3 DEQUE: K, A, C, -, R, L vi) S is added to the right of the deque. vii) T is added to the right of the deque. Solution: vi) LEFT=5, RIGHT=4 DEQUE: K, A, C, S, R, L vii) Since LEFT = RIGHT + 1, the deque is full. That is, OVERFLOW has occurred. 68

69 Priority Queues A priority queue is a collection of elements such that each element has been assigned a priority and such that the order in which the elements are deleted and processed comes from the following rules: An element of higher priority is processed before any element of lower priority. Two elements with the same priority are processed according to the order in which they were added to the queue. 69

70 One-Way List Representation of a Priority Queues One way to maintain a priority queue in memory is by means of a oneway list as follows: a) Each node contains three items of information: An information field INFO, a priority number PRN and a link number LINK. b) A node X proceeds a node Y in the list: When X has higher priority than Y OR When both have the same priority but X was added to the list before Y. 70

71 One-Way List Representation of a Priority Queues Start PRN AAA 1 BBB 2 CCC 2 DDD 4 EEE 5 FFF 6 71

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1

Some Applications of Stack. Spring Semester 2007 Programming and Data Structure 1 Some Applications of Stack Spring Semester 2007 Programming and Data Structure 1 Arithmetic Expressions Polish Notation Spring Semester 2007 Programming and Data Structure 2 What is Polish Notation? Conventionally,

More information

DEEPIKA KAMBOJ UNIT 2. What is Stack?

DEEPIKA KAMBOJ UNIT 2. What is Stack? What is Stack? UNIT 2 Stack is an important data structure which stores its elements in an ordered manner. You must have seen a pile of plates where one plate is placed on top of another. Now, when you

More information

INSTITUTE OF AERONAUTICAL ENGINEERING

INSTITUTE OF AERONAUTICAL ENGINEERING INSTITUTE OF AERONAUTICAL ENGINEERING (Autonomous) Dundigal, Hyderabad - 500 043 COMPUTER SCIENCE AND ENGINEERING TUTORIAL QUESTION BANK Course Name Course Code Class Branch DATA STRUCTURES ACS002 B. Tech

More information

Adapted By Manik Hosen

Adapted By Manik Hosen Adapted By Manik Hosen Question: What is Data Structure? Ans: The logical or mathematical model of particular organization of data is called Data Structure. Question: What are the difference between linear

More information

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3

EC8393FUNDAMENTALS OF DATA STRUCTURES IN C Unit 3 UNIT 3 LINEAR DATA STRUCTURES 1. Define Data Structures Data Structures is defined as the way of organizing all data items that consider not only the elements stored but also stores the relationship between

More information

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion.

Solution: The examples of stack application are reverse a string, post fix evaluation, infix to postfix conversion. 1. What is the full form of LIFO? The full form of LIFO is Last In First Out. 2. Give some examples for stack application. The examples of stack application are reverse a string, post fix evaluation, infix

More information

Content: Learning Objectives

Content: Learning Objectives 1 BLOOM PUBLIC CHOOL Vasant Kunj, New Delhi Lesson Plan Class: XII ubject: Computer cience Month : July No of s: 21 Chapter:Data structure: Linked List TTT: 8 WT: 12 Content: Learning Objectives At the

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours TED (10)-3071 Reg. No.. (REVISION-2010) (Maximum marks: 100) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- MARCH, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours PART

More information

Lecture Data Structure Stack

Lecture Data Structure Stack Lecture Data Structure Stack 1.A stack :-is an abstract Data Type (ADT), commonly used in most programming languages. It is named stack as it behaves like a real-world stack, for example a deck of cards

More information

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE

FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE TED (10)-3071 Reg. No.. (REVISION-2010) Signature. FORTH SEMESTER DIPLOMA EXAMINATION IN ENGINEERING/ TECHNOLIGY- OCTOBER, 2012 DATA STRUCTURE (Common to CT and IF) [Time: 3 hours (Maximum marks: 100)

More information

CS W3134: Data Structures in Java

CS W3134: Data Structures in Java CS W3134: Data Structures in Java Lecture #10: Stacks, queues, linked lists 10/7/04 Janak J Parekh HW#2 questions? Administrivia Finish queues Stack/queue example Agenda 1 Circular queue: miscellany Having

More information

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5

Stack. 4. In Stack all Operations such as Insertion and Deletion are permitted at only one end. Size of the Stack 6. Maximum Value of Stack Top 5 What is Stack? Stack 1. Stack is LIFO Structure [ Last in First Out ] 2. Stack is Ordered List of Elements of Same Type. 3. Stack is Linear List 4. In Stack all Operations such as Insertion and Deletion

More information

Formal Languages and Automata Theory, SS Project (due Week 14)

Formal Languages and Automata Theory, SS Project (due Week 14) Formal Languages and Automata Theory, SS 2018. Project (due Week 14) 1 Preliminaries The objective is to implement an algorithm for the evaluation of an arithmetic expression. As input, we have a string

More information

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Stack Operations on a stack Representing stacks Converting an expression

More information

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows:

STACKS. A stack is defined in terms of its behavior. The common operations associated with a stack are as follows: STACKS A stack is a linear data structure for collection of items, with the restriction that items can be added one at a time and can only be removed in the reverse order in which they were added. The

More information

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 )

ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) ADVANCED DATA STRUCTURES USING C++ ( MT-CSE-110 ) Unit - 2 By: Gurpreet Singh Dean Academics & H.O.D. (C.S.E. / I.T.) Yamuna Institute of Engineering & Technology, Gadholi What is a Stack? A stack is a

More information

17CS33:Data Structures Using C QUESTION BANK

17CS33:Data Structures Using C QUESTION BANK 17CS33:Data Structures Using C QUESTION BANK REVIEW OF STRUCTURES AND POINTERS, INTRODUCTION TO SPECIAL FEATURES OF C Learn : Usage of structures, unions - a conventional tool for handling a group of logically

More information

Postfix (and prefix) notation

Postfix (and prefix) notation Postfix (and prefix) notation Also called reverse Polish reversed form of notation devised by mathematician named Jan Łukasiewicz (so really lü-kä-sha-vech notation) Infix notation is: operand operator

More information

STACKS AND QUEUES. Problem Solving with Computers-II

STACKS AND QUEUES. Problem Solving with Computers-II STACKS AND QUEUES Problem Solving with Computers-II 2 Stacks container class available in the C++ STL Container class that uses the Last In First Out (LIFO) principle Methods i. push() ii. iii. iv. pop()

More information

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1

Prepared By: Ms. Nidhi Solanki (Assist. Prof.) Page 1 QUESTION BANK ON COURSE: 304: PRELIMINARIES: 1. What is array of pointer, explain with appropriate example? 2 2. Differentiate between call by value and call by reference, give example. 3. Explain pointer

More information

Linear Data Structure

Linear Data Structure Linear Data Structure Definition A data structure is said to be linear if its elements form a sequence or a linear list. Examples: Array Linked List Stacks Queues Operations on linear Data Structures Traversal

More information

Stacks and Queues. CSE Data Structures April 12, 2002

Stacks and Queues. CSE Data Structures April 12, 2002 Stacks and Queues CSE 373 - Data Structures April 12, 2002 Readings and References Reading Section 3.3 and 3.4, Data Structures and Algorithm Analysis in C, Weiss Other References 12-Apr-02 CSE 373 - Data

More information

End-Term Examination Second Semester [MCA] MAY-JUNE 2006

End-Term Examination Second Semester [MCA] MAY-JUNE 2006 (Please write your Roll No. immediately) Roll No. Paper Code: MCA-102 End-Term Examination Second Semester [MCA] MAY-JUNE 2006 Subject: Data Structure Time: 3 Hours Maximum Marks: 60 Note: Question 1.

More information

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms

Introduction. Problem Solving on Computer. Data Structures (collection of data and relationships) Algorithms Introduction Problem Solving on Computer Data Structures (collection of data and relationships) Algorithms 1 Objective of Data Structures Two Goals: 1) Identify and develop useful high-level data types

More information

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n))

ASSIGNMENTS. Progra m Outcom e. Chapter Q. No. Outcom e (CO) I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n)) ASSIGNMENTS Chapter Q. No. Questions Course Outcom e (CO) Progra m Outcom e I 1 If f(n) = Θ(g(n)) and g(n)= Θ(h(n)), then proof that h(n) = Θ(f(n)) 2 3. What is the time complexity of the algorithm? 4

More information

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero).

Where does the insert method place the new entry in the array? Assume array indexing starts from 0(zero). Suppose we have a circular array implementation of the queue,with ten items in the queue stored at data[2] through data[11]. The current capacity of an array is 12. Where does the insert method place the

More information

Draw a diagram of an empty circular queue and describe it to the reader.

Draw a diagram of an empty circular queue and describe it to the reader. 1020_1030_testquestions.text Wed Sep 10 10:40:46 2014 1 1983/84 COSC1020/30 Tests >>> The following was given to students. >>> Students can have a good idea of test questions by examining and trying the

More information

CS6202 - PROGRAMMING & DATA STRUCTURES I Unit IV Part - A 1. Define Stack. A stack is an ordered list in which all insertions and deletions are made at one end, called the top. It is an abstract data type

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May www.jwjobs.net R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 *******-****** 1. a) Which of the given options provides the

More information

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION

09 STACK APPLICATION DATA STRUCTURES AND ALGORITHMS REVERSE POLISH NOTATION DATA STRUCTURES AND ALGORITHMS 09 STACK APPLICATION REVERSE POLISH NOTATION IMRAN IHSAN ASSISTANT PROFESSOR, AIR UNIVERSITY, ISLAMABAD WWW.IMRANIHSAN.COM LECTURES ADAPTED FROM: DANIEL KANE, NEIL RHODES

More information

DS Assignment II. Full Sized Image

DS Assignment II. Full Sized Image DS Assignment II 1. A) For the Towers of Hanoi problem, show the call tree during the recursive call Towers(3, A, C, B). In the tree, label the root node as Towers (3, A, C, B) while marking all the intermediate

More information

DATA STRUCTURES AND ALGORITHMS

DATA STRUCTURES AND ALGORITHMS LECTURE 8 Babeş - Bolyai University Computer Science and Mathematics Faculty 2017-2018 In Lecture 7... ADT Queue ADT Matrix ADT List ADT Stack Today ADT Queue 1 ADT Queue 2 3 4 Note ADT Queue We will not

More information

12 Abstract Data Types

12 Abstract Data Types 12 Abstract Data Types 12.1 Foundations of Computer Science Cengage Learning Objectives After studying this chapter, the student should be able to: Define the concept of an abstract data type (ADT). Define

More information

UNIT-II. Part-2: CENTRAL PROCESSING UNIT

UNIT-II. Part-2: CENTRAL PROCESSING UNIT Page1 UNIT-II Part-2: CENTRAL PROCESSING UNIT Stack Organization Instruction Formats Addressing Modes Data Transfer And Manipulation Program Control Reduced Instruction Set Computer (RISC) Introduction:

More information

MCA SEM-II Data Structure

MCA SEM-II Data Structure MCA SEM-II Data Structure Timings: 30 Minutes] Objective Questions Keys [ Marks - 40 1. The memory address of the first element of an array is called a. Floor address b. Foundation address c. First address

More information

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017

Stack Applications. Lecture 27 Sections Robb T. Koether. Hampden-Sydney College. Wed, Mar 29, 2017 Stack Applications Lecture 27 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Wed, Mar 29, 2017 Robb T. Koether Hampden-Sydney College) Stack Applications Wed, Mar 29, 2017 1 / 27 1 Function

More information

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved

Stacks. Chapter 5. Copyright 2012 by Pearson Education, Inc. All rights reserved Stacks Chapter 5 Copyright 2012 by Pearson Education, Inc. All rights reserved Contents Specifications of the ADT Stack Using a Stack to Process Algebraic Expressions A Problem Solved: Checking for Balanced

More information

LIFO : Last In First Out

LIFO : Last In First Out Introduction Stack is an ordered list in which all insertions and deletions are made at one end, called the top. Stack is a data structure that is particularly useful in applications involving reversing.

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 03 / 31 / 2017 Instructor: Michael Eckmann Today s Topics Questions? Comments? finish RadixSort implementation some applications of stack Priority Queues Michael

More information

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1

Data Structure using C++ Lecture 04. Data Structures and algorithm analysis in C++ Chapter , 3.2, 3.2.1 Data Structure using C++ Lecture 04 Reading Material Data Structures and algorithm analysis in C++ Chapter. 3 3.1, 3.2, 3.2.1 Summary Infix to Postfix Example 1: Infix to Postfix Example 2: Postfix Evaluation

More information

March 13/2003 Jayakanth Srinivasan,

March 13/2003 Jayakanth Srinivasan, Statement Effort MergeSort(A, lower_bound, upper_bound) begin T(n) if (lower_bound < upper_bound) Θ(1) mid = (lower_bound + upper_bound)/ 2 Θ(1) MergeSort(A, lower_bound, mid) T(n/2) MergeSort(A, mid+1,

More information

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently.

DATA STRUCUTRES. A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. DATA STRUCUTRES A data structure is a particular way of storing and organizing data in a computer so that it can be used efficiently. An algorithm, which is a finite sequence of instructions, each of which

More information

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May

R10 SET - 1. Code No: R II B. Tech I Semester, Supplementary Examinations, May Code No: R21051 R10 SET - 1 II B. Tech I Semester, Supplementary Examinations, May - 2012 DATA STRUCTURES (Com. to CSE, IT, ECC ) Time: 3 hours Max Marks: 75 Answer any FIVE Questions All Questions carry

More information

DATA STRUCTURE UNIT I

DATA STRUCTURE UNIT I DATA STRUCTURE UNIT I 1. What is Data Structure? A data structure is a mathematical or logical way of organizing data in the memory that consider not only the items stored but also the relationship to

More information

Sample Question Paper

Sample Question Paper Scheme - I Sample Question Paper Marks : 70 Time: 3 Hrs. Q.1) Attempt any FIVE of the following. 10 Marks a. Write any four applications of data structure. b. Sketch the diagram of circular queue. c. State

More information

CS8391-DATA STRUCTURES QUESTION BANK UNIT I

CS8391-DATA STRUCTURES QUESTION BANK UNIT I CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Define data structure. The data structure can be defined as the collection of elements and all the possible operations which are required for those

More information

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3

Darshan Institute of Engineering & Technology for Diploma Studies Unit 3 Linear and Non-Linear Data Structures Linear data structure: Linear data structures are those data structure in which data items are arranged in a linear sequence by physically or logically or both the

More information

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out

Stack. Data structure with Last-In First-Out (LIFO) behavior. Out Stack and Queue 1 Stack Data structure with Last-In First-Out (LIFO) behavior In Out C B A B C 2 Typical Operations Pop on Stack Push isempty: determines if the stack has no elements isfull: determines

More information

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues

The Bucharest University of Economic Studies. Data Structures. ADTs-Abstract Data Types Stacks and Queues The Bucharest University of Economic Studies Data Structures ADTs-Abstract Data Types Stacks and Queues Agenda Definition Graphical representation Internal interpretation Characteristics Operations Implementations

More information

Table of Contents. Chapter 1: Introduction to Data Structures... 1

Table of Contents. Chapter 1: Introduction to Data Structures... 1 Table of Contents Chapter 1: Introduction to Data Structures... 1 1.1 Data Types in C++... 2 Integer Types... 2 Character Types... 3 Floating-point Types... 3 Variables Names... 4 1.2 Arrays... 4 Extraction

More information

CS 206 Introduction to Computer Science II

CS 206 Introduction to Computer Science II CS 206 Introduction to Computer Science II 07 / 26 / 2016 Instructor: Michael Eckmann Today s Topics Comments/Questions? Stacks and Queues Applications of both Priority Queues Michael Eckmann - Skidmore

More information

CSE 214 Computer Science II Stack

CSE 214 Computer Science II Stack CSE 214 Computer Science II Stack Spring 2018 Stony Brook University Instructor: Shebuti Rayana shebuti.rayana@stonybrook.edu http://www3.cs.stonybrook.edu/~cse214/sec02/ Random and Sequential Access Random

More information

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015

Stack Applications. Lecture 25 Sections Robb T. Koether. Hampden-Sydney College. Mon, Mar 30, 2015 Stack Applications Lecture 25 Sections 18.7-18.8 Robb T. Koether Hampden-Sydney College Mon, Mar 30, 2015 Robb T. Koether Hampden-Sydney College) Stack Applications Mon, Mar 30, 2015 1 / 34 1 The Triangle

More information

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted,

Revision Statement while return growth rate asymptotic notation complexity Compare algorithms Linear search Binary search Preconditions: sorted, [1] Big-O Analysis AVERAGE(n) 1. sum 0 2. i 0. while i < n 4. number input_number(). sum sum + number 6. i i + 1 7. mean sum / n 8. return mean Revision Statement no. of times executed 1 1 2 1 n+1 4 n

More information

CSE 230 Intermediate Programming in C and C++

CSE 230 Intermediate Programming in C and C++ CSE 230 Intermediate Programming in C and C++ Structures and List Processing Fall 2017 Stony Brook University Instructor: Shebuti Rayana http://www3.cs.stonybrook.edu/~cse230/ Self-referential Structure

More information

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO)

Stacks. stacks of dishes or trays in a cafeteria. Last In First Out discipline (LIFO) Outline stacks stack ADT method signatures array stack implementation linked stack implementation stack applications infix, prefix, and postfix expressions 1 Stacks stacks of dishes or trays in a cafeteria

More information

PA3 Design Specification

PA3 Design Specification PA3 Teaching Data Structure 1. System Description The Data Structure Web application is written in JavaScript and HTML5. It has been divided into 9 pages: Singly linked page, Stack page, Postfix expression

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in themodel answer scheme. 2) The model answer and the answer written by candidate may

More information

CS8391-DATA STRUCTURES

CS8391-DATA STRUCTURES ST.JOSEPH COLLEGE OF ENGINEERING DEPARTMENT OF COMPUTER SCIENCE AND ENGINEERI NG CS8391-DATA STRUCTURES QUESTION BANK UNIT I 2MARKS 1.Explain the term data structure. The data structure can be defined

More information

Types of Data Structures

Types of Data Structures DATA STRUCTURES material prepared by: MUKESH BOHRA Follow me on FB : http://www.facebook.com/mukesh.sirji4u The logical or mathematical model of data is called a data structure. In other words, a data

More information

CS301 - Data Structures Glossary By

CS301 - Data Structures Glossary By CS301 - Data Structures Glossary By Abstract Data Type : A set of data values and associated operations that are precisely specified independent of any particular implementation. Also known as ADT Algorithm

More information

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi

Data Structures & Algorithm Analysis. Lecturer: Souad Alonazi Data Structures & Algorithm Analysis Lec(3) Stacks Lecturer: Souad Alonazi What is a stack? Stores a set of elements in a particular order Stack principle: LAST IN FIRST OUT = LIFO It means: the last element

More information

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305

DATA STRUCTURE : A MCQ QUESTION SET Code : RBMCQ0305 Q.1 If h is any hashing function and is used to hash n keys in to a table of size m, where n

More information

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms...

First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms... First Semester - Question Bank Department of Computer Science Advanced Data Structures and Algorithms.... Q1) What are some of the applications for the tree data structure? Q2) There are 8, 15, 13, and

More information

This book is licensed under a Creative Commons Attribution 3.0 License

This book is licensed under a Creative Commons Attribution 3.0 License 6. Syntax Learning objectives: syntax and semantics syntax diagrams and EBNF describe context-free grammars terminal and nonterminal symbols productions definition of EBNF by itself parse tree grammars

More information

Prefix/Infix/Postfix Notation

Prefix/Infix/Postfix Notation Prefix/Infix/Postfix Notation One commonly writes arithmetic expressions, such as 3 + 4 * (5-2) in infix notation which means that the operator is placed in between the two operands. In this example, the

More information

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks

Outline. Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications. ADT for stacks Stack Chapter 4 Outline Introduction Stack Operations Stack Implementation Implementation of Push and Pop operations Applications Recursive Programming Evaluation of Expressions ADT for stacks Introduction

More information

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \

1 P a g e A r y a n C o l l e g e \ B S c _ I T \ C \ BSc IT C Programming (2013-2017) Unit I Q1. What do you understand by type conversion? (2013) Q2. Why we need different data types? (2013) Q3 What is the output of the following (2013) main() Printf( %d,

More information

Lab 7 1 Due Thu., 6 Apr. 2017

Lab 7 1 Due Thu., 6 Apr. 2017 Lab 7 1 Due Thu., 6 Apr. 2017 CMPSC 112 Introduction to Computer Science II (Spring 2017) Prof. John Wenskovitch http://cs.allegheny.edu/~jwenskovitch/teaching/cmpsc112 Lab 7 - Using Stacks to Create a

More information

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above

Data Structures. 1. Each entry in a linked list is a called a (a)link (b)node (c)data structure (d)none of the above Data Structures 1. Each entry in a linked list is a called a --------- (a)link (b)node (c)data structure (d)none of the above 2. Linked list uses type of memory allocation (a)static (b)dynamic (c)random

More information

Stacks, Queues (cont d)

Stacks, Queues (cont d) Stacks, Queues (cont d) CSE 2011 Winter 2007 February 1, 2007 1 The Adapter Pattern Using methods of one class to implement methods of another class Example: using List to implement Stack and Queue 2 1

More information

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1

Prepared by Mrs.D.Maladhy (AP/IT/RGCET) Page 1 Basics : Abstract Data Type(ADT) introduction to data structures representation - implementation Stack and list: representing stack implementation application balancing symbols conversion of infix to postfix

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure using C Model wer Subject Code: 22317 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given

More information

PESIT Bangalore South Campus Department of MCA Course Information for

PESIT Bangalore South Campus Department of MCA Course Information for 1. GENERAL INFORMATION: PESIT Bangalore South Campus Department of MCA Course Information for Data Structures Using C(13MCA21) Academic Year: 2015 Semester: II Title Code Duration (hrs) Lectures 48 Hrs

More information

The Stack and Queue Types

The Stack and Queue Types The Stack and Queue Types Hartmut Kaiser hkaiser@cct.lsu.edu http://www.cct.lsu.edu/ hkaiser/fall_2012/csc1254.html 2 Programming Principle of the Day Do the simplest thing that could possibly work A good

More information

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR

MULTIMEDIA COLLEGE JALAN GURNEY KIRI KUALA LUMPUR STUDENT IDENTIFICATION NO MULTIMEDIA COLLEGE JALAN GURNEY KIRI 54100 KUALA LUMPUR FIFTH SEMESTER FINAL EXAMINATION, 2014/2015 SESSION PSD2023 ALGORITHM & DATA STRUCTURE DSEW-E-F-2/13 25 MAY 2015 9.00 AM

More information

1. Two main measures for the efficiency of an algorithm are a. Processor and memory b. Complexity and capacity c. Time and space d.

1. Two main measures for the efficiency of an algorithm are a. Processor and memory b. Complexity and capacity c. Time and space d. 1. Two main measures for the efficiency of an algorithm are a. Processor and memory b. Complexity and capacity c. Time and space d. Data and space 2. The time factor when determining the efficiency of

More information

Sorting. Introduction. Classification

Sorting. Introduction. Classification Sorting Introduction In many applications it is necessary to order give objects as per an attribute. For example, arranging a list of student information in increasing order of their roll numbers or arranging

More information

S.Y. B.Sc. (IT) : Sem. III. Data Structures

S.Y. B.Sc. (IT) : Sem. III. Data Structures S.Y. B.Sc. (IT) : Sem. III Data Structures Time : 2½ Hrs.] Prelim Question Paper Solution [Marks : 75 Q.1Attempt the following (any THREE) [15] Q.1(a) What is an algorithm? Write down characteristics of

More information

Introduction to Data Structures & Algorithms S.GokulaKrishnan B.Tech.,M.Tech.,[Ph.D] Assistant Professor[Stage-I] Department of Computer Science and

Introduction to Data Structures & Algorithms S.GokulaKrishnan B.Tech.,M.Tech.,[Ph.D] Assistant Professor[Stage-I] Department of Computer Science and Introduction to Data Structures & Algorithms S.GokulaKrishnan B.Tech.,M.Tech.,[Ph.D] Assistant Professor[Stage-I] Department of Computer Science and Engineering Sri Chandrasekharendra Saraswathi Viswa

More information

Lecture 4 Stack and Queue

Lecture 4 Stack and Queue Lecture 4 Stack and Queue Bo Tang @ SUSTech, Spring 2018 Our Roadmap Stack Queue Stack vs. Queue 2 Stack A stack is a sequence in which: Items can be added and removed only at one end (the top) You can

More information

GUJARAT TECHNOLOGICAL UNIVERSITY COMPUTER ENGINEERING (07) / INFORMATION TECHNOLOGY (16) / INFORMATION & COMMUNICATION TECHNOLOGY (32) DATA STRUCTURES

GUJARAT TECHNOLOGICAL UNIVERSITY COMPUTER ENGINEERING (07) / INFORMATION TECHNOLOGY (16) / INFORMATION & COMMUNICATION TECHNOLOGY (32) DATA STRUCTURES GUJARAT TECHNOLOGICAL UNIVERSITY COMPUTER ENGINEERING () / INFMATION TECHNOLOGY (6) / INFMATION & COMMUNICATION TECHNOLOGY (32) DATA STRUCTURES Type of course: Compulsory SUBJECT CODE: 2302 B.E. 3 rd Semester

More information

Stack Abstract Data Type

Stack Abstract Data Type Stacks Chapter 5 Chapter Objectives To learn about the stack data type and how to use its four methods: push, pop, peek, and empty To understand how Java implements a stack To learn how to implement a

More information

The time and space are the two measure for efficiency of an algorithm.

The time and space are the two measure for efficiency of an algorithm. There are basically six operations: 5. Sorting: Arranging the elements of list in an order (either ascending or descending). 6. Merging: combining the two list into one list. Algorithm: The time and space

More information

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113)

Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) Objective Questions for Online Practical Exams under CBCS Scheme Subject: Data Structure-I (CS-113) 1. The number of interchanges required to sort 5, 1, 6, 2 4 in ascending order using Bubble Sort (A)

More information

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal.

Stacks (Section 2) By: Pramod Parajuli, Department of Computer Science, St. Xavier s College, Nepal. (Section 2) Linked list implementation of stack Typical Application of stacks Evaluation of expressions (infix, postfix, prefix) References and further details By: Pramod Parajuli, Department of Computer

More information

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU

Data Structures. Chapter 04. 3/10/2016 Md. Golam Moazzam, Dept. of CSE, JU Data Structures Chapter 04 1 Linear Array A linear array is a list of finite number N of homogeneous data elements such that: The elements of the array are referenced respectively by an index set consisting

More information

A Programming Approach with C DHARMENDER SINGH KUSHWAHA

A Programming Approach with C DHARMENDER SINGH KUSHWAHA DATA STRUCTURES A Programming Approach with C SECOND EDITION DHARMENDER SINGH KUSHWAHA Associate Professor Department of Computer Science and Engineering Motilal Nehru National Institute of Technology

More information

This is an individual assignment and carries 100% of the final CPS 1000 grade.

This is an individual assignment and carries 100% of the final CPS 1000 grade. CPS 1000 Assignment This is an individual assignment and carries 100% of the final CPS 1000 grade. Important instructions (read carefully and thoroughly): The firm submission deadline is Friday 5 th February

More information

Code No: R Set No. 1

Code No: R Set No. 1 Code No: R05010106 Set No. 1 1. (a) Draw a Flowchart for the following The average score for 3 tests has to be greater than 80 for a candidate to qualify for the interview. Representing the conditional

More information

An Introduction to Trees

An Introduction to Trees An Introduction to Trees Alice E. Fischer Spring 2017 Alice E. Fischer An Introduction to Trees... 1/34 Spring 2017 1 / 34 Outline 1 Trees the Abstraction Definitions 2 Expression Trees 3 Binary Search

More information

Class / Sem: I CSE / II Semester Subject Code: CS 6202 Subject: Programming and Data Structures I Prepared by T. Vithya Unit IV - LINEAR DATA STRUCTURES STACKS AND QUEUES Stack ADT Evaluating arithmetic

More information

infix expressions (review)

infix expressions (review) Outline infix, prefix, and postfix expressions queues queue interface queue applications queue implementation: array queue queue implementation: linked queue application of queues and stacks: data structure

More information

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC)

R13. II B. Tech I Semester Supplementary Examinations, May/June DATA STRUCTURES (Com. to ECE, CSE, EIE, IT, ECC) SET - 1 II B. Tech I Semester Supplementary Examinations, May/June - 2016 PART A 1. a) Write a procedure for the Tower of Hanoi problem? b) What you mean by enqueue and dequeue operations in a queue? c)

More information

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified)

MAHARASHTRA STATE BOARD OF TECHNICAL EDUCATION (Autonomous) (ISO/IEC Certified) WINTER 18 EXAMINATION Subject Name: Data Structure Model wer Subject Code: 17330 Important Instructions to examiners: 1) The answers should be examined by key words and not as word-to-word as given in

More information

Arrays and Linked Lists

Arrays and Linked Lists Arrays and Linked Lists Abstract Data Types Stacks Queues Priority Queues and Deques John Edgar 2 And Stacks Reverse Polish Notation (RPN) Also known as postfix notation A mathematical notation Where every

More information

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES

COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES COMPUTER ARCHITECTURE AND PARALEL PROCESSING STUDY NOTES UNIT 1 INTRODUCTION Central Processing unit (CPU): Alternately referred to as a processor, central processor, or microprocessor, the CPU is the

More information

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack.

Stacks. CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack. Stacks CONTENTS 3.1 Introduction 1. Stack as an abstract data type 2. Representation of a Stack as an array 3.2Applications of Stack Hours: 12 Marks: 18 3.1 Introduction to Stacks 1. Stack as Abstract

More information

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi

Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi Tutorial 3: Infix, Prefix, Postfix & Tower of Hanoi ? 2 Consider the following operation performed on a stack of size 5. Push(1); Pop(); Push(2); Push(3); Pop(); Push(4); Pop(); Pop(); Push(5); a) 1 b)

More information

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms

Stacks and Queues. Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Stacks and Queues Chris Kiekintveld CS 2401 (Fall 2010) Elementary Data Structures and Algorithms Two New ADTs Define two new abstract data types Both are restricted lists Can be implemented using arrays

More information