39
| Jul 2012| © 2012 UPES UPES DATA STRUCTURE USING ‘C’

unit-I

Embed Size (px)

DESCRIPTION

data stuructures lesson 1

Citation preview

| Jul 2012| © 2012 UPES

UPES

DATA STRUCTURE USING ‘C’

© 2012 UPESJul 2012Jul 2012

TEXT BOOK [T]: Fundamental of Data Structures in C 2nd Ed., Ellis Horowitz, Sartaj Sahni,

Universities Press. Data Structures and Algorithms: Concepts, Techniques and Applications, G.

A. V. PAI, TMH Data Structure using C, G.S. Baluja, Galgotia Publications. Schaum's Outline of Data Structures with C, John Hubbard, TMH Data Structures Using C, Sudipta Mukherjee, TMH

REFERRENCE BOOKS: Data Structures Using C and C++, Yedidyah Langsam, Moshe J. Augenstein,

Aaron M. Tenenbaum, Pearson An Introduction To Data Structures With Application, Jean-Paul Tremblay,

Paul Sorenson

© 2012 UPESJul 2012Jul 2012

Unit-I : INTRODUCTION & SIGNIFICANCE Basic Terminology ,Elementary Data Organization Data are simply values or set of values. Data item refers to a single unit of values. Data items that are divided

into sub items are called group items. Entity is something that has certain attributes or properties which

may be assigned values. Example:

Information meaning full or processed data The way that data are organized into the hierarchy of fields, records

and files.

© 2012 UPESJul 2012Jul 2012

Field is a single elementary unit of information representing an atribute of entity.

Record is a collection of field values of a given entity File is a collection of records of the entities in a given set

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

DATA STRUCTURES

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

Classification of Data Structures

© 2012 UPESJul 2012Jul 2012

A data structure is said to be linear if its elements form a sequence or linear list

The data is arranged in a linear fashion although the way they are stored in memory need not be sequential.

Examples : arrays , linked list , stacks and queues

Non-linear: if the data is not arranged in sequence. The insertion and deletion of data is therefore not possible in a linear

fashion. Example: Trees , graphs

© 2012 UPESJul 2012Jul 2012

Arrays

List of a finite number n of similar data elements referenced respectively by a set of n consecutive numbers.

© 2012 UPESJul 2012Jul 2012

TERMINOLOGY Size : Number of elements in an array is called the size of the array Type : kind of data type it is meant for Base : the address of memory location where the first element in the

array is located

Index: All the elements in an array can be referenced by a subscript like Ai or A[i],

Range of index : Indices of array elements may change from a lower bound (L) to an upper bound (U),

Word :the size of an element

© 2012 UPESJul 2012Jul 2012

Operations on Arrays Various operations that can be performed on an array are:

traversing sorting searchinginsertion deletionmerging

© 2012 UPESJul 2012Jul 2012

Insertion This operation is used to insert an element into an array provided that

the array is not full

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Deletion

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Merge

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

MULTIDIMENSIONAL ARRAYS Matrix (2- dimensional array), 3-dimensional array are two examples

of multidimensional arrays Two-dimensional Arrays

Memory representation of a matrix 1. Row-major order 2. Column-major order

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Reference of elements in a matrix Row-major orderAssume that the base address is the first location of the memory, that is, 1. So, the address of aij will be obtained as Address (aij) = Storing all the elements in first (i – 1)-th rows+ The number of elements in i-th row up to j-th column. = (i – 1) × n + j So, for the matrix A3 × 4, the location of a32 will be calculated as 10 Column-major order Address (aij) = Storing all the elements in first (j – 1)-th columns + The number of elements in j-th column up to i-th rows. = (j – 1) × m + i

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Data Structure Operations

Four operations play a major role

Special situation Operations are:

© 2012 UPESJul 2012Jul 2012

Abstract data type

When an application requires a special kind of data which is not available as built-in data type then it is the programmer’s burden to implement his own kind of data. Here, the programmer has to give more effort regarding how to store value for that data, what are the operations that meaningfully manipulate variables of that kind of data, amount of memory require to store for a variable. The programmer has to decide all these things and accordingly implement it. Programmers’ own data type is termed as abstract data type.

Abstract data type is also alternatively termed as user-defined data type.

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

ADT Model

© 2012 UPESJul 2012Jul 2012

Algorithms : Complexity ,Time-Space Tradeoff

An algorithm is a well-defined list of steps for solving a particular problem.

Complexity of an algorithm is a measure of the amount of time and/or space required by an algorithm for an input of a given size (n).

In computational complexity theory, asymptotic computational complexity is the usage of the asymptotic analysis for the estimation of computational complexity of algorithms and computational problems, commonly associated with the usage of the big O notation.

