Вы находитесь на странице: 1из 22

Books Tenenbaum et al, Data Structures and Program Design in C, Prentice Hall Lipschtez S(Schaums Outline), Theory and

Problems of Data Structures, TMH

What is Data Structure?


A data structure is a particular way of storing and

organizing data in a computer so that it can be used efficiently. Data Structure is a group of memory locations used to represent the information used by the algorithm. It is a way to store and organize data in the structured manner. Computer's memory is divided into small parts, we use different data structures to store data in those small blocks. Example: Arrays, Linked list, Trees ,etc. Why Do we need them Computers take on more and more complex tasks. Software implementation and maintenance is difficult.

Types of Data Structures


Linear Data Structures: Linear data structure is linear if

element is adjacent to each other. It has exactly two neighbors elements to which it is connected as its previous and next member. Eg: Array, Linked list, Stack, Queue. Non Linear Data Structures: Non-Linear data structure is that if one element can be connected to more than two adjacent element then it is known as non-linear data structure. Eg: Tree, Graph.

Arrays
Array is basic data structure in which we store data in continues

memory locations. Array is variable which holds multiple elements of same type.
Generic form of arrays: data type array_name[size]; Data type: what type of data(int, float, char...)

Size: number elements you want to store.


Every element will have index value If you want to access any element we must use index value of the

element.

Stack

Stack New nodes can be added and removed only at the top Similar to a pile of dishes Last-in, first-out (LIFO) Eg: A stack of books When a person wear bangles the last bangle worn is the first one to be removed and the first bangle would be the last to be removed.

Queue
Queue Similar to a supermarket checkout line First-in, first-out (FIFO). Nodes are removed only from the head. Nodes are inserted only at the tail. The bullet in a machine gun..(you cannot fire 2 bullets at the same time).

Linked Lists
It is a list or chain of items where each item points to

the next one in the list. Each item in a linked list is called a node. Each node contains the data and also the location to the next item. START

bat

cat

sat

vat

NULL

Trees
The data structure which reflects a hierarchical relationship between various elements is called rooted tree/tree.

Graph
A graph G = (V,E) is composed of: V: set of vertices E: set of edges connecting the vertices in V An edge e = (u,v) is a pair of vertices Example: V= {a,b,c,d,e}
a b

c d e

E= {(a,b),(a,c),(a,d), (b,e),(c,d),(c,e), (d,e)}

Static and Dynamic Data Structures


Static:
A static data structure is a data structure created for an input data set

which is not supposed to change within the scope of the problem. When a single element is to be added or deleted, the update of a static data structure incurs significant costs, often comparable with the construction of the data structure from scratch. In real applications, dynamic data structures are used, which allow for efficient updates when data elements are inserted or deleted. Have fixed maximum size. Memory is reserved at the time of compilation. Eg: Array

Dynamic: Have flexible size Memory allocation for the data structure takes place at the run time, only required amount of memory is allocated. Eg: Linked List

Address Calculation in Linear Arrays


A linear array is a list of a finite number n of

homogeneous data elements. (a) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. (b) The elements of the array are referenced respectively by an index set consisting of n consecutive numbers. n- length/ size if index set consists of 1,2,3n

Length= UB-LB+1
UB= largest index LB=lowest index Let LA be liner array K- index LOC(LA[k])- Address of the element LA[k] of the array

LA. LOC(LA[K])= Base(LA)+w(K-LB)

Eg
Consider the array AUTO which records the number of

automobiles sold each year from 1932 through 1984. given Base(AUTO)=200, w=4. Find the address of the array element for the year 1965. LOC(AUTO[1965]=Base(AUTO)+w(1965-LB) =200+4(1965-1932)=332

Two Dimensional Array


Column major order- subscript (1,1)(2,1)(3,1)- 1

column Row major order-(1,1)(1,2)(1,3)- 1 row m*n array LOC(A[j,k]=Base(A)+w[M(k-1)+(j-1)]- column major LOC(A[j,k]=Base(A)+w[N(j-1)+(k-1)]- row major

General Multidimensional Arrays


Li- length of ith dimension
Li=UB-LB+1 Ei=Ki-LB Effective Index Coloum major Base(c)+w((EnLn-1+En-1)Ln-2)++E3)L2+E2)L1+E1) LOC(C[K1,k2Kn])=base(C)+w((E1L2+E2)L3+E3)L4+ +En-1)Ln+En) n is dimension

Eg
Suppose a 3d array MAZE is declared using MAZE(2:8,

-4:1, 6: 10), base(MAZE)=200, w=4. find loc of MAZE[5,-1,8] L1=8-2+1=7, L2=1-(-4)+1=6, L3=10-6+1=5 Total elements in maze= li.l2.l3=7.6.5=210 E1=5-2=3, E2=-1-(-4)=3, E3=8-6=2 Row major E1L2=3.6=18 E1L2+E2=18+3=21 (E1L2+E2)L3=21.5=105

(E1L2+E2)L3+E3=105+2=107
LOC(MAZE[5,-1,8])=200+4(107)=200+428=628

Sparse Matrices
Matrices with a relatively high portion of zero entries

are called sparse matrices. If all entries above the main diagonal are zero is known as lower triangular matrices. If non zero entries can only occur on the diagonal or on elements immediately above or below the diagonal, is called a tridiagonal matrix. B[1]=a11, B[2]=a21, B[3]=a22 4 5 6 3 -5 2 4 8 1 9 6 9 3 6

Traversing Linear Array( Printing/counting elements)


1. [initialize counter] Set K:=LB 2.Repeat Step 3 and 4 while K<=UB 3.

[Visit Element] Apply Process to LA[K] 4. [Increase Counter] Set K:=K+1 [end of step 2 loop] 5. Exit

Inserting into a linear array (N no of elements not the size of array )


INSERT(LA,N,K,ITEM)
1. [Initialize counter] Set J:=N 2. Repeat Steps 3 and 4 while J>=K 3.

[Move Jth element downward] Set LA[J+1]:=LA[J] 4. [Decrease Counter] Set J=J-1 [End of step 2 loop] 5. [Insert element] Set LA[K]:= ITEM 6. [Reset N] Set N=N+1 7. Exit

Deleting from a Linear Array


DELETE(LA,N,K,ITEM)
1. Set ITEM:=LA[K] 2. Repeat for J=K to N-1

[Move J+Ist element upward] Set LA[J]=LA[J+1] [End of Loop] 3. [Reset the number N of elements in LA] Set N=N-1 exit

Вам также может понравиться