data structure

12
DATA STRUCTURE Data are represented by data values held temporary or recorded permanently on a file. Often the data value are related to each other and to unable the program to make use of the relationship. This data value must be in organized form; this organized collection of data is called DATA STRUCTURE. There are 2 types of data structure. 1) Primitive data structure: Integer, float, character are its example. 2) Non-primitive data structure: It’s further classified in linear and nonlinear data structure. a) Linear data structure: In linear data structure the item are arrange in linear sequence like an array. b) Nonlinear data structure: In this data structure items are not in sequence. Non-primitive data structure is further classified as homogeneous and non-homogeneous data structure. a) Homogeneous data structure: In this data structure all the elements are of same type like array. b) Non-homogeneous data structure:In this data structure all elements are not of same type. So, array is non-primitive nonlinear, homogeneous data structure. Data may be organized in many different ways: the logical or mathematical model of a particular organization of data is called data structures. Choice of a particular data mode depends on two considerations: it must be rich enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the structure should be simple enough that one can effectively process the data when necessary. In other word data structure is a collection of data elements whose organization is characterized by assessing operations that are used to store and retrieve the individual data elements effectively. 1

Upload: onlytej

Post on 19-Nov-2014

3 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Data Structure

DATA STRUCTURE Data are represented by data values held temporary or recorded permanently on

a file. Often the data value are related to each other and to unable the program to make use of the relationship. This data value must be in organized form; this organized collection of data is called DATA STRUCTURE.

There are 2 types of data structure.1) Primitive data structure: Integer, float, character are its example.2) Non-primitive data structure: It’s further classified in linear and nonlinear data structure.

a) Linear data structure: In linear data structure the item are arrange in linear sequence like an array.

b) Nonlinear data structure: In this data structure items are not in sequence. Non-primitive data structure is further classified as homogeneous and non-

homogeneous data structure.a) Homogeneous data structure: In this data structure all the elements

are of same type like array.b) Non-homogeneous data structure:In this data structure all

elements are not of same type.So, array is non-primitive nonlinear, homogeneous data structure.

Data may be organized in many different ways: the logical or mathematical model of a particular organization of data is called data structures.

Choice of a particular data mode depends on two considerations: it must be rich enough in structure to mirror the actual relationships of the data in the real world. On the other hand, the structure should be simple enough that one can effectively process the data when necessary.

In other word data structure is a collection of data elements whose organization is characterized by assessing operations that are used to store and retrieve the individual data elements effectively.

Knowledge of data structure is required for people who design and develop computer program.

Data structure can be also classified as

1. Homogenous Data Structure : This is a data structure in which all element are of same type : e.g. Array int a[10]

2. Non-Homogenous Data Structure : This is a data in which all element or data may or may not be of same type. e.g. StructureStructure student{ int r_no

char name[20]float percentage

}We can also classify the Non-Primitive data structure as:1. Static Data Structure : Static data structure are those whose size and structure

associated with memory location are fixed at compile time . e.g. Array int no[5]

2. Dynamic Data Structure : Dynamic structures are those which expand or shrink as required during the program execution. e.g. Link List

1

Page 2: Data Structure

ARRAY :

An array is an ordered set which consists of a fixed no of objects. No insertion or deletion operation are performed on array. We can perform the delete operation by setting of an element in an array to zero.

We can not remove the element from the array but we can mark it for deletion.

INTRODUCTIONAn array is a group of related data items that share a common name.

This data structure is useful in maintaining table and queue in several applications. Some common examples are, An array for storing the rainfall of 365 days of a year,An array of scores of a set of team, A bit matrix table for computer screen,A symbol table, etc…We can define an array name salary to present a set of salaries of a group of employees. Writing a number called index number or subscript in brackets after the array name indicates a particular value. For example,

Salary [10];Represent the salary of the 10th employee. While the complete set of values is referred to as an ARRAY, the individual values are called elements. Arrays can be of any variable type.

The ability to use a single name to represent a collection of items and to refer to an item by specifying the item number enables us develops concise in efficient program. For example: A loop with the subscript as the control variable can be used to read the entire array, perform the calculation and, printout the results.

TYPES OF ARRAYS:

ONE-DIMENSIONAL ARRAYS:A list of items can be given one variable name using only one subscript and such a

variable is called single subscripted value or one-dimensional array. Storage structure for one-dimensional array

The memory of computer is linear and one dimensional array is started in continuous memory cells storage of an element a [I+1] will be adjacent to the storage for element a[I],for I=1 to n.

2

Array

Static Dynamic

Fixed

Variable

Limited

Unlimited

Page 3: Data Structure

The declaration new line (int a [100]) reserves 100 successive memory locations each large enough to contain a single integer. The address of the first of this location is called the base address of the array ‘a’ and is denoted by base (a).

If ‘s’ is the size of each individual element of the array then the reference to the element [0] is to the element at location base (a) then reference to a [1] is to the element at base (a) + s. a reference to a [2] is the element at base (a)+ 2* s. In general the reference to a [I] is to the element at location base (a)+I*s.Declaration of one dimensional arrays

