0% found this document useful (0 votes)
7 views91 pages

Introduction to Data Structures

The document provides an introduction to data structures, covering classifications such as primitive and non-primitive types, operations, and dynamic memory allocation. It discusses arrays, structures, stacks, algorithms, and recursion, along with their implementations and uses in programming. Additionally, it explores polynomials, sparse matrices, and strings, including their representations and operations in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views91 pages

Introduction to Data Structures

The document provides an introduction to data structures, covering classifications such as primitive and non-primitive types, operations, and dynamic memory allocation. It discusses arrays, structures, stacks, algorithms, and recursion, along with their implementations and uses in programming. Additionally, it explores polynomials, sparse matrices, and strings, including their representations and operations in C programming.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 91

INTRODUCTION TO DATA STRUCTURES: Data Structures, Classifications

(Primitive
& Non-Primitive), Data structure Operations
Review of pointers and dynamic Memory Allocation,
ARRAYS and STRUCTURES: Arrays, Dynamic Allocated Arrays, Structures and
Unions,
Polynomials, Sparse Matrices, representation of Multidimensional Arrays, Strings
STACKS: Stacks, Stacks Using Dynamic Arrays, Evaluation and conversion of
Expressions
Text Book: Chapter-1:1.2 Chapter-2: 2.1 to 2.7 Chapter-3: 3.1,3.2,3.6
Reference Book 1: 1.1 to 1.4
ALGORITHM SPECIFICATION

• Definition: An algorithm is a finite set of instructions that, if followed,


accomplishes a particular task.
• All algorithms must satisfy the following criteria:

1. Input
2. Output
3. Definiteness
4. Finiteness
5. Effectiveness
Binary search

• searchnum < list[middle]. In this case, if searchnum is present, it


must be in the positions between 0 and middle - 1. Therefore, we set
right to middle - 1.
• searchnum = list[middle]. In this case, we return middle.
• searchnum > list[middle]. In this case, if searchnum is present, it
must be in the positions between middle + 1 and n - 1. So, we set left
to middle + 1.
Swap function
Recursive Algorithms
• A function calls itself either directly or indirectly during execution.
• Recursive-algorithms when compared to iterative-algorithms are
normally compact and easy to understand.
• Types of recursion:
• 1) Direct recursion: where a recursive-function invokes itself.
• 2) Indirect recursion: A function which contains a call to another
function which in turn calls another function and so on and eventually
calls the first function.
Direct recursion
Indirect recursion
ARRAY

• An Array is defined as, an ordered set of similar data items. All the data items of an array are stored
in consecutive memory locations.
• The data items of an array are of same type and each data items can be accessed using the same
name but different index value.
• Array is used to store, process and print large amount of data using a single variable.
• Set of integers, set of characters, set of students etc. are examples of various arrays. Marks of all
students in a class is an array of marks .
• The pictorial representation of an array of 5 integers, an array of 5 floating point numbers and an
array of 5 characters is shown below:
• An array is a set of pairs, such that each index has a value associated
with it. It can be called as corresponding or a mapping
• Ex: <index, value>
< 0 , 25 > list[0]=25
< 1 , 15 > list[1]=15
< 2 , 20 > list[2]=20
< 3 , 17 > list[3]=17
< 4 , 35 > list[4]=35
THE ARRAY AS AN ABSTRACT
DATA TYPE
one-dimensional array or single dimensional array
• A single-dimensional array (also called one-dimensional array) is a linear
list consisting of related data items of same type. In memory, all the data
items are stored in contiguous memory locations one after the other.
• A one-dimensional array in C is declared implicitly by appending brackets
to the name of a variable.
• For example, a single dimensional array consisting of 5 integer elements
• declare and define a single dimensional array

• The declaration and definition infoms the compiler about the


• Implementation of one-dimensional arrays
int list[5];
o Allocates five consecutive memory locations.
o Each memory location is large enough to hold a single integer.
o The address of the first element list[0], is called the base address.
o If the size of an integer on your machine is denoted by sizeof(int), then
we get the following memory addresses for the five elements of list[]
One-dimensional array addressing
• Assume that we have the following declaration

• the address of the ith element is ptr + i. *(ptr + i) indicates that we want
the contents of the ptr+i position.
DYNAMICALLY ALLOCATED ARRAYS ONE-
DIMENSIONAL ARRAYS
• When writing programs, sometimes we cannot reliably determine how large an
array must be.
• A good solution to this problem is to
→ defer this decision to run-time &
→ allocate the array when we have a good estimate of required array-size
• Dynamic memory allocation can be performed as follows:

The above code would allocate an array of exactly the required size and hence
would not result in any wastage.
Multidimensional arrays

• Arrays with two or more dimensions are called multi-dimensional arrays.


int b[10][10];
int c[3][4][5];
• A two-dimensional array is used when data items are arranged in row-wise
and column wise in a tabular fashion.
• To identify a particular item we have to specify two indices (also called
subscripts): the first index identifies the row number and second index
identify the column number of the item.
Declaring and initializing two
dimensional arrays
• int a[3][4];
• This declaration informs the compiler to reserve 12 locations (3 * 4 =
12) contiguously one after the other.
Initialization
• int a[4][3] = {
{11, 22, 33},
{44, 55, 66},
{77, 88, 99},
{10, 11, 12}
};
Two dimensional array - Array of arrays.
• A 2-dimensional array is represented as a 1-dimensional array in which each
element has a pointer to a 1-dimensional array.
• int x[5][7]; //we create a 1-dimensional array x whose length is 5;
//each element of x is a 1-dimensional array whose
length is 7.
• Address of x[i][j] = x[i]+j*sizeof(int)
Pointers can be dangerous
• We may attempt to access an area of memory that is out of range of our
program
int *p;
int a = 10;
p = &a;
printf(“%d”, *p);
printf(“%d”, *(p-1)); // accessing memory which is out of range
• The pointer may not contain address of legitimate variable (memory location).