© 2012 UPESJul 2012Jul 2012

O,Ω and, Θ –notation

Let f(n) and g(n) be functions mapping form integers to real numbers. A function f (n) = O(g(n)) (read as, expressed in big oh of g(n)) iff f (n) ≤ c.g( n ) for some n>=n0 and c, where n0 and c are some constants > 0.

Let f ( n ) and g( n ) be two functions mapping integers to real numbers. We say that f (n) =Ω (g(n)) (read as, is big-omega of g(n)) iff for a real constant c > 0 and no > 0 f ( n ) ≥ c.g( n ) for all n>=n0

Let and be two functions mapping integers to real numbers. We say that f (n) =Θ (g(n)) iff there exist two real constants c1 ,c2 and no > 0 such that c1.g( n ) ≤ f ( n ) ≤ c2 .g( n ) for all n ≥ no

© 2012 UPESJul 2012Jul 2012

Pointers A pointer is a variable that contains the address of

a variable. if c is a char and p is a pointer that points to it, we

could represent the

p = &c;

© 2012 UPESJul 2012Jul 2012

The unary operator * is the indirection or dereferencing operator

int x = 1, y = 2, z[10]; int *ip; /* ip is a pointer to int */ ip = &x; /* ip now points to x */ y = *ip; /* y is now 1 */ *ip = 0; /* x is now 0 */ ip = &z[0]; /* ip now points to z[0] */

If ip points to the integer x, then *ip can occur in any context where x could, so *ip = *ip + 10; increments *ip by 10.

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

#include <stdio.h>

int main ( )

{ int a=3, b=5, S=0, D=0, M=0;

int *p1, *p2, *p3, *p4, *p5; // five pointers are declared

// assigning address of a, b, S, D and M to these pointers

p1 = &a; p2= &b; p3 = &S; p4 = &D; p5=&M;

*p3 = *p1 + *p2; // same as s = a + b;

printf(“%d”,*p3); // it prints 8

printf (“%d”,p1); // it prints the address of a

D = *p1 - b; // it calculates -2

printf (“%d”,*p4); // it prints -2

*p5 = a * *p2; // it calculates 15

printf (“%d”, M); // it prints 15 return 0; }

© 2012 UPESJul 2012Jul 2012

Pointers and Function Arguments . swap(a, b); where the swap function is defined as void swap(int x, int y) /* WRONG */ { int temp; temp = x; x = y; y = temp; } Because of call by value, swap can't affect the arguments a and b in

the routine that called it

© 2012 UPESJul 2012Jul 2012

swap(&a, &b); void swap(int *px, int *py) /* interchange *px and *py */ { int temp; temp = *px; *px = *py; *py = temp; }

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

© 2012 UPESJul 2012Jul 2012

Pointer Arrays Use to store address of some memory variables or the address of

other arrays. Address of memory variable or array is known as pointer and array

containing pointers as its elements is known as pointer array.

© 2012 UPESJul 2012Jul 2012

Pointers and Arrays#include <stdio.h>

int main( )

{ int a[12]= { 5, 2 , 6, 9, 12, 7, 56, 34, 11, 76, 37, 55,69};

int i, *p;

// printing array elements using index / subscript

for ( i = 0 ; i < 12 ; i++) printf(%d,a[i]);

// Following will store the address of a[0] into p (pointer)

p = a; // same as p = a[0];

for ( i = 0 ; i < 12 ; i++)

{ printf (“%d”,*p); // prints the contents of the address

p++; // it shift the pointer to next element of the array

} return 0; }

© 2012 UPESJul 2012Jul 2012

Pointers and Structures#include <stdio.h>

struct rectangle {

float length;

float width;

float area; };

int main( ) { struct rectangle Rect;

Rect . length = 12.5 ; Rect . width = 6.78;

Rect . area = Rect . length * Rect . width;

struct rectangle *P;

P = &Rect;

Printf(” Length= %f“, P ->length); Printf(” \n Width= %f“, P -> width;

Printf(”Area= %f“<< P -> area); return 0; }

© 2012 UPESJul 2012Jul 2012

struct Node {

int value;

struct Node* next; };

// Allocate the pointers

struct Node *x; struct Node *y; struct Node *z;

// Allocate the pointees In Simple c-language

x = new (Node); // (struct Node*) malloc ( sizeof(Node));

y = new (Node); // (struct Node*) malloc( sizeof(Node));

z = new (Node); // (struct Node*) malloc (sizeof(Node));

// Put the numbers in the pointees

x->value = 34; y->value = 44; z->value = 65;

// Put the pointers in the pointees

x->next = y; y->next = z; z->next = x; }

© 2012 UPESJul 2012Jul 2012