Data Structures and Algorithms Reviewer
Data Structures and Algorithms 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
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
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