0% found this document useful (0 votes)
4 views

Introduction

The document provides an introduction to data structures, defining them as specialized formats for organizing and storing data in memory, and categorizing them into primitive and non-primitive types. It discusses operations on both types, including creating, selecting, updating, and deleting elements, as well as traversing, inserting, and searching for elements in arrays. Additionally, the document covers sorting methods and the differences between linear and binary search algorithms.

Uploaded by

Mamata Anilgod
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views

Introduction

The document provides an introduction to data structures, defining them as specialized formats for organizing and storing data in memory, and categorizing them into primitive and non-primitive types. It discusses operations on both types, including creating, selecting, updating, and deleting elements, as well as traversing, inserting, and searching for elements in arrays. Additionally, the document covers sorting methods and the differences between linear and binary search algorithms.

Uploaded by

Mamata Anilgod
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 78

Introduction

to
Data Structures
Definition
Data: Collection of raw facts.
Data structure is representation of the
logical relationship existing between
individual elements of data.
Data structure is a specialized format for
organizing and storing data in memory that
considers not only the elements stored but
also their relationship to each other.
• For Example: We have the data student’s Name
“Akash” and age “16”. Here “Akash” is a string type
data type and 16 are of integer. We can now organize
this data as a record like a Student Record. We can
collect and store other Student’s Record in a file or a
database as a data structure.
Introduction

Data structure affects the design of both


structural & functional aspects of a program.
Program=algorithm + Data Structure
You know that a algorithm is a step by step
procedure to solve a particular function.
Data Representation:
 Computer memory is used to store the data which is
required for processing. This process is known as data
representation.
 Data may be of same type or different type.
 Data Structure provides efficient way of combining these
data types and process data.
Classification of Data Structure
Data structure are normally divided into two
broad categories:
◦Primitive Data Structure
◦Non-Primitive Data Structure
Classification of Data Structure
Primitive Data Structure
There are basic structures and directly operated upon
by the machine instructions.
Data structures that are directly operated upon the
machine-level instructions are known as primitive
data structures.
Integer, Floating-point number, Character constants,
string constants, pointers etc, fall in this category.
Primitive Data Structure
The most commonly used operation on data
structure are broadly categorized into
following types:
◦Create
◦Selection
◦Updating
◦Destroy or Delete
Operations on Primitive Data structures:
• Create: Create operation is used to create a new data structure. This operation
reserves memory space for the program elements. It may be carried out at compile
time and run time.
• Example: int n=15; // memory spaced to be created for n.
• Select: This operation is used programmers to access the data within the data
structure. This operation updates or alters data. Example: cin>>x;
• Update: This operation is used to change data of data structures. An assignment
operation is a good example. Example: int n = 5; //modifies the value of n to store
the new value 5 in it.
• Destroy: This operation is used to destroy or remove the data structure from the
memory space. In C++ one can destroy data structure by using the function called
delete
Non-Primitive Data Structure
There are more sophisticated data
structures.
The Data structures that are derived from the primitive
data structures are called Non-primitive data structure.
The non-primitive data structures emphasize on
structuring of a group of homogeneous (same type)
or heterogeneous (different type) data items.
Non-Primitive Data Structure
Linear Data structures:
◦ Linear Data structures are kind of data structure that has homogeneous
elements.
◦ The data structure in which elements are in a sequence and form a liner
series.
◦ Linear data structures are very easy to implement, since the memory of the computer is also organized in
a linear fashion.
◦ Some commonly used linear data structures are Stack, Queue and Linked
Lists.
Non-Linear Data structures:
◦ A Non-Linear Data structures is a data structure in which data item is
connected to several other data items.
◦ Non-Linear data structure may exhibit either a hierarchical relationship or
parent child relationship.
◦ The data elements are not arranged in a sequential structure.
◦ The different non-linear data structures are trees and graphs.
Non-Primitive Data Structure
The most commonly used operation on data
structure are broadly categorized into following
types:
◦ Traversal
◦ Insertion
◦ Selection
◦ Searching
◦ Sorting
◦ Merging
◦ Destroy or Delete
Operations on Non-Primitive Data
structures:
1.Traversing: The processing of accessing each element exactly
once to perform some operation is called traversing.
2.Insertion: The process of adding a new element into the given
collection of data elements is called insertion.
3.Deletion: The process of removing an existing data element
from the given collection of data elements is called deletion.
4.Searching: The process of finding the location of a data element
in the given collection of data elements is called as searching.
5.Sorting: The process of arrangement of data elements in
ascending or descending order is called sorting.
6.Merging: The process of combining the elements of two
structures to form a single structure is called merging.
Different between them
A primitive data structure is generally a basic structure
that is usually built into the language, such as an integer, a
float.