Like any other variable, array must be declared before they are used. The general form of array declaration is,

Type variable name [size];The type specifies the type of element that will be contained in the array, such as int,

float or character and the size indicates the maximum number of element that can be stored inside the array. For example,

Float height [50];Declares the height to be an array containing 50 real elements. And subscripts 0 to 49 are valid.

Similarly, Int group [10];

Declares the group as an array to contain a maximum of 10 int constant. Remember, any reference to the array out side the declared limits would not necessarily cause an error. Rather, in might result in unpredictable program results.

Initialization of one dimensional arraysArray has one disadvantage, as compare to dynamic data structure that it’s size must

be preceded at the beginning of the program.Static type array _ name [size] = {list of value};

This statement initializes the element of the array. Which is stored in the static section of the memory ex; static int no [3]= {0,0,0};Will declare the variable no. as an array of size 3 and will assign 0 to each element. If the no of values in the list is less than the no. of elements, they only that many elements will be initialized. The remaining elements will be set to zero automatically.For example: static int no []={10,20,30};

It will automatically declare the three element of variable ‘no’.

TWO-DIMENSIONAL ARRAYS:Array is used to stored the list of value sometime there will situation where a value in

the form of table to be stored. Storage structure for two-dimensional array

in a two dimensional array the data is arranged in rows and column , however the machine memory is arranged as a row of memory all. In a two dimensional array the data is started in cells row by row that is used first started the first row of the array then the second row of the array and then the next the so on. such a storage scheme is called row major order.

The alternative is to store in an array column by column, it is called column major order.

Let supposed that a two dimensional array integer array is store in row major sequence and let base (a) be the address of first element at the array a [r1][r2] are the range of

3

Page 4: Data Structure

first and second dimensional array respectively. Base [a] is the address of a [0][0]. If ‘s’ is the size of each element then the address of any element a[I1][i2] can be calculated as ,

Base (a)+(i1*r1+i2)*sWhere, -a is array name,-if row major then i1 is the row of an array and i2 is column of an array,-if column major then i1 is column of an array and i2 is row of an array;-s is the size of element.

Figure, followed for row major,

Column (i2=0) column (i2=1)Base (a)+(i1*r1+i2)*s Base (a)+(i1*r1+i2)*s

Base (a)+(i1*r1+i2)*s Base (a)+(i1*r1+i2)*s

Initialization of tow dimensional arraysLike the one-dimensional arrays, following their declaration with a list of initial value

enclosed in braces may initialize two-dimensional arrays. For example,Static int table [2][3] = {0,0,0,1,1,1};

Initializes the element of the first row to 0 and the second row to 1,.the initialization is done row by row. The above the above statement can be equivalently written as,

Static int table [2][3] = {{0,0,0},{1,1,1}};By surrounding the elements of each row by braces.

MULTI DIMENSIONAL ARRAY:

the different compiler allows different level or dimensional array . C allows arrays of three or more dimensions. The compiler determines the exact limit. The general form of a multi-dimensional array is

Type array name [s1][s2][s3]….[sn];Where sn is the size of the n th dimensional for ex.

Int table [5][4][3];In the three-dimensional array the row column and the height must be declared. The

above stated statement declare the three dimensions. The first value shows the row, the second shows column of the array and the third value shows the height of the array. The height of array is known as level. The logical memory the elements are stored as,

1) First value is stored for first level.

2) In the first as row major sequence And so on for each and every level of size.

Operation for array

Number [0][0] Number [0][1]Number [1][0] Number [1][1]Number [2][0] Number [2][1]

4

Row (i1=0)

Page 5: Data Structure

There are various operations perform in array that are describe as below Creating and initializing an array in this operation array of any dimensional

must be allocated memory and value which is assign default should be declared. The size of an array must be fixed precisely that is called creation or construction of an array. While assigning value at the time of creation is called initialization of an array.

Storing an element in array during the running of the program we many times storing the value inside the array with using index no of an array. For this in c programming many times we use loop or iteration. Int a [5];For (int i=0;I<5;I++){

Cout<<”enter the value=”;Cin>>a [I];

} Inserting an element in array

In this type of operation the value for an array is inserted during the execution time of the program.

Deleting element of an array: in this type of operation the value of array can be deleted the execution time of program.

Sorting of the element in array it is the very important operation of an array through with all the elements or the record of the array can be ordered in the ascending or descending order.There is the various type of sorting.1) Bubble sort 2) Selection sort 3) Insertion sort 4) Shell sort 5) Quick sort Searching the element of an array –->this operation have most advantages

over the link list because in link list if we want to search any record or elements, we must travel through out the list but in array we can addressing or manipulating the record or element with the use of index or subscript. There are the various types of searching technique.1) Sequential search 2) Binary search

APPLICATION OF AN ARRAY :

1) Matrix The array having the application in matrix representation. By various mechanisms matrix representation is carried out. a) Sparse matrix b) Graph representation of matrix through array

2) Object as array:Through this application in the c programming, class structure or any user define function can be used in the form of array it is called object of array

