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 .

Queues etc • Non-Linear DS – In which elements are stored in non-sequential way – E.g. in which elements are stored in a sequential fashion – E. Stacks.g. Arrays. Trees. Graphs .Types of Data Structure • Linear DS – Type of DS.

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

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 .

Lists or arrays into one single data structure) .g.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.

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

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

or is random access allowed? . or are insertions intersperse with other operations? – Can data be deleted? – Are all data processed in some well-defined order.Selecting DS • Some Questions to Ask: – Are all data inserted into the data structure at the beginning.

g.Simple Vs Complex DS • Simple DS – Simple data structures can hold one or more pieces of data of a single type – E. – Structure etc . Array • Complex DS – Composite data structures can hold several pieces of data of one or more types. Variable.

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

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

Sample Algorithm…2 Algorithm Max[This algorithm is used to find the Max of two nos] Step No.3 [Show the Max on the screen] Print “Max=” Max Step No.1 [Read Two Values] No1  Read Value No2  Read Value Step No.4 [Finish] Exit .2 [Checnk weather No1 or No2 is greater] if No1 > No2 then Max  No1 Else Max  No2 End If Step no.

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 .

which is termed as its Index .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.

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

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

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

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. then how many elements can P hold . when LB= 0 and UB = 9 • If the UB of an array P is 15.

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. so we can easily compute the memory address of any other element given its location index . • Starting Address of the array is called its base address • E. the base address of P is 2000 • As the base address represents the starting memory address of the first element of an array.g.

1) .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) Index = 3 Base = 2000 Size = 4 So. Loc (P[3]) = 2000 + 4 (3-1) = 2000 + 8 = 2008 .Array Representation We know that Loc (X[Index]) = Base + Size (Index .

Operations on Array DS • Traversing • Inserting • Deleting • Searching • Sorting .

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

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