Unit 2 DS N Stack
Unit 2 DS N Stack
com
Welcome!!
Hello!
a B S
et h
m S hw
I a
3
Data Structures Using C
g. c om
a v andv
www.d
15/06/2021
17/5/2021 www.davandvg.com
6
Data Structure
Shwetha BS
Today’s topic
3. Stack: Definition
7
4. Array implementation of stack
5. Operations on stack
6. Applications on stack
7. Conversion of arithmetic expressions : infix , prefix , postfix
g. c om
a v andv
www.d
15/06/2021
03-06-2021
Data and Information
What is Data?
Data is a raw and unorganized fact that required to be
processed to make it meaningful. Data can be simple at
the same time unorganized unless it is organized.
Generally, data comprises facts, observations,
perceptions numbers, characters, symbols, image, etc.
Data is always interpreted, by a human or machine, to
derive meaning. So, data is meaningless. Data contains
numbers, statements, and characters in a raw form.
Data Structure
Data Structure : A data structure can be defined as a method of storing and organizing the
data items in the computer's memory. Data structure is a programming construct which
helps in storing and processing logical collection of programming elements.
A data structure is a way of organizing not only the elements stored in RAM but also
their
relationship to each other.
The data structures mainly deal with:
Storing data in memory
Organizing data in memory
Fetching and processing data
Degree ofShwetha
associativity
BS Data Structure www.davandvg.com 11
15/06/2021
17-06-2021
categories-
It is a structure in which the elements are stored sequentially, and the elements are connected to the
previous and the next element. As the elements are stored sequentially, so they can be traversed or
memory.
The data elements in an array are traversed one after another and can access only one element at a time
and the next element), whereas, in the non-linear data structure, an element can be
Create:-
The create operation results in reserving memory for program elements. This can
be done by declaration statement.
Creation of data structure may take place either during compile-time or run-time.
malloc() function of C language is used for creation.
Destroy:-
Destroy operation destroys memory space allocated for specified data structure.
free() function of C language is used to destroy data structure.
Searching:- It finds the presence of desired data item in the list of data items, it
may also find the locations of all elements that satisfy certain conditions.
Sorting:- Sorting is a process of arranging all data items in a data structure in a
particular order, say for example, either in ascending order or in descending
order.
Merging:- Merging is a process of combining the data items of two different
sorted list into a single sorted list.
Splitting:- Splitting is a process of partitioning single list to multiple list.
Traversal:- Traversal is a process of visiting each and every node of a list in
systematic manner.
PUSH
This operation is used to add or insert an element to the
top of a stack.
You cannot perform push operation, if stack does not
exist.
The number of elements to be added must not exceed the
size of a stack.
POP
This operation is used to remove or delete an element at the top of a stack.
You cannot perform pop operation, if stack does not exist.
The element being removed is returned to the user.
TOP
This operation returns the value of the element present at the top of the stack.
The initial value of TOP is -1 when the stack is created using an array.
The TOP grows or shrinks as the insertion or deletion operations are performed.
STACKFULL
When we try to add an element to the top of a stack beyond its size, then the
stackfull condition is flagged
This operation returns true if the stack is full. Otherwise, it returns false.
STACKEMPTY
When we try to remove the element from the stack beyond its base, the
stackempty condition is flagged
This operation returns true if the stack is empty. Otherwise, it returns false.
When we attempt to delete an element from the recently created stack also results
in stackempty condition.
DISPLAY
It prints all the elements available in the stack.
Shwetha BS Data Structure www.davandvg.com 23
17-06-2021 PUSH operation
push(): When we insert an element from the top in
a stack then the operation is known as a push. Only 1
element is inserted at a time.
The steps involved in the PUSH operation is given below:
Before inserting an element in a stack, we check
whether the stack is full.
If we try to insert the element in a stack, and the stack
is full, then the overflow condition occurs.
When we initialize a stack, we set the value of top as -1
to check that the stack is empty.
Shwetha BS Data Structure www.davandvg.com 24
17-06-2021
5.Write a c program to demonstrate the working of stack of size n using an array the
elements of the stack may be assumed to be of type integer by creating an array , the
operations to be supported are 1. PUSH 2. POP 3. DISPLAY. The program to should
print the appropriate message for stack is underflow and overflow.
Expression : The sequence of operators and operands that reduces to a single value
after evaluation is called an expression. The operands consist of constants and
variables whereas the operators consist of symbols such as +, -, *. / and so on. The
operators indicate the operation to be performed on the operands specified
Representation of
Postfix expression ab+
expression
The rules that determine the order in which different operators are evaluated
are called precedence rules or precedence of operators.
be evaluated Highest precedence operators will have the highest value and
Thank you
47