Sie sind auf Seite 1von 44

Introduction to Data Structure Click to edit Master subtitle style

4/25/12

Topics

Data Structure Arrays Records & Pointers Multidimensional Arrays Pointer Arrays Record Structure
4/25/12

Data

Data simple values or sets of values Data Item refers to a single unit of values Data item divided into sub items are called group items Data item not divided into sub items are called elementary items For example employees name divided into first

4/25/12

Data

Entity that has certain attributes or properties which may be assigned values. Values may be numeric or non numeric Employee

Attributes: Name Values: Arpit

Age 20

Sex M

SSN 134-24-5533

Entity set Entities with similar attributes The way that data are organized into the hierarchy of fields, records and files reflects the relationship between attributes, entities and entity sets.
4/25/12

Data
Data organized as Fields Records File
Fixed Length Records Variable Length Records

The data may be organized into many such different ways the logical and mathematical model of a particular organization of data is called data structure The way information is organized in the memory of a computer is called a data structure. A data structure helps you to understand relationship of one data element with the other and organize it within the memory. 4/25/12

Introduction to Data Structures


way of organizing all data items considers not only elements stored but also their relationship to each other. Specifies fallowing

Organization of data. Accessing methods. Degree of associativity. alternatives for information.

Processing 4/25/12

Classification of Data structure

Preeti S. Patil

Introduction to Data structure MPSTME, SHIRPUR

Primitive Data Structure


Basic structures Directly operated upon by the machine instructions

4/25/12

Non primitive Data structure


v

Derived from primitive data structure. Emphasize on structuring of a group of homogeneous or heterogeneous data items Eg:

Arrays Lists

Files 4/25/12

Data Structure Operations

The data in data structures are processed by means of certain operations The four main operations:

Traversing Processing each element in the list. Searching Inserting Deleting Sorting Merging

4/25/12

Arrays, Records and Pointers Click to edit Master subtitle style

4/25/12

Data Structure

Data Structure : classified as either linear or non linear Linear data structure: if its elements form a sequence or linear list There are two basic ways of representing such linear structures in memory:

One way is to have the linear relationship between the elements represented by means of sequential memory locations e.g. Arrays

The other way linear relationship between the elements represented by means of pointers or links e.g. Linked Lists

4/25/12

Linear Arrays

A linear array list of finite number n of homogeneous data elements, finite collection of similar elements stored in adjacent memory locations. The elements of the array are referenced respectively by an index set consisting of n consecutive numbers The elements of the array are stored respectively in successive memory locations. n indicates total number of elements in the array size or length of the array. Length = Total number of elements = UB LB +1

4/25/12

Linear Arrays

Array declaration must give, three items of information:


The name of the array The data type of the array The index set of the array

Memory for array can be allocated in two ways:


Statically : Compile time size of array is fixed during program execution

Dynamically: Run time read value of n at run time and then allocate memory while program execution.

4/25/12

Representation of Linear Arrays in Memory Arr[] linear array


Loc(Arr[k]) = address of the element Arr[k ] of the array Arr

1000 1001 1002 1003 1004

Base(Arr)

No need to keep track of the address of every element of Arr. Track only the address of the first element of Arr, denoted by Base (Arr) Using base address, computer calculates address of any element of Arr by using below formula: Loc(Arr[k]) = Base(Arr) + w(K-Lower bound) 4/25/12

Arrays in C

Declaration of an Array in C:

Data type followed by array name.

Subscript in bracket indicates the number of elements array will hold.

By declaring an array, the specified number of memory locations are allocated in the memory

For example,

int age[20] ; float sal[10]; char 100 grade[10]; 102


int arr[5]

104 arr[2]

106 arr[3]

108 arr[4]

arr[0]

arr[1]

4/25/12

Arrays in C

Array initialization:

Can be initialized at the time of declaration:

int age[5] = [8,10,5,15,20]; float sal[3] = [2000, 2000.50,1000];

2000

2000.50 sal[1]

1000 sal[2]

sal[0]

An array of characters is called a string and it is terminated by a null (o) character. char name[3]=abc;
4/25/12

