You are on page 1of 47

www.davandvg.

com

Spurthi Educational Trust ®


Davan Institute of Advanced Management Studies
Nutana Institute of Management Studies
13th Main, MCC 'B' Block, Davangere-04: Pho: 08192-225550, 7026-225550
Instructions To The Students
01 Every one have to keep your video ON through out the session.

02 Mute your Audio. Unmute only when you need to answer.

03 Any kind of misbehaviour is not tolerated and strict action will be


taken .
2

04 These topics will not be repeated in the offline classes.

05 Attendance will be automatically taken by the


system. And the students whose video will be ON
considered as present. g. c om
a v andv
www.d
www.davandvg.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

Topic Of The Day

Data structures and Stack

Shwetha BS Data Structure www.davandvg.com 5


17/5/2021
15/06/2021
Quotations

6
Data Structure
Shwetha BS
Today’s topic

Unit 2. Introduction to Data Structure and Stack

1. Definition , Application , Classification of data structures


2. Operations on Data Structures

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.

Shwetha BS Data Structure www.davandvg.com 8


15/06/2021
17-06-2021
What is Information?
 Information is a set of data which is processed in a meaningful way
according to the given requirement. Information is processed,
structured, or presented in a given context to make it meaningful
and useful.
 It is processed data which includes data that possess context,
relevance, and purpose. It also involves manipulation of raw data.
 Information assigns meaning and improves the reliability of the
data. It helps to ensure undesirability and reduces uncertainty. So,
when the data is transformed into information, it never has any
useless details.
Shwetha BS Data Structure www.davandvg.com 9
15/06/2021
17-06-2021

Shwetha BS Data Structure www.davandvg.com 10


15/06/2021
17-06-2021

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

Classification of Data structure

Data structure is basically

classified into two

categories-

• Primitive Data type

• Non- primitive data type

Shwetha BS Data Structure www.davandvg.com 12


15/06/2021
17-06-2021

Primitive Data Structures


 Primitive Data Structures are the basic data structures that directly operate upon the machine
instructions.
 They have different representations on different computers.
 Ex: Integers, Floating point numbers, Character constants, String constants and Pointers

Non-primitive Data Structures


 Non-primitive data structures are more complicated data structures and are derived from primitive
data structures linking together in a meaningful way.
 They emphasize on grouping same or different data items with relationship between each data
item and are not directly operated upon machine level instructions.
 Ex: Arrays, Lists and Files

Shwetha BS Data Structure www.davandvg.com 13


15/06/2021
17-06-2021

Linear data structure

 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

accessed in a single run.


 The implementation of linear data structures is easier as the elements are sequentially organized in

memory.
 The data elements in an array are traversed one after another and can access only one element at a time

 Ex: Array, Queue, Stack, Linked List.

Shwetha BS Data Structure www.davandvg.com 14


15/06/2021
17-06-2021

Non-linear data structure


 It is data structure in which the data elements are not arranged in a contiguous manner.

As the arrangement is nonsequential, so the data elements cannot be traversed or

accessed in a single run.


 In the case of linear data structure, element is connected to two elements (previous

and the next element), whereas, in the non-linear data structure, an element can be

connected to more than two elements.


 Trees and Graphs are the types of non-linear data structure.

Shwetha BS Data Structure www.davandvg.com 15


15/06/2021
17-06-2021
Operation on Data Structures

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.

Shwetha BS Data Structure www.davandvg.com 16


15/06/2021
17-06-2021
Selection:-
 Selection operation deals with accessing a particular data within a data structure.
 Selection is used to
 display a element or its place,
 delete an element
 add a new element
Updation:-
 It updates or modifies the data in the data structure after a particular operation is
performed.
 Updation is used
 when a new element is added or inserted .
 When an element is deleted.

Shwetha BS Data Structure www.davandvg.com 17


15/06/2021
17-06-2021

 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.

Shwetha BS Data Structure www.davandvg.com 18


15/06/2021
17-06-2021

 A stack is a simple data structure in which all insertions and deletions


are done at only one end called the top of the stack. While we
perform the insertion and deletion operations at the same end in a
stack, we will remove the last element first and the first element last.
Because of this process, the stack is called the Last-In-First-Out
(LIFO) data structures.
 A stack has a specific capacity to store data elements which are
inserted into and deleted from a stack. This capacity is called the size
of a stack.
 The other end is always fixed. This end is called the base of a stack
Shwetha BS Data Structure www.davandvg.com 19
15/06/2021
17-06-2021
Stack Representation

 The following diagram depicts a stack
and its operations
 A stack can be implemented by means of
Array, Structure, Pointer, and Linked
List. Stack can either be a fixed size one
or it may have a sense of dynamic
resizing.
 Here, we are going to implement stack
using arrays, which makes it a fixed size
stack implementation.
Shwetha BS Data Structure www.davandvg.com 20
Stack Operations
15/06/2021
17-06-2021

The main operations that are performed on a stack are as


follows:

 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.

Shwetha BS Data Structure www.davandvg.com 21


15/06/2021
17-06-2021

 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.

Shwetha BS Data Structure www.davandvg.com 22


15/06/2021
17-06-2021

 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

 When the new element is pushed in a


Function for push()
stack, first, the value of the top gets
incremented, i.e., top=top+1, and the void push()
{
element will be placed at the new position If (top==stack_size-1)
of the top. {
printf(“stack overflow\n”);
 The elements will be inserted until we return ;
reach the max size of the stack. }
top=top+1;
 If the stack is full then the stack overflow s[ top]=item;
or stackfull condition occurs }

Shwetha BS Data Structure www.davandvg.com 25


POP operation
17-06-2021

 pop(): When we delete an element


from the stack, the operation is
known as a pop. One element can be
deleted at a time.
 Before deleting the element from the
stack, we check whether the stack is
empty.
 If we try to delete the element from
the empty stack, then
the underflow condition occurs.
Shwetha BS Data Structure www.davandvg.com 26
17-06-2021

 If the stack is not empty, we first access Function for pop()


the element which is pointed by the top
 Once the pop operation is performed, the int pop()

top is decremented by 1, i.e., top=top-1. {

 If the stack is empty means that no int item_deleted;

element exists in the stack, this state is if(top==-1) return 0;

known as an underflow state. item_deleted=s[top--];


return item_deleted;
}

Shwetha BS Data Structure www.davandvg.com 27


17-06-2021
Display operation
Function for display
 In the display process if the
void display()
stack already have some items {
int i ;
those will be displayed one after
if(top==-1)
the other. {
printf(“stack is empty\n”);
 If there are no elements in the
return;
stack the appropriate error }
printf(“content of the stack \n”);
message will be displayed as
for( i=0;i<=top;i++)
stack empty. printf(“%d\n”,s[i]);
}
Shwetha BS Data Structure www.davandvg.com 28
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.

#include<stdio.h> printf (“enter the elements to be inserted /n’);


#include<conio.h> scanf(“%d”,&item);
#define STACK_SIZE 3 top=top+1;
int choice, item, top, s [10]; s[top]=item; }
void push () void pop () {
{ if (top==-1)
if (top==STACK_SIZE -1) {
{ printf(“Stack underflow/n”);
printf (“stack overflow/n”); return; }
return; printf(“The element deleted is %d\n”, s[top--]);
} }
Shwetha BS Data Structure www.davandvg.com 29
17-06-2021

void display () void main ()


{ { {
int i; top=-1; case 1: push();
if(top==-1)
clrscr(); break;
{
for (;;) case2 : pop();
printf(“stack is empty/n’);
{ break;
return;
} printf(“contents of stack /n”); printf(“1: push 2:pop 3:display case3:display();
for (i=0; i<=top; i++) 4:exit\n”);
break;
{ printf(“enter the choice\n”);
case4: exit(0);
printf(“%d/n”,s[i]); scanf(“%d”,&choice);
}}}
}} switch(choice)
Shwetha BS Data Structure www.davandvg.com 30
17-06-2021
18/06/2021

Shwetha BS Data Structure www.davandvg.com 31


Conversion of Expression
17-06-2021
18/06/2021

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

Infix expression a+b

Representation of
Postfix expression ab+
expression

Prefix expression +ab


Shwetha BS Data Structure www.davandvg.com 32
17-06-2021
18/06/2021

 Infix expression : In an expression, if an operator is in between two


operands, the expression is called an infix expression. The infix
expression can be parenthesized or un-parenthesized.
 a + b is an un-parenthesized infix expression
 (a + b) is a parenthesized infix expression
 In the expression,
Operand 2
Operand 1 a+b
Operator
Shwetha BS Data Structure www.davandvg.com 33
17-06-2021
18/06/2021

 Postfix expression: In an expression, if an operator follows the two


operands the expression is called postfix expression.
 No parenthesis is allowed in postfix expressions
 It is also called suffix expression or reverse polish expression.
 For example, a b +

 Prefix expression: In an expression, if an operator precedes the two


operands (i.e., operator comes before the two operands), the expression
is called prefix expression.
 No,parenthesis is allowed in prefix expressions.
 It is also called polish expression. For example, +ab
Shwetha BS Data Structure www.davandvg.com 34
17-06-2021
18/06/2021
Precedence and associativity of the operators

 The rules that determine the order in which different operators are evaluated
are called precedence rules or precedence of operators.

 we associate values to determine the order in which the operators have to

be evaluated Highest precedence operators will have the highest value and

lowest precedence operators will have the least value.


 The order in which the operators with same precedence are evaluated in an
expression is called associativity of the operator

Shwetha BS Data Structure www.davandvg.com 35


17-06-2021
18/06/2021

 In an expression, if two or more operators have the same priority and


are evaluated from left-to-right, then the operators are called left
associative operators. We normally denote using L → R. The process of
evaluating from left to right is called left associativity.

 In an expression, if two or more operators have the same priority and if


they are evaluated from right-to-left, then the operators are called right
associative operators.

Shwetha BS Data Structure www.davandvg.com 36


17-06-2021
18/06/2021
Precedence and associativity of the operators

Description Operator Priority Associativity


Exponentiation $,^ 6 Right to Left
Multiplication * 4 Left to Right
Division / 4 Left to Right
Mod % 4 Left to Right
Addition + 2 Left to Right
Subtraction - 2 Left to Right
Braces (, ) 9 Left to Right

Shwetha BS Data Structure www.davandvg.com 37


18/06/2021
17-06-2021

Shwetha BS Data Structure www.davandvg.com 38


17-06-2021
18/06/2021

Shwetha BS Data Structure www.davandvg.com 39


17-06-2021
18/06/2021

Shwetha BS Data Structure www.davandvg.com 40


17-06-2021
18/06/2021

Shwetha BS Data Structure www.davandvg.com 41


17-06-2021
01/07/2021

Shwetha BS Data Structure www.davandvg.com 42


17-06-2021
01/07/2021

Shwetha BS Data Structure www.davandvg.com 43


17-06-2021
01/07/2021

Shwetha BS Data Structure www.davandvg.com 44


01/07/2021
17-06-2021
6.Write a c program to convert and print the valid fully parenthesized infix
arithmetic expression to postfix .

#include<stdio.h> postfix[j++] = s[top--];


#include<conio.h> if (F(s[top])!= G(symbol))
#include<string.h> s[++top] = symbol;
void infix_postfix(char infix[], char postfix[]) else
{
top--;
int top , j , i;
char s[30] , symbol; }
top = -1; while ( s[top] != '#’)
s[++top] = '#’; {
j = 0; postfix [j+] = s[top--];
for(i = 0; i <strlen(infix); i++) }
{ postfix[j] = '\0’;
symbol = infix[i]; }
while (F(s[top]) > G(symbol))

Shwetha BS Data Structure www.davandvg.com 45


17-06-2021
01/07/2021

int F(char symbol) int G(char symbol)


{ { void main()
switch(symbol) switch(symbol) {
{ { char infix [20];
case ‘+’: case ‘+: char postfix[20];
case '-': return 2; case '-': return 1; clrscr();
case ‘*’: case’*’: printf("Enter a valid infix expression\n");
case ‘/’: return 4; case ‘/’: return 3; scanf(“infix=%s",infix);
case ‘^’: case’^’: infix_postfix(infix, postfix);
case ‘$’: return 5; case '$': return 6; printf("The postfix expression is\n");
case '(': return 0; case '(': return 9; printf(“postfix=%s\n",postfix);
case '#': return -1; case ')': return 0; getch();
default: return 8; default: return 7; }
}} }}

Shwetha BS Data Structure www.davandvg.com 46


www.davandvg.com

Thank you

47

You might also like