A non-primitive data structure is built out of primitive


data structures linked together in meaningful ways, such as
a or a linked-list, binary search tree, AVL Tree, graph etc.
Arrays:
 An array is an ordered collection of elements
of same data type that share common name.
 The elements are arranged one after the
other in adjacent memory location.
 These elements are accessed by numbers
called subscripts or indices.
 The elements are accessed using subscripts;
arrays are also called as subscripted variables.
Arrays
Simply, declaration of array is as follows: int
arr[10]
Where int specifies the data type or type of
elements arrays stores.
“arr” is the name of array & the number specified
inside the square brackets is the number of elements
an array can store, this is also called sized or length
of array.
Calculating the length of the array:
 A[n], the number of n is called the size or length of
the array.
 The length of the array can be calculated by:
• L = UB – LB + 1
 Here, UB is the largest Index and LB is the smallest
index
 Example: If an array A has values 10, 20, 30, 40, 50,
stored in location 0,1, 2, 3, 4 the
• UB = 4 and LB=0
 Size of the array L = 4 – 0 + 1 = 5
Arrays
◦The elements of array will always be stored in the
consecutive (continues) memory location.
◦Array can always be read or written through loop.
For(i=0;i<=9;i++)
{
scanf(“%d”,&arr[i]);
printf(“%d”,arr[i]);

}
Arrays
types
Single Dimension Array
◦ Array with one subscript

T wo Dimension Array
◦ Array with two subscripts
(Rows and Column)

Multi Dimension Array


◦ Array with Multiple
subscripts
One dimensional array:
 An array with only one row or column is called one-dimensional
array.
 It is finite collection of n number of elements of same type such
that:
◦ can be referred by indexing.
◦ The syntax Elements are stored in continuous locations.
◦ Elements x to define one-dimensional array is:
 Syntax: Datatype Array_Name [Size];
 Where,

Datatype : Type of value it can store (Example: int, char, float)


Array_Name: To identify the array.
 Size : The maximum number of elements that the array can hold.
Basic operations of Arrays
Some common operation performed on array are:
◦Traversing
◦Searching
◦Insertion
◦Deletion
◦Sorting
◦Merging
Basic operations on One-
dimensional array:
1. Traversing : Processing each element in the
array.
2. Insertion : Inserting a new element into the
array.
3. Deletion : Removing an element from the
array.
4. Searching : Finding the location of an element
in the array.
5. Sorting : Arranging the elements of the array
Represent a Linear Array in
memory
The elements of linear array are stored in
consecutive memory locations. It is shown
below:
Traversing an array:
•  Traversing is the process of visiting each subscript at
least once from the beginning element to last element.
· For example, to find the maximum element of the array
we need to access each element of the array.

• ALGORITHM: Traversal (A, LB, UB) A is an array with


Lower Bound LB and Upper Bound UB.
• Step 1: for LOC = LB to UB do
• Step 2: PROCESS A [LOC]
• [End of for loop]
• Step 3: Exit
#include<iostream.h> Code :
#include<conio.h>;
#include<iomanip.h.>
Void main()
{
int a[10], i, n ;
clrscr();
cout<<“How many elements ? “ ;
cin>>n;
cout<<“Enter the elements :” ;
for(i=0; i<n; i++)
cin>>a[i];
cout<<“The elements are :” ;
for(int i=0; i<n; i++)
cout<<setw(4)<<a[i];
getch();
}
Inserting an element into an array:
• Insertion refers to inserting an element
into the array.
 Based on the requirement, new element
can be added at the beginning, end or any
given position of array.
 When an element is to be inserted into a
