Plant propagation: Sexual and Asexual propapagation.pptx
Ds
1. Unit V
Stack
In the field of computing we have formulated many ways to handle data
efficiently.
So far we have seen how to use variables to store data. However
variables are not feasible when handling a huge amount of data.
Also we need an organized medium of storage to handle any correlated
information. Thus the concept of data structure is introduced.
Data structure is a collection of organized data that are related to each
other.
Data structures are primarily of two types they are
1. Primitive data structures
- Integers
- Real
- Char
2. Non-Primitive Data Structures
- Linear Data structures
- stack
-Queue
- List
- Array
- Non-Linear Data Structures
- Trees
- Graphs.
2. Operations performed on data structure:
The most common operations on the Data Structure
Insertion: Adding a new element from the Data Structure
Deletion: Deleting an element from the Data Structure.
Traversing: Each element of data is accessed on a process is known as
traversing.
Searching: To find the position of the element in the given Data
Structure.
Merging : Combining the elements of two similar data structures into
one.
Stack:
A Stack is a linear data structure in which a data item is inserted and deleted at one
end.
- A stack is called a “Last in First Out(LIFO)” Structure because the data item that
is inserted last into the stack is the first data item to be deleted from the stack.
- Stacks are extensively used in computer applications.
- Their most notable use is in system software.
- When one function calls another function and passes some
parameters.These parameters are passed using stack. In
fact even many compilers store the local variables inside a
function on the stack.
- Inserting a value to the stack is called as “push” operation,
Where as reading a value from it is called as a “pop”
operation.
3. Deleting: - The pop operation in the case of a stack is destructive i.e
once an item is popped from the stack, it is no longer available
Operation Content of stack after operation
Push(A) A
Push(B) A B
Pop A
Push( c) A C
Push(D) A C D
Pop A C
POP A
Pop Empty
Stack Implementation:
- Stack implementation can be achieved using arrays. It is a
very simple method but has few limitations.
- Once the size of an array is declared it can not be altered
during program execution.
- The program itself decides the amount of memory needed
and it informs accordingly to the compiler.
- In this method the memory requirement is determined
before compilation and compiler provides the required
memory.
- It is an in efficient memory allocation technique.
4. - This is because in case we intend to store less arguments
than declared memory is wasted and if we desire to store
more elements than declared array could not expand.
- It is suitable only when we exactly know the number of
elements to be stored.
Dynamic implementation:
- Pointers can also be used for implementation of a stack.
- The dynamic implementation is achieved using pointers.
- The limitations noticed in static implementation can be
removed using dynamic implementation.
- Using pointer implementation at run time. There is no
restriction for number of elements the stack may be
expandable.
- It is efficient in memory allocation because the program
informs the compiler its memory requirement at run time.
- Memory is allocated only after an element is pushed
- Pointer implementation of a stack is carried similar to
array implementation.
- Like an array elements can be stored in successive memory
locations using a pointer.
WAp to perform the stack operations such as push() and pop()
functions using arrays.
#include<stdio.h>
#include<conio.h>
#define max 100;
5. int stack[max]=n;
int top=-1;
void main()
{
int option,x;
char ch=’y’;
clrscr();
while(ch==’y’)
{
Printf(“1.pushn2.popn3.displayn”);
Printf(“enter choice”);
Scanf(“ %d”,&option);
Switch(option)
{
Case 1:
{
Printf(“enter elements to be pushedn”);
Scanf(“%d”,&n);
Push(stack,n);
Break;
}
6. Case 2:
{
Pop(stack)
Break;
}
Case 3:
{
Printf(“the elements are n”);
Display(stack);
Break;
Case 3:
{
Printf(“the elements are”);
Display(stack);
Break;
}
}
Printf(“do u want to continue(y/n) n”);
Ch=getche();
}
9. 1.push
2.pop
3.display
Enter choice
1
Enter elements to be pushed
80
Pushed
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
1
Enter element to be pushed
90
Pushed
Do u want to continue(y/n)
Y
1.push
10. 2.pop
3.display
Enter ur choice
3
The elements are
90
80
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter ur choice 2
Poped
Do u want to continue(y/n)
Y
1.push
2.pop
3.display
Enter choice
2
11. Poped
Do u want to continue(y/n) y
1.push
2.pop
3.display
Enter choice 2
Stack is empty
Do u want to continue(y/n) n
A stack is a ordered collection of data items into which new items
may be inserted and from which data items may be deleted at one end.
- A stack is also called push down lists.
Eg: A coin stacker etc.
- A stack is most commonly used as a place to store local
variables,parameters and return addresses when a function
is called.
Applications of stack:
1.Situations where useful information must be held temporarily during
the course of a program.
2. compiler in evaluation of an expression by recursion.
3. Memory management in operating system.
4. Evaluation of an arithmetic expression.
12. 5. Nesting of functions.
6. Storing of register contents in process swapping.
Basic terminology associated with stacks.
1.Stack pointer(top) : Keeps track of the current position on the stack.
2.Overflow: occurs when we try to insert more information on a stack
than it can hold.
Underflow:occurs when we try to delete an item of stack which is
empty.
Algorithm for inserting an item into stack:
Procedure push(s,max,top,item)
s-stack
max- stack size
top- stack pointer
item- value in a cell
step1: {check for stack overflow}
if top>=max then
print stack overflow
return
step 2: {Increment pointer top}
13. toptop+1
step 3:{Insert item at top of the stack}
s[top]item
return
Algorithm for deleting an item from the stack:
Function pop(s top)
S array
Top stack pointer
Step 1: {check for stack underflow}
If top=0 then
Print “stack underflow”
Return
Step 2: {return former top element of stack}
Items[top]
Step 3:{decrement pointer top}
Toptop-1
Return
Applications of stack:
A stack has various real life applications
14. 1. Stack is very useful to maintain the sequence of processing
2. Stacks are used to convert bases of numbers.
3. In evaluation of expressions stacks are used.
Queues
-A queue is a linear sequential list of
items that are accessed in the order
First In a Out(FIFO)
- A queue is special type of data
structure in which insertions take
place from one end called “rear” end
and deletions take place from another
end called “front” end i.e insertions
and deletions take place from different
ends.
- Queue works on the basis of first in
first out[FIFO]. Since the first element
15. entered in a queue will be the first
element to be deleted.
A queue can be represented using
sequential allocation.
Queue type of data structures is used
in time sharing system where many
user jobs will be waiting in the system
queue for processing. These jobs may
request the service of CPU,Main
memory or external devices such as
printer.
Eg: people waiting in a line at a bank
from a queue, where the first person in
line is the first person to be waiting
and so on.