Unit2 Stack
Unit2 Stack
UNIT-2
STACK
KCS-301
The Last-In-First-Out (LIFO) concept is used by Stacks, a type of linear data structure.
The Queue has two endpoints, but the Stack only has one (front and rear).
It only has one pointer, top pointer, which points to the stack's topmost element.
When an element is added to the stack, it is always added to the top, and it can only be removed from the
stack.
To put it another way, a stack is a container that allows insertion and deletion from the end known as the
stack's top.
Due to the possibility of understating inventory value, LIFO is not a reliable indicator of ending inventory value.
Due to increasing COGS, LIFO leads to reduced net income (and taxes). However, under LIFO during inflation,
there are fewer inventory write-downs. Results from average cost are in the middle of FIFO and LIFO.
Stack: A stack is a type of linear data structure whose components may only be added to or removed from the top
side of the list. The Last in First out (LIFO) principle governs stacks, meaning that the element put last is the first
element to be removed. Push operations and pop operations are the terms used to describe the addition and
removal of elements from stacks, respectively.
A pointer named top is used in stack to maintain track of the last piece that is currently present in the list.
Push(stacktop, size,x)
Here we insert an element x into stack, where size is the size of stack and we have
to insert an element using Top.
1- if top= size-1 then write stack is empty or Overflow and go to step 4
2- set Top= Top+1
3- set x into array of stack or Stack(Top)= x
4- exit
1- A + (B* C – ( D/E^F)*G)*H
2- ((A+B)*D)^(E-F)
3- A+((B+C) + (D+E)*F)/G
4- A-B/(C*D^F)
5- (A-B/C) * (D*E-F)
6- A/B^C-D
return res;
}
return 0;
}
2- Indirect Recursion
3- Tail Recursion
4- No Tail/ Head Recursion
5- Linear recursion
6- Tree Recursion
if (num == 0)
return ;
else
printf ("\n Number is: %d", num); // print the number
return fun1 (num - 1); // recursive call at the end in the fun() function
}
int main ()
{
fun1(10); // pass 10 as integer argument
return 0;
}
else
mid = (low + high) / 2
if x == arr[mid]
return mid
Recursion Iteration
Recursion uses the selection structure. Iteration uses the repetition structure.
Infinite recursion occurs if the step in recursion doesn't reduce the problem to
An infinite loop occurs when the condition in the loop
a smaller problem. It also becomes infinite recursion if it doesn't convert on a
doesn't become False ever.
specific condition. This specific condition is known as the base case.
Recursion terminates when the base case is met. Iteration terminates when the condition in the loop fails.
Recursion is slower than iteration since it has the overhead of maintaining and Iteration is quick in comparison to recursion. It doesn't
updating the stack. utilize the stack.
Recursion uses more memory in comparison to iteration. Iteration uses less memory in comparison to recursion.
Recursion reduces the size of the code. Iteration increases the size of the code.
Data Structure Unit-2 70