data structures using c

32
Prof. K. Adisesha [email protected]

Upload: adisesha-k

Post on 12-Apr-2017

323 views

Category:

Education


2 download

TRANSCRIPT

Page 1: Data structures using c

Prof. K. [email protected]

Page 2: Data structures using c

Objectives :• To understand how various data structures can be classified• To understand the most commonly used, basic data types and data arrays• To understand the characteristics and mechanisms of problem-oriented data structures used to solve specific problems, as well as how to use a basic data structure for program implementation

2K. Adisesha

Page 3: Data structures using c

Data It is an entity piece of information that is fact.

Information Instruction + Data

Data Structure It is a way of organizing data that considers not only the items stored but also the relationship of each data.

3K. Adisesha

Page 4: Data structures using c

Definition Primitive data structures

Data structure which can be directly operated by machine level instruction

Non primitive data structures

Data structure which can not be directly operated by machine level instruction

Linear data structure Non-Linear data structure

4K. Adisesha

Page 5: Data structures using c

Data Structure

Problem-OrientedBasic Data Structure

List Structure

Stack

Queue

Basic Data TypeStructure TypeAbstract Data Type

Hash

TreeArray Type

Record Type Simple Type

Pointer Type

IntegerReal NumberCharacter

EnumerationPartial

Logical

5K. Adisesha

Page 6: Data structures using c

Dynamic memory allocation and pointers

Recursion Searching and Sorting Stack Queue Linked list Tree

6K. Adisesha

Page 7: Data structures using c

Basic Data Type Simple Type

Integer - represents whole number and is represented inside a computer as binary numbers of fixed-point numbers that have no significant digits below the decimal point.Real Number - represents fixed-point and floating point numbers.

Character - represents fixed-point and floating point numbers.Logical - used to perform logical operations, such as AND, OR, and NOT operations.

7K. Adisesha

Page 8: Data structures using c

Structured Type Array Type

One Dimensional Array One of the simplest and most common type of data structure. It is an ordered set consisting of a variable number of elements. The number of subscripts of an array determines its dimensionality.

ArrayX [ j ]Array Name

Element / Subscript / Index

8K. Adisesha

Page 9: Data structures using c

Structured Type Array Type

One Dimensional Array

Example:Grade [ j ] Grade [ 0 ] = 95Grade [ 5 ] Grade [ 1 ] = 85

Grade [ 3 ] = 100

Grade [ 2 ] = 75

Grade [ 4 ] = 65

9K. Adisesha

Page 10: Data structures using c

Structured Type Array Type

Two Dimensional Array An array with two subscripts. The first subscripts is called the “row”, and the second is called the “column”.

int ArrayX [ j , k ]

Array Name

indexBase type

10K. Adisesha

Page 11: Data structures using c

Structured Type Array Type

Two Dimensional Array

Example: Grade [ 3 , 4 ]

Grade C0 C1 C2 C3

R0 71 85 90 95

R1 97 88 78 87

R2 76 84 92 65

Grade [0,0] = 71Grade [0,1] = 85Grade [0,2] = 90Grade [0,3] = 95Grade [1,0] = 97Grade [1,1] = 88Grade [1,2] = 78Grade [1,3] = 87Grade [2,0] = 76Grade [2,1] = 84Grade [2,2] = 92Grade [2,3] = 65

Row major

11K. Adisesha

Page 12: Data structures using c

Structured Type Array Type

Two Dimensional Array

Example:

Grade [ 3 , 4 ]

Grade C0 C1 C2 C3

R0 71 85 90 95

R1 97 88 78 87

R2 76 84 92 65

Grade [0,0] = 71Grade [1,0] = 97Grade [2,0] = 76Grade [0,1] = 85Grade [1,1] = 88Grade [2,1] = 84Grade [0,2] = 90Grade [1,2] = 78Grade [2,2] = 92Grade [0,3] = 95Grade [1,3] = 87Grade [2,3] = 65

Column major

12K. Adisesha

Page 13: Data structures using c

ExerciseAn array has an index of x[3..8] and

start at the address 245. It has 4 words per memory cell. What will the location of element x[5]?To get the location of elements

To get the number of elements in an array

Loc [k] = base + w (k-LB)

NE = UB – LB + 1

13K. Adisesha

Page 14: Data structures using c

Memory Map

3 245

4 249

5 253

6 257

7 261

8 265

Elements

Address

Locate

14K. Adisesha

Page 15: Data structures using c

ExerciseAn automobile company uses array AUTO

to record the number of automobile sold each year from 1932 to 1996. Locate AUTO[1980]. Assume 801 as starting address with 5 words long. Also find the length of the array AUTO.

ANSWER:

Loc[1980] = 1041NE = 65

15K. Adisesha

Page 16: Data structures using c

ExerciseGiven a 4x5 array with [-3..0, 2..6) index,

starting address is 81 with 2 words per memory cell. Locate [-1,5] using row major and column major representation.

To get the location of elements (ROW MAJOR)Loc [j,k] = base + w [ N (j-LB1) + (k-LB2) ]To get the location of elements (COLUMN MAJOR)Loc [j,k] = base + w [ M (k-LB2) + (j-LB1) ]

To get the number of elements in an arrayM = UB1 – LB1 +

1N = UB2 – LB2 + 1

NE = M x N

16K. Adisesha

Page 17: Data structures using c

Memory MapCOLUMN

Locate

BACK

