DATA STRUCTURES AND ALGORITHMS (ITC104L) REVIEWER
STACK
Stack is a container of objects that are inserted and removed according to the last-in first-out (LIFO)
principles
A stack is a limited access data structure - elements can be added and removed from the stack only at the
top
Push adds an item to the top of the stack
Pop removes the item from the top
ALGORITHM TO CONVERT INFIX TO POSTFIX
1. Algorithm to convert Infix To Postfix
2. If the character is an operand, put it into output stack
3. If the character is an operator and operator’s stack is empty, push operator into operators’ stack
4. If the operator’s stack is not empty, there may be following possibilities
a. If the precedence of scanned operators is greater than the top most operator of operator’s stack,
push this operator into operand’s stack
b. If the precedence of scanned operator is less than or equal to the top most operator of operator’s
stack, pop the operators from the operand’s stack until we find a low precedence operator than
the scanned character. Never pop out (‘(‘) or (‘)’) whatever may be the precedence level of
scanned characters
c. If the character is opening round bracket (‘(‘), push it into operator’s stack
d. If the character it closing round bracket (‘)’), pop out operators from operator’s stack until we
find an opening bracket (‘(‘)
e. Now pop out all the remaining operators from the operator’s stack and push into output stack.
STACK APPLICATION
The simplest application of a stack is to reverse a word.
o You push a given word to stack - letter by letter - and then pop letters from the stack
“undo” mechanism in text editors; this operation is accomplished by keeping all text changes in a stack
Backtracking
o This is a process when you need to access the most recent data element in a series of elements
o Therefore, at each choice point you store on a stack all possible choices. Then backtracking
simply means popping a next choice from the stack
Space for parameters and local variables is created internally using a stack
Compiler’s syntax check for matching braces is implemented by using stack
Support for recursion
STACK IMPLEMENTATION
Adapter Class
Array-based implementation
Linked List-based implementation
Queues
LOOPING STATEMENT IN C
INTRODUCTION
Looping statements
o Are the statements that execute one or more statement repeatedly several number of times
o There are three types of loops; while, for and do-while
o The sequence of statements to be executed is kept inside the curly braces {} known as the
Loop body
Advantage with looping statement
o Reduce length of code
o Take less memory space
o Burden on the developer is reducing
o Time consuming process to execute the program is reduced
TYPES OF LOOP
while loop
o can be addressed as an entry control loop
o completed in 3 steps
Variable initialization
Condition
Variable increment or decrement
For loop
o Used to execute a set of statements repeatedly until a particular condition is satisfied
o An open ended loop
o Have exactly two semicolons
One after initialization
Second after the condition
o Can have more than one initialization or increment/decrement, separated using comma operator
o Can have only one condition
o The for loop is executed as follows:
It first evaluates the initialization code
Then it checks the condition expression
If it is true, it executes the for-loop body
Then it evaluates the increment/decrement condition and again follows from step 2
When the condition expression becomes false, it exits the loop
o Nested for loop
We can also have nested for loops, one for loop inside another for loop
do while loop
o used when it is necessary to execute body of the loop before testing the condition
o do statement evaluates the body of the loop first and at the end, the condition is checked using
while statement
o the body of the loop will be executed at least once
JUMPING OUT OF LOOPS
break statement
o when break statement is encountered inside a loop, the loop is immediately exited and the
program continues with the statement immediately following the loop
continue statement
o It causes the control to go directly to the test-condition and then continue the loop process
o On encountering continue, cursor leave the current cycle of loop, and starts with the next cycle