Traversing Linear Arrays

Traversing a Linear Array


LA = Linear Array LB = Lower Bound UB = Upper Bound
1.

[Initialize Counter] set K:=LB Repeat Steps 3 and 4 while K<=UB

2.

[Visit element] Apply Process to LA[K] [Increase Counter] set K:=K+1


1.

[End of Step 2 loop] Exit

2.

1.

Repeat for K=LB to UB

Apply Process to LA[K]


1.

[End of Loop]

4/25/12 Exit
2.

Inserting into a Linear N=4 NAME Arrays Inserting into a Linear Array
LA = Linear Array LB = Lower Bound UB = Upper Bound N = array with N elements K= K is a positive integer such that K<=N. Davis Smith

Brown Johnson

This algorithm Inserts an element ITEM into the Kth position in LA N=4 K=3 INSERT(LA, N, K, Item) NAME Item=Fo [Initialize counter] Set J:=N Brown rd Repeat Steps 3 and $ while J>= K J=N=4 Davis
1. 2. 3.

NAME Brown Davis Johnson Smith

[Move Jth element downward] Set LA[J+1]:=LA[J] [Decrease counter] Set J:=J-1 [End of step 2 loop]

Ford Johnson Smith

4.

5.

[Insert element] set LA[K]:=Item 4/25/12


6.

Multi Dimensional Arrays

Linear arrays referenced by one sub scripts Multi Dimensional arrays- referenced by more than one subscript two or three A two dimensional M* N array A is a collection of M*N elements such that each element is specified by pair of integers such as j, k called subscripts The element of A with subscript j and second subscript k is denoted by

Aj,k or A[j][k]

Two dimensional arrays are called matrices with M rows and N columns. 4/25/12

Representation of Two Dimensional Arrays in Memory

A two dimensional m* n array in memory represented sequential memory locations. Array A can be stored in either of two ways:

as m*n

Column by column- Column major order Row by row Row Subscri major order A

Column Major Order

4/25/12

pt (1,1) (2,1) (3,1) (1,2) (2,2) (3,2) (1,3) (2,3) (3,3) (1,4) (2,4) (3,4)

Row Major Order A Subscri pt (1,1) (1,2) (1,3) (1,4) (2,1) (2,2) (2,3) (2,4) (3,1) (3,2) (3,3) (3,4)
Row 1

Column 1 Column 2

Row 2

Column 3 Column 4

Row3

Search

Linear Search: Compare search element with each element one by one in array Binary Search :

Array must be in sorted order Divide array into two halves

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

lo = Lower bound hi = Upper bound


6 1 1 2 3 4 Ex. Binary search for 33.
0

3 1

4 2

5 3

3 4

3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14 h i

l o

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

mid= (lo + hi)/2 Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4

4 3 5

5 1 6

5 3 7 mi d

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14 h i

l o

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4

4 3 5

5 1 6 h i

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

l o

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3 mi d

3 3 4

4 3 5

5 1 6 h i

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

l o

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4 l o

4 3 5

5 1 6 h i

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

l mi h o d i

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

l mi h o d i

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4 l o h i

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4 lo hi mi d

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4 lo hi mi d

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

4/25/12

Binary Search
Binary search. Given value and sorted array a[], find index i such that a[i] = value, or report that no such index exists. Invariant. Algorithm maintains a[lo] value a[hi].

Ex. Binary search for 33.


6
0

1 3 1

1 4 2

2 5 3

3 3 4 lo hi mi d

4 3 5

5 1 6

5 3 7

6 4 8

7 2 9

8 4 10

9 3 11

9 5 12

9 6 13

9 7 14

4/25/12

Binary Search Algorithm


(Binary Search) Binary(DATA, LB, UB, Item, LOC) Initially LOC = NULL ITEM=20 */
1.

/* DATA = [10, 20,30,40, 50,60] [1 2 3 4 5 6]

[Initialize segment variables.] /* BEG := 1, END:= 5

Set BEG:= LB, END:= UB and MID =INT(BEG +END)/2 MID = (1+5)/2 = 3 /*20 < DATA[MID]= /* END = MID-1= 3-1 = 2 */ /* DATA = Repeat steps 3 and 4 while BEG<=END and DATA[MID] ITEM IF ITEM < DATA[MID], then: DATA[3] =30 */ Set END := MID-1. Else: [10,20,30,40,50,60] */ Set BEG:=MID+1.
4/25/12
4.

