You are on page 1of 37

Data Structure

Basic Concepts

Data & Information


Data
Raw Facts

Information
Processed data

Data Structure (DS)


Many ways to organize the data The way in which data items are logically related and organized is called DS To solve a specific problem, the data is organized in such a manner that these can be accessed easily

Types of Data Structure


The representation of a data item in computer memory is called the storage structure Many storage structure exists Divided into two main classes

Types of Data Structure


Data Structure

Linear Data Structure

Non-Linear Data Structure

Types of Data Structure


Linear DS
Type of DS, in which elements are stored in a sequential fashion E.g. Arrays, Stacks, Queues etc

Non-Linear DS
In which elements are stored in non-sequential way E.g. Trees, Graphs

Why DS needed???
Data structures organize data more efficient programs More powerful computers more complex applications.

More complex applications demand more calculations

Operations on a DS
Inserting
Add new data to existing DS

Deleting
Remove data from the existing DS

Searching
Finding the item from the DS

Operations on a DS
Traversing
Accessing the data item in the DS

Sorting
Giving some ordering look to the data

Merging
Merging two data structures (e.g. Lists or arrays into one single data structure)

How to Selecting a DS
Select a data structure as follows:
Analyze the problem to determine the resource constraints a solution must meet. Determine the basic operations that must be supported. Quantify the resource constraints for each operation. Select the data structure that best meets these requirements.

Selecting DS
The choice of data structure and algorithm can make the difference between a program running in a few seconds or many days.

Selecting DS
Some Questions to Ask:
Are all data inserted into the data structure at the beginning, or are insertions intersperse with other operations?
Can data be deleted?

Are all data processed in some well-defined order, or is random access allowed?

Simple Vs Complex DS
Simple DS
Simple data structures can hold one or more pieces of data of a single type E.g. Variable, Array

Complex DS
Composite data structures can hold several pieces of data of one or more types. Structure etc

What is Algorithm?
Step-by-step procedure to solve a specific problem is called algorithm

Sample Algorithm1
Algorithm Sum [This algorithm is used to read two Numbers from users, sum two nos and show their result on screen] Step No.1 [Read Two Values] No1 Read Value No2 Read Value Step No.2 [Compute The Sum] Sum No1 + No2 Step no.3 [Show the result on the screen] Print Sum= Sum Step No.4 [Finish] Exit

Sample Algorithm2
Algorithm Max[This algorithm is used to find the Max of two nos]

Step No.1 [Read Two Values] No1 Read Value No2 Read Value
Step No.2 [Checnk weather No1 or No2 is greater] if No1 > No2 then Max No1 Else Max No2 End If Step no.3 [Show the Max on the screen] Print Max= Max Step No.4 [Finish] Exit

Practice Algorithm
Read three nos from the user and find the maximum of three nos Read two nos from the user and then print their product on the screen

Using looping structure find the sum of first 10 natural nos

Lecture Linear Data Structures

Linear Data Structure

Arrays

Stacks

Queues

Linked Lists

Linear Data Structure

Arrays

Stacks

Queues

Linked Lists

Arrays
Linear DS Simplest type of DS
An array is collection of cells of the same type
Elements referenced by index Elements stored in successive memory locations

Arrays
Each array is given a name Elements in the array are accessed with reference to their position Each element in array has distinct position, which is termed as its Index

Arrays
An element in the array is referred by the array name and its position
Array-Name [element-index]

Arrays
Array elements are denoted by
A1, A2, A3, . . . . An Or A(1), A(2), A(3), . . . . A(n) Or A[1], A[2], A[3], . . . . . , A[n]

Array Layout
Array cells are contiguous in computer memory The memory can be thought of as an array E.g. x[6]

Arrays Length
The maximum no of elements that an array can hold is called the arrays size Arrays size can be found using the following formula
Length(array_name) = UB-LB+1 UB = Upper bound LB = Lower Bound

Arrays Length
UB = 5 LB = 0 Length (X) = UB-LB+1 = 5 -0 + 1 =6

Quiz??
How many elements will be in an array named Y, when LB= 0 and UB = 9 If the UB of an array P is 15, then how many elements can P hold

Array Representation
2000 2004 2008 2012 2016 2020

123 P[0]

456 P[1]

88 P[2]

996 P[3]

55 P[4]

443 P[5]

Array Representation.
Starting Address of the array is called its base address E.g. the base address of P is 2000 As the base address represents the starting memory address of the first element of an array, so we can easily compute the memory address of any other element given its location index

Array Representation
We use the following formula to compute the memory address of any element in the array Loc (X[Index]) = Base + Size (Index - 1)

Array Representation
2000 2004 2008 2012 2016 2020

123
P[0]

456
P[1]

88
P[2]

996
P[3]

55
P[4]

443
P[5]

Compute the memory address of third element in P Array

Loc (X[Index]) = Base + Size (Index - 1)

Array Representation
We know that Loc (X[Index]) = Base + Size (Index - 1) Index = 3 Base = 2000 Size = 4 So, Loc (P[3]) = 2000 + 4 (3-1) = 2000 + 8 = 2008

Operations on Array DS
Traversing Inserting Deleting Searching

Sorting

Algorithm
Algorithm ArraySum[This algorithm is used to find the Sum of an Array P] Step No.1 [Read Values into Array P] For I = 1 To 10 P[I] Read (Value) Loop Step No.2 [Compute the Array Sum] For I = 1 To 10 Sum Sum + P [I] Loop Step no.3 [Show the Sum on the screen] Print Sum= Sum Step No.4 [Finish] Exit

Assignment No.1 Array Algorithm


For printing Even nos from an Array For finding the Maximum no from the array Fro finding the sum of even nos from the array

For finding the product of odd nos in the array


Adding an element in array at a specific position Deleting an element from an array with the given position

Assignment Rules
Hand written Make sure to submit on or before the deadline Deadline for the first Assignment: October 19, Friday

You might also like