Introduction to Data Structures
Introduction to Data Structures
(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
1. Input
2. Output
3. Definiteness
4. Finiteness
5. Effectiveness
Binary search
• 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 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
• 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};
• 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
• 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: