1. 1
Introduction to Data
Structures
Session I
Dr. V.Umadevi M.Sc(CS &IT). M.Tech (IT)., M.Phil., PhD., D.Litt.,
Director, Department of Computer Science, Jairams Arts and
Science College, Karur.
Data Structures & Algorithms
2. 2
• Introduction to Data Structures
• Primitive Data Structures
• Types of Data structures
– Stacks
– Queues
– Lists
• Stack
– Basic Operation of Stack
– Operations on the Data Structures
– Applications of Infix, Postfix and Prefix
Data Structures & Algorithms
Introduction to Data Structures
3. 3
Data Structures & Algorithms
• A Computer is a machine that manipulates information.
• Computer science studies includes:
– How information is organized in a computer
– How it can be manipulated
– How it can be utilized
Introduction to Data Structures
• Data is represented by the values held temporarily in the program’s data
area or recorded permanently on a file
• Different data values may be related to each other which need to be
organized to form a proper data structure.
• Program follow certain rules to access manipulate and process the
structured data.
Information – Processed data given as output
Data – Raw facts and figures given as input
4. 4
Data Structures & Algorithms
• Organization of data in a computer's memory or in a file.
• Proper choice of a data structure can lead to more
efficient programs.
• Example: Array, Stack, Queue, Linked list, Tree and
Graph.
• Represented as follows:
Data structure=Data organization + allowed operations.
Data Structure
5. 5
Primitive Data structure
• Data structures typically are directly operated upon by the machine
level instructions.
• Primitive data constitute the numbers and characters, which are
built into a programming language.
• Examples are: Integer, Boolean and Characters.
• Other data structures can be constructed from one or more
primitives.
• Simple data structures built from primitives are Strings, Arrays,
and Records
Data Structures & Algorithms
6. 6
Types of Data Structures
– Linear data structure.
– Non-Linear data structure.
• Various names used for naming the elements of a data structure
( Commonly used names are data element, data item,
item aggregate, node list and data object)
• Various data structures are used depending upon the needs and convenience
Data Structures & Algorithms
7. 7
Data Structures & Algorithms
•Elements or items form a sequence one after the other.
•Linear list can be realized in memory using either an
array or
a linked list structure.
•Data is also stored in memory locations by means of
pointers.
Examples are: Arrays, Stacks, Queues and Linked list.
Linear Data Structure
8. 8
Linear Data Structures Types
• Arrays - elements stored in memory locations in a sequential
order
• Stack is an ordered collection of data items
– inserted or deleted from one end i.e., Last In First Out (LIFO)
principle.
• Queue is an ordered list
– inserted at one end and are retrieved from the other end i.e., First
in First out (FIFO) principle.
• Linked Lists -elements stored in memory locations by means of
pointers (chain of structures).
Data Structures & Algorithms
9. 9
Non-Linear Data Structures
• Represents the hierarchical relationship between individual data
items.
• Examples are : Trees, Graphs
• Tree - hierarchical relation between its elements.
• Graph - collection of node and edges
– Node - data element
– Edge - path between two nodes.
Data Structures & Algorithms
10. 10
Operations on the Data Structures
Data Structures & Algorithms
• Traversal-is a technique in which each element is processed individually
and separately.
• Search-is an activity in which a particular record or item is to be found.
• Insertion- is a process in which a new element is added into the structure.
• Deletion- is a process in which a given item is removed from the structure
• Sorting-is a process in which all elements are arranged in a specific order
so that each item can be retrieved easily.
• Merging-is a process in which two structures are combined into a single
structure.
11. 11
Stack
• Ordered collection of items
• Item may be inserted or deleted only from the top of the stack.
• Works on Last In First Out (LIFO) concept.
• Helps backtracking or to return to the previous state.
Data Structures & Algorithms
12. 12
Basic Stack Operations
Createstack Create and Initializes a stack to be empty before it is first used
Push adds an item to a stack
Pop extracts the most recently pushed item from the stack
( removing an item from the stack.)
Full returns non-zero if the stack is full
Empty returns non-zero if the stack is empty
Data Structures & Algorithms
13. 13
Declaration of Stack
• For Contiguous implementation, set an array which hold the entries in the
stack and a counter.
• Create and Initializes a stack to be empty before it is first used
Data Structures & Algorithms
Push and Pop
• Stack is implemented using ‘Push’ and ‘Pop’ operations.
• Operations during implementation should be done with care;
For e.g.,
• popping an item from an empty stack
• pushing an item into a stack that is full should not be done.
[Create stack: create and initialize the stack to be empty]
void Createstack (stack sptr)
return [ sptr.top=0 ]
14. 14
Algorithm for push
Procedure push [s,top ,x]
[this procedure inserts an element [x] to the top of the stack.]
[check for stack overflow, maxstack is constant for maximum size]
[allowed for stacks]
if top> maxstack then
write (“Stack overflow”)
exit
[increment top]
top=top+1
[insert element]
s[top]=x
[finished]
End push
Push Operation
Data Structures & Algorithms
• First step of the algorithm checks for an overflow condition.
• Such a condition exits then the insertion cannot be performed and an
appropriate error message result.
15. 15
Pop Operation
Algorithm for pop
Procedure pop [s, top, x ]
[ this function removes the top element from a stack.]
[check for underflow on stack]
if top <=0 then
write (“Stack is Underflow”) exit
[Pop element from stack]
x=s [top]
[decrement pointer]
top = top-1
End pop
• First step itself checking underflow condition
• If exist an appropriate error message will display and the
procedure is terminated
Data Structures & Algorithms
16. 16
Other operations
[Full: returns non-zero if the stack is full]
boolean_type full(stack_type sptr)
{return sptr .top>=MAXSTACK;}
[Empty: returns non-zero if the stack is empty]
boolean - type empty (stack_type sptr)
{return sptr .top<=0;}
Data Structures & Algorithms
17. 17
Application of Stack
Stack based Language (Re_expressing the expression)
– Computers solve arithmetic expressions by restructuring them so that
the order of each calculation is embedded in the expression.
– Once converted, to the required notation (Either Prefix or Postfix), an
expression can be solved.
Types of Expression
• Any expression represent in 3 ways
• Infix, Prefix and Postfix.
• Expression 4 + 5 * 5 is represented as follows:
– Infix - Operators are embedded between operands- 4+5*5.
– Prefix - Operators precede the operands- + 4 * 5 5
– Postfix - Operators follow the operands- 4 5 5 * +
– Default representation of mathematical expression is Infix (or Polish)
notation.
Data Structures & Algorithms
18. 18
Conversion of Expression-
• Infix to Postfix
– Applying the precedence rule first for operators
– Place the Operators follow the operands
– For e.g., an expression A+(B*C)
• A + ( B * C) -Parenthesis for emphasis
• A + ( BC *) -Convert the multiplication
• A (BC *) + -Convert the addition
• ABC * + -Postfix Form
• Infix to Prefix
– Applying the precedence rule first for operators
– Place operators precede the operands
– For e.g., an expression A+(B*C)
• A + ( B * C) -Parenthesis for emphasis
• A + (* BC) -Convert the multiplication
• + A ( * BC ) -Convert the addition
• * + ABC -Prefix Form
Data Structures & Algorithms
19. 19
• Scratch pad
– Use to write down instructions that cannot be acted upon
immediately.
– Example of this is the rat-in-maze problem.
– Use to solve the problem of traversing a maze.
– Keep track of previously explored routes, or else an infinite loop
could occur.
• Text Editor
– Characters are pushed on a stack as the user enters text.
– Commands to delete one character or a command to delete a
series of characters would also push a character on a stack.
– Example, an identifier to delete one character would pop the
stack once.
– Example, an identifier to delete a sentence would pop all
characters until the stack is empty or a period is encountered.
Data Structures & Algorithms