2 3 4 5 6

-3 81 83 85 87 89

-2 91 93 95 97 99

-1 101 103 105 107 109

0 111 113 115 117 119

ROW

ROW MAJOR

17K. Adisesha

Page 18: Data structures using c

Memory MapCOLUMN

Locate

BACK

2 3 4 5 6

-3 81 89 97 105 113

-2 83 91 99 107 115

-1 85 93 101 109 117

0 87 95 103 111 119

ROW

COLUMN MAJOR

18K. Adisesha

Page 19: Data structures using c

ExerciseWhen storing a two-dimensional array “a” with

ten rows and ten columns in continuous memory space in the direction of rows, what is the address where a [5,6] is stored? In this question, the address is represented in decimal numbers.

a [1,1]

a [1,2]

Address

100101102103

a. 145 c. 190b. 185 e. 212d. 208

19K. Adisesha

Page 20: Data structures using c

Problem-Oriented Data StructureList Structure A linear collection of data elements called nodes and where linear order is given by means of pointers.

DATA POINTER FIELD FIELD

NODE

AddressData

20K. Adisesha

Page 21: Data structures using c

Problem-Oriented Data StructureTypes of List Structure

Uni-directional List

RAMOS 07H

03H

AQUINO 03H

05H

MARCOS 05H

01H

HEAD

ESTRADA NULL

07H

TAIL

21K. Adisesha

Page 22: Data structures using c

Problem-Oriented Data StructureTypes of List Structure

Bi-directional List

NULL MARCOS 05H

01H

HEAD

01H AQUINO 03H

05H

05H RAMOS 07H

03H

03H ESTRADA NULL

07H

TAIL22K. Adisesha

Page 23: Data structures using c

Problem-Oriented Data StructureSTACK An ordered list where all operations are restricted at one end of the list known as TOP.List processing is based on Last-In First-Out (LIFO).

Bottom

Top

23K. Adisesha

Page 24: Data structures using c

Problem-Oriented Data StructureSTACK OPERATION

PUSH - inserts a new element at the top of the stack

POP - retrieves the element at the top and deletes it from stack.

TOP - retrieves the element at the top of the stack

24K. Adisesha

Page 25: Data structures using c

ExerciseWhat will be the content of the stack

after performing the following operation?

1.Pop (S)2.Push (E,S)3.Push (F,S)4.Pop (S)5.Pop (S)6.Push (G)

DCBA

25K. Adisesha

Page 26: Data structures using c

Problem-Oriented Data StructureSTACK APPLICATION

INFIX - an expression where operator is placed in between the operandsExample : (A + B)

PREFIX - an expression where operator is placed before the operandsExample : (+AB)

POSTFIX - an expression where operator is placed after the operandsExample : (AB+)

26K. Adisesha

Page 27: Data structures using c

Problem-Oriented Data StructureTREE Structure

It is a collection of data items called nodes. Each node has a relationship with one or more nodes thereby giving a hierarchical structure.

A

CB D

FE G IH J

LK M

Binary Tree

/

+*

CB DA

27K. Adisesha

Page 28: Data structures using c

ExerciseCreate a Tree Structure based on the

given prefix and postfix notation.

1.) - + / *A B G M * ^ N 3 - + G H ^ I 2

2.) F M 3 ^ * S / K * M 3 ^ L * - Q P 2 ^ * +

28K. Adisesha

Page 29: Data structures using c

1. Write a C program to search for an element in an array using Binary search

2. Write a C program to sort a list of N elements using Bubble sort Technique

3. Write a C program to demonstrate the working of stack of size N using an array. The elements of the stack may assume to be of type integer or real, the operations to be supported are 1. PUSH 2. POP 3. DISPLAY. The program should print appropriate messages for STACK overflow, Under flow and empty, use separate functions to detect these cases

4. Write a C program to simulate the working of an ordinary Queue using an array. Provide the operations QINSERT, QDELETE and QDISPLAY. Check the Queue status for empty and full.

5. Write a C program to simulate the working of an Circular Queue using an array. Provide the operations CQINSERT, CQDELETE and CQDISPLAY. Check the Circular Queue status for empty and full.

K. Adisesha 29

Page 30: Data structures using c

6. Using dynamic variables and pointers Write a C program to construct a singly linked list consisting of the following information in each node; Roll - No (Integer), Name (Character string) The operations to be supported are ; 1. LINSERT Inserting a node in the front of the list 2. LDELETE Deleting the node based on Roll - No 3. LSEARCH Searching a node based on Roll-No 4. LDISPLAY Displaying all the nodes in the list

7. Write a C program to sort a list of N elements using Merge sort Algorithm

8. Using Dynamic variables and pointers construct Binary search tree of integers , Write C functions to do the following ; 1. Given a KEY, Perform a search in Binary search tree . If it is found display Key found else insert the key in the Binary search tree. 2. While constructing the Binary search tree do not add any duplicate 3. Display the tree using any of the traversal method

K. Adisesha 30

Page 31: Data structures using c

9. Write a C program to sort a list of N elements of integer type using heap sort Algorithm

10. Write a C program to simulate the working of Towers of Hanoi problem for N disks , print the total number of Moves taken by the program.

11. Write a C program to sort a list of N elements of integer type using quick sort Algorithm

12. Write a C program to find ncr using recursion

K. Adisesha 31

Page 32: Data structures using c

Thank you

K. Adisesha 32