particular position, all the elements from
the asked position to the last element
should be shifted into higher order
• For example: Let A[5] be an array with items 10, 20, 40, 50, 60
stored at consecutive locations. Suppose item 30 has to be inserted
at position 2. The following procedure is applied.
• o Move Number 60 to the position 5.
• o Move Number 50 to the position 4.
• o Move Number 40 to the position 3.
• o Position 2 is blank. Insert 30 into the position 2 i.e. A[2]=30.
Deleting an element into an array:
 Deletion refers to deleting an element from
the array.
 Based on the requirement, element can be
deleted at the beginning, end or any given
position of array.
 When an element is to be deleted from a
particular position, all the subsequent shifted
into lower order position.
• For example: Let A[4] be an array with items 10, 20, 30, 40, 50
stored at consecutive locations. Suppose item 30 has to be deleted
at position 2. The following procedure is applied.
• o Copy 30 to ITEM, i.e. Item = 30.
• o Move Number 40 to the position 2.
• o Move Number 50 to the position 3.
ALGORITHM: Delete (A, N, ITEM, Pos) A is an
array with N elements. ITEM is the element to be
deleted in the position Pos and it is stored into
variable Item.
Step 1: ITEM = A [Pos]
Step 2: for I = Pos down to N-1
A[ I ] = A[I+1]
[End of for loop]
Step 3: N = N-1
Step 4: Exit
Searching an element in an array:

 Searching refers to finding the location of


the element in a linear array.

 There are different algorithms, but the


most common methods are linear search
and binary search.
Linear Search:
This is the simplest method in which the
element to be searched is compared
with each element of the array by one
from the beginning to end of the array.

 Searching is one after the other, it is


also called as sequential search.
ALGORITHM: Linear_Search (A, N, Element) A is an array with N elements.
Element is the being searched in the array.

Step 1: LOC = -1 [Assume the element does not exist]


Step 2: for I = 0 to N-1 do
if ( Element = A [ I ] ) then
LOC = I
Goto Step 3
[End if]
[End of for loop]
Step 3: if ( LOC >= 0 ) then
Print Element, “Found in Location”, LOC
else
Print Element, “Not Found”
Step 4: Exit
• Example: Consider the following elements stored in an array and
we are searching for the element 17. The trace of the algorithm is
given below.
Binary Search:
 When the elements of the array are in sorted order, the
best method of searching is binary search.
 This method compares the element to be searched with the
middle element of the array.
 If the comparison is searched either at the right half of the
array or at the left half of the array.
 Let the position of first (BEG) and last (END) elements of
the array. The middle element A[MID] can be obtained by
find the middle location MID by MID= (BEG+END)/2.
•  If A[MID]= ELE, then search is successful.
Otherwise a new segment is found as follows:

 If ELE < A[MID], searching is continued at left half of the array.


Reset
END = MID - 1.

 If ELE > A[MID], searching is continued at right half of the array.


Reset BEG = MID + 1.

 If ELE not found then we get a condition BEG > END. This results
in unsuccessful search.
• ALGORITHM: Binary_Search (BEG, END, MID ELE) A is an array with N elements. Let BEG, END, MID
denote Beginning, end and middle location of the array
• Step 1: Set BEG=LB, END=UB LOC = -1
• Step 2: While(BEG <= END)
• MID = (BEG+END)/2
• if ( ELE = A [ MID ] ) then
• LOC = MID
• Goto Step 3
• else
• if( ELE < A[MID])
• END=MID-1;
• else
• BEG=MID+1;
• [End if]
• [End if]
• [End of While loop]
• Step 3: if ( LOC >= 0 ) then
• Print Element, “Found in Location”, LOC
• else
• Print Element, “Search is Unsuccessful”
•  Example: Consider the following elements stored in an array and
we are searching for the element 47. The trace of the algorithm is
given below.
• Example: Consider the following elements stored in an array and we are
searching for the element 67. The trace of the algorithm is given below.
Difference between Linear Search and
Binary Search
Linear Search Binary Search
This can be used in sorted and This can be used only in sorted
unsorted array array

Array elements are accessed One must have direct access


sequentially to the middle element in the
sub list.
Access is very slow Access is faster.
This can be used in single and Used only in single
multi dimensional array dimensional array.