• We may de-reference a NULL pointer. When a NULL pointer is de-referenced,


some computers may return 0 and some computers may return data in
location zero producing serious error
Dynamically create a two-
dimensional array
STRUCTURES AND UNIONS

• A structure is defined as a collection of data of same/different data types.


• All data items thus grouped are logically related and can be accessed using
variables.
• Thus, structure can also be defined as a group of variables of same or
different data types.
• The variables that are used to store the data are called members of the
structure or fields of the structure.
• In C, the structure is identified by the keyword struct.
• In the above structure, name, roll_number and average_marks are the fields
of the structure.
• They are also called members of the structure.
• Even though the size of structure is 22 bytes, no space is reserved for the
above structure.
• Memory is reserved only if the above definition is associated with variable.
• That is, once the structures are defined, they have to be declared. Then only,
22 bytes of memory space is reserved.
• A structure can be declared using three different ways

• Tagged Structure
The structure definition with tag name is called tagged structure
• Structure variables
• Type-Defined Structure
• Method 1: The structure definition associated with keyword typedef is
called type-defined structure .
Method 2:
Structure initialization
• Method 1:

• Method 2:
Example

• Valid Initialization

• Invalid Initialization

• Valid Initialization
struct employee a = {"RAMA", 10950, 2001};
• Partial Initialization – remaining initialized with default value (0)
struct employee a = {"RAMA"};

• Too many initializers – the number of initializers should not exceed the
number of members. It leads to syntax error
struct employee a = {“Rama”,10950, 2001,10.2};

• Invalid Initialization - initializing members in the middle of a structure


without initializing the previous members is invalid
struct employee a = {10950, 2001};
Accessing structures
• We use dot operator (.) to select a particular member of the structure.
• The members of a structure can be accessed by specifying the variable
followed by dot operator followed by the name of the member.
• a.name
• a.salary
• a.id
Structure within a structure
Unions
• A union declaration is similar to a structure, but the fields of a union
must share their memory space.
• This means that only one field of the union is "active" at any given
time.
• The compiler will reserve the memory whose size is that of the largest
member.
• Since double is the largest data type of the member of the union,
sizeof(double) = 8 bytes of memory is reserved for the variable x.
• All the members have the same starting address and hence they share
the same allocated space.
Internal implementation of structures
Self-Referential Structures
• Covered during linked list
POLYNOMIAL
• A polynomial is a mathematical expression consisting of sum of terms where
each term is made up of a coefficient multiplied by a variables raised to a
power.

• Each term has a form axe , where x=variable, a=coefficient and e=exponent.
• The largest(or leading) exponent of a polynomial is called its degree.
• Assume that we have 2 polynomials,
POLYNOMIAL REPRESENTATION
• Consider the two polynomials

• These polynomials are stored in the array terms.


• The index of the first term of A and B is given by starta and startb,
respectively, while finisha and finishb give the index of the last term of A
and B.
• The index of the next free location in the array is given by avail. For our
example, starta = 0, finisha = 1, startb = 2, finishb = 5, and avail = 6.

2x1000 + x4 + 10x3 + 3x2 + 2


THE SPARSE MATRIX
• A sparse matrix is a matrix that has very few non-zero elements spread out
thinly.
• A matrix which has more number of zero elements or high proportion of
zeros elements is called sparse matrix.
• The sparse matrix can be single dimensional or multidimensional such as 2-
dimensional, 3-dimensional and so on.
Disadvantage of a sparse matrix
• The sparse matrix contains many zeroes.
• If we are manipulating only non-zero values, then we are wasting the
memory space by storing unnecessary zero values.
• This disadvantage can be overcome by storing only non-zero values
thus saving the space.
Transposing a Matrix
• To transpose a matrix we must interchange the rows and columns.
• This means that each element a[i][j] in the original matrix becomes
element b[i][j] in transpose matrix.
Matrix Multiplication
Strings
• A string is a sequence of characters that are obtained by concatenating
the various characters in a character set.
• Each programming language contains a set of characters:
Letters: A B C D E F G H I J K L M N O P Q R S T U V W X Y Z
Digits: 0 1 2 3 4 5 6 7 8 9
Special characters: +* / ( ) [ ] { } # $ and so on.
• A sequence of characters enclosed within double quotes is called a string.
• In C, string is implemented as an array of characters terminated by a NULL
character.
Pattern Matching
• Assume that we have two strings, string and pat, where pat is a pattern to be searched
for in string. The easiest way to determine if pat is in string is to use the built-in function
strstr.

• Although strstr seems ideally suited to pattern matching, there are two reasons why we
may want to develop our own pattern matching function:
(1)The function strstr is new to ANSI C. Therefore, it may not be available with the compiler
we are using.
(2)There are several different methods for implementing a pattern matching function. The
easiest but least efficient method sequentially examines each character of the string until it
finds the pattern or it reaches the end of the string. If pat is not in string, this method has a
computing time of O(n.m) where n is the length of pat and w is the length of string.
Knuth, Morris, Pratt Pattern
Matching algorithm
Knuth, Moris and Pratt method(KMP Algorithm)
• So, the failure function for the corresponding pattern string can be
written as shown below:

You might also like