3) StackThis is the most useful application of array. It permits insertion and deletion of element from only one end defined as stack. The end is called the top of the stack. We can access element in opposite order from that in which we were added . this

5

Page 6: Data Structure

phenomenon is referred as LIFO. It is very useful in the prefix, postfix and infix expression.

4) QueueThis is the another important application of array. it permits deletion to be performed at one end called front . of list and insertion at other end called rear. The information in list processed in some order in which it was received that is first in first out (FIFO). Or first come first serve (FCFS) basis. the type of list referred as queue.

Storage Structure For Array :

The simple data structure that makes use of computed address to locates elements is the one dimensional array called vector.

Normally a number of memory location sequentially allocate to the vector. Assuming that each element require one word of memory an N element vector

will occupy N consecutive word in memory. A vector size is fixed and there for require fixed number of memory locations. In general , a vector A with a subscript lower bound of ‘One’ can be represented

pictorially as in figure.

STACK :

In the most general form of linear list, we are allow to delete an element from and insert an element to any position in the list.

An Important subclass of lists permits the insertion or deletion of an element to occur only at one end. A linear list belonging to this subclass is called a stack.

The insertion operation is referred to as “push” and the deletion operation as “pop” The most and least accessible element in a stack are known as TOP and BOTTOM of

stack respectively

Since insertion and deletion operation are performed at one end of stack, the elements can only be removed in the opposite order from that in which they are added to the stack such a linear list frequently to as LIFO ( Last In First Out).

The familiar example of Stack is a pile of trays in cafeteria. These are supported by some kind of spring action in such a manner that person

desiring a tray finds that only one is available to him or her at the surface of the tray counter. The removal of the top tray causes the load on the spring to be lighter and the next tray to appear at the surface of the counter. A tray which is placed on the pile causes the entire pile to be pushed down and that tray to appear above the tray counter such an arrangement of tray is shown in figure.

OPERATION ON STACK : The representation of stack by a vector is as given bellow : A Pointer TOP keep track of the top element in the stack Initially when the stack is empty TOP has a value of ‘zero’, and when stack contain

single element TOP has value ‘one’ and so on. Each time a new element is inserted in the stack the pointer is incremented by ‘one’

before the element is placed on the stack. The pointer is decremented by ‘One’ each time a deletion is made from stack.

QUEUE :

6

Page 7: Data Structure

The important subclass of list permits deletion to be performed at one end of a list and insertion at the other.

The information in such a list is processed in same order as it was received that is on first in , first out (FIFO) or First come , First served (FCFS) basis. This type of list is frequently referred to as queue.

Figure shows the representation of queue illustrating how an insertion is made to the right of the rightmost element in queue and how a deletion consists of deleting the left most element in the queue.

The familiar and traditional example of a queue is a checkout line at a supermarket cash register. The first person in line is the first to be checked out.

More relevant example of a queue can be found in a time sharing computer system where many user share the system simultaneously. Since such a system typically has a single central processing unit and one main memory these resources must be share by allowing one user program to execute for a short time followed , followed by the execution of another user’s program, etc., and processed the information in a queue pattern.

This queue may not operate on a strictly first in , first out basis but on some complex priority scheme based on such factors as what compiler is being used, the resulting queue is sometimes called a priority queue.

An algorithm for inserting an element in a queue is as given bellow. In Case of Queue, we can not reuse the space on which deletion is performed until

Queue becomes empty as shown in figure. A more suitable method of representing a QUEUE which prevents and excessive use

of memory is to arrange the element Q[1], Q[2] …Q[n] in a circular fashion with Q[1] following Q[n]

DEQUE : A Deque ( Double ended queue) is a liner list in which insertion and deletion are made to or from either end of the structure such a structure can be represented by figure as given bellow.

It is clear that a deque is more general than a stack or queue. There are two variation of deque, namely the input-restricted deque and the output restricted deque. The input restricted deque allows insertion at only one end, while an output restricted deque permits deletion from only one end.

APPLIACTION OF QUEUE (simulation).

◌ It is one of the most useful application of queue, priority queue & link list.◌ A simulation program attempts to model a real world situation. In order to learn

something about it. As it would be too expensive or dangerous to experiment with the real system. Each object & action in real situation as it’s counter part in the program.

◌ If program successfully mirrors real world situation, the results of the program should mirror.

◌ The result of actions been simulated that it is possible to understand both occurs in the rear world situation without actually observing it’s occurrence.

◌ Ex: They are physical simulators such as flights simulators used to train and air pilot.

7

Page 8: Data Structure

◌ In order to simulate a system a model of the system must be produce & to determine the structure of a model for some situation, the entities, attributes & activities of the system should be determine.

◌ An entity represents the components of the system & is the object that is persons, places or things of interest in the simulations.

◌ Attributes denote the characteristics of these entities. The state of the system at any given time is specified by the attributes of the system entities and the related among the entities at that time.

◌ An activity is a process which causes a change of system state & event is the occurrences of an activity at a particular instant of time. If we consider a bank system an example entities should be customer. The attribute for the customer could be the balance in the account & there credit rating where as the activity might include deposit or withdraw & even would occur when a customer enters or leave the queue.

8