This technique is easy and Complex in operation


simple in implementing
Sorting the elements in an array:

• Sorting is the arrangement of elements of the


array in some order.

•  There are different sorting methods like


Bubble Sort, Selection Sort, Shell Sort, Quick
sort, Heap Sort, Insertion Sort etc.
Insertion Sort:
• In Insertion sort, the first element of
the array is assumed to be in the correct
position next element is considered as the
key element and compared with the
elements before the key element and is
inserted in its correct position.
• Example: Consider the following array contains
8 elements as follows:
A[0] A[1] A[2] A[3] A[4] A[5] A[6] A[7]
45 26 23 56 29 36 12 4
• ALGORITHM: Insertion_Sort (A, N) A is an array with N unsorted
elements.
Step 1: for I=1 to N-1
Step 2: J = I
While(J >= 1)
if ( A[J] < A[J-1] ) then
Temp = A[J];
A[J] = A[J-1];
A[J-1] = Temp;
[End if]
J = J-1
[End of While loop]
[End of For loop]
Step 3: Exit
Two dimensional array:
· A two dimensional array is a collection of elements and
each element is identified by a pair of subscripts. ( A[5]
[5] )
 The elements are stored in continuous memory locations.
 The elements of two-dimensional array as rows and
columns.
 The number of rows and columns in a matrix is called as
the order of the matrix and denoted as mxn.
[0]
 The number of elements can be obtained [1] multiplying
by [2]
number of rows and number A[0] 1
of columns. 2 3
A[1] 7 8 9
A[2] 4 5 6
Representation of Two Dimensional Array:
· A is the array of order m x n. To store m*n number of elements,
we need m*n memory locations. The elements should be in
contiguous memory locations.
•  There are two methods:
i. Row-major method
ii. Column-major method

• Row-Major Method: All the first-row elements are stored in


sequential memory locations and then all the second-row elements
are stored and so on.

• Column-Major Method: All the first column elements are stored


in sequential memory locations and then all the second-column
[0] [1] [2]
A[0 1 2 3
]
A[1 7 8 9
]
A[2 4 5 6
]
Example :Consider the array A of order 25 X 4 with base value 2000
and one word per memory location. Find the address of A[12][3] in
row-major order and column – major order
Given : Base(A) = 2000 m = 25 n=4 LB = 0
W=1 I = 12 J=3