2.

3.

[End of If Structure.] /* MID = (1 + 2)/2 = 1.5 =2 */

Set MID:= INT(BEG +END)/2.

Pointers

Pointers are special variables which contain the address of another memory location. Pointers are useful in accessing any memory location directly. An address of a memory location is a long integer which is stored in pointer type variable. Adding two pointers or subtracting two pointers gives number of bytes between two memory addresses.

4/25/12

Pointers

& - address operator represents the address of the variable. %u used for obtaining the address = printf(address of X = %u, &X); Declare base variable

int X;

Declare pointer type variable related to base variable

int *P;

Address of variable Establish relation between base Memoryand pointer variable can pointer P one

With the help of

P = &X;

1011 25 X

where value for variable X is stored

Variable name 4/25/12

1011 P

access the variable X. *P = Value at Pointer P = Value at memory location 1011 = Value of variable X = 25 Pointer Type Variable

Pointers

Another pointer variable can store the address of a pointer variable int a=2; int *b; int **c; b=&a; c=&b; 1011
2

/* c has been declared as a pointer to pointer variable which contains address of pointer variable b */
Address of Memory where value for variable a is stored 1021 1011 b 1021 c Pointer to Pointer Type Variable Pointer Type Variable

Variable name

4/25/12

Pointer Arrays

On array declaration sufficient amount of storage is allocated by the compiler The compiler also defines the name of the array as a pointer to the first element. The name arr acts as a pointer pointing to the first element. arr = &arr[0] =100; int *ip; ip = arr or ip=&arr[0];
arr[0] &arr[0] = 100; arr[1] ip = 2 4/25/12 4 ip+1 =6&arr[1] = 102; 10 ip+2 = &arr[2] = 104; 10 0 2 104 arr[2] 8 106 arr[3]

Call By Value

The process of passing the actual values of variables as arguments to a function is called call by value. Before calling 2 1 program Main()

{ int a =2; int b=1; Value(a,b);

a (1000)

b (2000)

Printf(Before Calling the function, a and b are %d %d , a, b); Printf(After calling the function, a and b are %d %d, a, b);
After p++ and q++ in } function 3 2 4/25/12 p(5000) Q(8000) After calling function 2 a(1000) 1 b(2000)

Call By Reference

Pass the addresses of the variables as parameters to the function. Before calling program
2 1

Main() { int a =2; int b=1; Printf(Before Calling the function, a and b are %d %d , a, b); 1000 2000 Value(&a,&b); Printf(After calling the function, a and b are %d %d, a, b);
After (*p)++ and (*q)++ in function 1000 2000

a (1000)

b (2000)

After calling function 3 a(1000) 2 b(2000)

Value(int 4/25/12 p(5000) *p, int *q) Q(8000)

In Function

Records

Recall that elements of arrays must all be of the same type


scores : 85 79 92 57 68 80 . . .

In some situations, we wish to group elements of different types


employee R. Jones 123 Elm 6/12/55 $14.75
4/25/12 4141

98 99

Record Structures

Collections of data - organized into a hierarchy of fields, records and files. Record collection of related (Not similar) data items, called as field or attribute. File collection of similar records. Record- collection of non homogenous data the data items in a record may have different data types. The data items in a record are indexed by attribute names.
4/25/12

Record Structures

Record for new born baby in hospital Newborn


2. Name 2. Sex 2 Birthday 3 Month 3 Day 3 Year 2 Father 3 Name 3 Age 2 Mother 3 Name

1.

4/25/12

3 Age

Record Structures
1. Student(20) 2. Name 3. Last 3. First 3. MI 2. Test(3) 2. Final 2. Grade To access last name of first student Student.Name.Last[1] To access marks of third test of first student Student.Test[1][3]

4/25/12

Das könnte Ihnen auch gefallen