Row - major order : LOC(A[I][J]) = Base(A) + W[n(I-LB) + (J-LB)


LOC(A[12][3]) = 2000 + 1[4(12-0) + (3-0)]
= 2000 + 4(12) + 3
= 2000 + 48 + 3
= 2051

Column-major order :LOC(A[I][J]) = Base(A) + W[(I-LB) + m(J-LB)


LOC(A[12][3]) = 2000 + 1[(12-0) + 25(3-0)]
= 2000 + 1(12 + 75)
= 2000 + 87
= 2087
Advantages of Array:
•  It is used to represent multiple data items of
same type by using single name.
•  It can be used to implement other data
structures like linked lists, stacks, queues, tree,
graphs etc.
•  Two-dimensional arrays are used to represent
matrices.
•  Many databases include one-dimensional arrays
whose elements are records.
Disadvantages of Array:

•  We must know in advance the how many elements are to be


stored in array.
•  Array is static structure. It means that array is of fixed size.
The memory which is allocated to array cannot be increased or
decreased.
•  Array is fixed size; if we allocate more memory than
requirement then the memory space will be wasted.
•  The elements of array are stored in consecutive memory
locations. So insertion and deletion are very difficult and time
consuming.
Stack
Stack is a linear data structure which follows a particular order in
which the operations are performed.
Insertion of element into stack is called PUSH and deletion of
element from stack is called POP.
The order may be LIFO(Last In First Out) or FIFO(First In First
Out).
Representation of Stack in Memory
The stack can be implemented into two ways:

◦Using arrays (Static implementation)


◦Using pointer (Dynamic implementation)
Representation of Stacks
using Array:
Stack can be represented using a one-
dimensional array.
 The items into the stack are stored in a
sequential order from the first location of the
memory block.
 A pointer TOP contains the location of the top
element of the stack.
 A variable MAXSTK contains the maximum
number of element that can be stored in the stack.
• If we attempt to add new element beyond the
maximum size, we will encounter a stack
overflow condition. Similarly, you cannot remove
elements beyond the base of the stack. If such is
the case, we will reach a stack underflow
condition.
•  The condition TOP = MAXSTX indicates that
the stack is full and TOP = NULL indicates that
stack is empty.
Stack
Conditions
Operation on Stacks:
· Stack( ): It creates a new stack that is empty. It needs no parameter and
returns an empty stack.

· push(item): It adds a new item to the top of the stack.

· pop( ): It removes the top item from the stack.

· peek( ): It returns the top item from the stack but does not remove it.

· isEmpty( ): It tests whether the stack is empty.

•  size( ): It returns the number of items on the stack.


PUSH
Operation
The process of adding one element or item to the
stack is
represented by an operation called as the PUSH operation.
PUSH
Operation:
 The process of adding one element or item to the stack is represented by an
operation called as the PUSH operation.
 The new element is added at the topmost position of the stack.
ALGORITHM:
PUSH (STACK, TOP, SIZE, ITEM)
STACK is the array with N elements. TOP is the pointer to the top of the element of the array.
ITEM to be inserted.
Step 1: if TOP = N then [Check Overflow]
PRINT “ STACK is Full or Overflow”
Exit
[End if]
Step 2: TOP = TOP + 1 [Increment the TOP]
Step 3: STACK[TOP] = ITEM [Insert the ITEM]
Step 4: Return
POP
The process of
Operation
deleting one element or item from the stack is
represented by an operation called as the POP operation.
When elements are removed continuously from a stack, it shrinks at
same end i.e., top
POP
Operation
The process of deleting one element or item from the stack is represented by an
operation called as the POP operation.
ALGORITHM: POP (STACK, TOP, ITEM)
STACK is the array with N elements. TOP is the pointer to the top of the element of the array.
ITEM to be inserted.
Step 1: if TOP = 0 then [Check Underflow] PRINT “ STACK is Empty or
Underflow”
Exit [End if]

Step 2: ITEM = STACK[TOP] [copy the TOP


Step 3: TOP = TOP - 1 Element] [Decrement
Step 4: Return the TOP]
Application of Stacks:
• It is used to reverse a word. You push a given word to stack –
letter by letter and then pop letter from the stack.
· “Undo” mechanism in text editor.
· Backtracking: This is a process when you need to access the
most recent data element in a series of elements. Once you reach
a dead end, you must backtrack.
· Language Processing: Compiler’ syntax check for matching
braces in implemented by using stack.
 Conversion of decimal number to binary.
 Conversion of infix expression into prefix and postfix.
 Quick sort
 Runtime memory management.
Arithmetic Expression:
• An expression is a combination of operands and
operators that after evaluation results in a single
value.
• Operand consists of constants and variables.
· Operators consists of {, +, -, *, /, ), ] etc.
• Expression can be
 Infix Expression
 Postfix Expression
 Prefix Expression
• Infix Expression: If an operator is in between two
operands, it is called infix expression.
• Example: a + b, where a and b are operands and + is
an operator.
• Postfix Expression: If an operator follows the two
operands, it is called postfix expression.
• Example: ab +
• Prefix Expression: an operator precedes the two
operands, it is called prefix expression.
• Example: +ab
Algorithm for Infix to Postfix:
• 1. Examine the next element in the input.
• 2. If it is operand, output it.
• 3. If it is opening parenthesis, push it on stack.
• 4. If it is an operator, then
 If stack is empty, push operator on stack.
 If the top of the stack is opening parenthesis, push operator on
stack.
 If it has higher priority than the top of stack, push operator on
stack.
 Else pop the operator from the stack and output it, repeat step
4.
• 5. If it is a closing parenthesis, pop operators
from the stack and output them until an opening
parenthesis is encountered. Pop and discard the
opening parenthesis.

• 6. If there is more input go to step 1.

• 7. If there is no more input, pop the remaining


operators to output.
Example: Suppose we want to convert 2*3/(2-1)+5*3
into postfix form.
Consider the following examples of converting from
infix to postfix.
Consider the following examples of converting
from infix to prefix

You might also like