Module 01 - Introduction, Arrays, Structures, Stacks
Module 01 - Introduction, Arrays, Structures, Stacks
MODULE-1
Introduction
✓ Data is a value or a set of values.
Example 10, Anjali, Bengaluru
✓ A data item refers to a single unit of values. Data items that are divided into sub items are
called group items.
Example :Name of an employee can be divided to three subitems- first name, middle
name and last name
✓ Data items that are not divided into sub items are called elementary items.
Data Structures
Data Structures: A data structure is a particular method of storing and organizing data in a
computer so that it can be used efficiently. The data Structure is classified into
a. Primitive data structure: These can be manipulated directly by the machine instructions.
Example: integer, character, float, double, boolean etc
b. Non primitive data structures: They cannot be manipulated directly by the machine
instructions. The non primitive data structures are further classified into linear and non linear
data structures.
• Linear data structures: Data structure in which data elements are arranged
sequentially or linearly, where each element is attached to its previous and next
adjacent elements is called a linear data structure. Example are arrays, stacks, queues
, list etc.
➢ Static Data Structure: Static data structure has a fixed memory size. It is easier to
access the elements in a static data structure. Example: array.
➢ Dynamic Data Structure: In dynamic data structure, the size is not fixed. It can be
randomly updated during the runtime which may be considered efficient concerning
the memory (space) complexity of the code. Example: Queue, Stack, etc.
• Non linear data structure: They do not show the relationship of adjacency between
the elements. Example are Trees and graphs
Arrays
• Array is a collection of elements of the same data type
• An array is declared by appending brackets to the name of a variable.
• Elements of the array are stored at contiguous memory locations where the first element is stored at
the smallest memory location.
For example
1. Single Dimensional Arrays: This is the most basic type of array where elements are stored in a
single row or sequence. It is essentially a list of elements of the same type.
Syntax:
int arr[5];
//Program
#include <stdio.h>
int main() {
int arr[5]; // Declare an array of size 5
int i;
// Get user input to initialize the array
printf("Enter 5 integers:\n");
for(i = 0; i < 5; i++) {
printf("Enter element %d: ", i + 1);
scanf("%d", &arr[i]); // Store user input in the array
}
// Print the array elements
printf("Array elements: ");
for(i = 0; i < 5; i++) {
printf("%d ", arr[i]);
}
return 0;
}
2. Two-Dimensional Array : A 2D array is like a table (matrix) with rows and columns. It stores data
in two dimensions.
Syntax:
int arr[3][4]; // 3 rows, 4 columns
//Program
#include <stdio.h>
int main()
{
// declaring and initializing 2d array
int arr[2][3] = { 10, 20, 30, 40, 50, 60 };
printf("2D Array:\n");
// printing 2d array
for (int i = 0; i < 2; i++) {
for (int j = 0; j < 3; j++) {
printf("%d ",arr[i][j]);
}
printf("\n");
}
return 0;
}
Structures
Structures: Structure is basically a user-defined data type that can store related information that may
be of same or different data types together.
The major difference between a structure and an array is that an array can store only information of same
data type. A structure is therefore a collection of variables under a single name. The variables within a
structure are of different data types and each has a name that is used to select it from the structure.
5. Pointer to Structure
6. Self-Referential Structure
For example,
Struct student {
char name[10];
int age;
float average_marks;
} st;
To assign values to these fields dot operator (. ) is used as the structure member operator. We use
this operator to select a particular member of the structure.
#include <stdio.h>
int main() {
// Declare a variable of type Student
struct Student student1;
return 0;
}
• Structure Declaration with Initialization : We can also assign values to members of a structure variable at
declaration time, in a single line. Just insert the values in a comma-separated list inside curly brace {}. Note that
you don't have to use the strcpy() function for string values with this technique:
// Create a structure
struct Student {
char name[50]; // Character array for storing the student's name
int age; // Integer for the student's age
float grade; // Float for the student's grade
};
int main() {
// Create a structure variable and assign values to it
struct Student s1 = {‘Abhishek’, 16, 90.23};
// Print values
printf("%d %c %s", s1.name, s1.age, s1.grade);
return 0;
}
Output:
Abhishek 16 90.23
We can create our own structure data types by using the typedef statement. Consider an example
that creates a structure for the employee details.
Ex:
#include <stdio.h>
// Using typedef to define an alias for 'struct Student'
typedef struct {
int id;
char name[50];
float marks;
} Student
int main() {
// Now you can declare the structure variable without 'struct'
Student student1 = {1, "Alice", 88.0};
// Print values
printf("ID: %d, Name: %s, Marks: %.2f\n", student1.id, student1.name, student1.marks);
return 0;
}
Output:
ID: 1, Name: Alice, Marks: 88.00
• Nested Structure: A structure can be embedded within another structure. That is a structure can have
another structure as its member such a structure is called a nested structure.
For example, associated with our employee structure we may wish to include the date of Birth of an
employee by using nested stucture
typedef struct {
int month;
int day;
int year;
} date;
typedef struct {
char name[10];
int age;
float salary;
date dob;
}employee;
• Array of Structures: In the case of a student or the employee we may not store the details ofonly 1
student or 1 employee. When we have to store the details of a group of students we can declare an
array of structures.
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
// Declare an array of structures
struct Student students[3] = {
{1, "Tom", 85.5},
{2, "Jerry", 91.0},
{3, "Spike", 76.8}
};
// Traverse the array and print details
for (int i = 0; i < 3; i++) {
printf("ID: %d, Name: %s, Marks: %.2f\n", students[i].id, students[i].name, students[i].marks);
}
return 0;
}
Output:
• Self-Referential Structures: A self-referential structure is one in which one or more of its data member
is a pointer to itself. They require dynamic memory allocation (malloc and free) to explicitlyobtain and
release memory.
Example:
typedef struct list {
int data;
list *link ;
};
✓ Each instance of the structure list will have two components, data and link. data is a single
character, while link is a pointer to a list structure.
✓ The value of link is either the address in memory of an instance of list or the null pointer.
Consider these statements, which create three structures and assign values to their respective fields:
list item1, item2, item3;
item1.data = 5
item2.data = 10
item3.data = 15
item1.link = item2.link = item3.link = NULL;
We can attach these structures together by replacing the null link field in item 2 with one that points
to item 3 and by replacing the null link field in item 1 with one that points to item 2.
item1.link = &item2; item2.1ink = &item3;
#include <stdio.h>
// Define a node structure for a linked list
struct Node {
int data;
struct Node *next; // Pointer to the next node
};
int main() {
// Creating two nodes
struct Node node1, node2;
node1.data = 10;
node1.next = &node2;
node2.data = 20;
node2.next = NULL;
// Print the data
printf("Node 1 data: %d\n", node1.data);
printf("Node 2 data: %d\n", node1.next->data); // Accessing node 2 through node 1
return 0;
}
Output:
Node 1 data: 10
Node 2 data: 20
Comparing structures: Return TRUE if employee 1 and employee 2 are the same otherwise return
FALSE
Unions
Unions: A union is a user-defined data type that can store related information that may be of different
data types or same data type, 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.
Example1: Suppose a program uses either a number that is int or float we can define a union as
Union num
{
int a;
float b;
};
Union num n1;
Now we can store values as n1.a=5 or n2.b= 3.14 only one member is active at a point of time.
Pointer
• Initialization of pointer variables: Uninitialized variables have unknown garbage values stored in
them, similarly uninitialized pointer variables will have uninitialized memory address stored inside
them which may be interpreted as a memory location, and may lead to runtime error.
These errors are difficult to debug and correct, therefore a pointer should always be initialized with a
valid memory address.
//Here the variable a and the pointer variable p are of the same data type. To make p to point at a we
have to write a statement
p=&a; // now the address of a is stored in the pointer variable p and now p is said to be
pointing at a.
If we do not want the pointer variable to point at anything we can initialize it to point at NULL
NOTE: A pointer variable can only point at a variable of the same type.
We can have more than one pointer variable pointing at the same variable. For example
int a;
int *p,*q;
p=&a;
q=&a;
now both the pointer variable p and q are pointing at the same variable a. There is no limit to the
number of pointer variable that can point to a variable.
Note:
➢ we need parenthesis for expressions like (*p) ++ as the precedence of postfix increment is more
than precedence of the indirection operator (*). If the parenthesis is not used the address will
be incremented.
➢ The indirection and the address operators are the inverse of each other when combined in an
expression such as *&a they cancel each other
Memory usage: Four memory management functions are used with dynamic memory. malloc, calloc
and realloc are used for memory allocation. The function free is used to return memory when it is not
used.
Heap: It is the unused memory allocated to the program When requests are made by memory
allocating functions, memory is allocated from the heap at run time.
Dynamic memory allocation in C is managed using four key functions from the <stdlib.h> library:
1. malloc(): Allocates a specified number of bytes and returns a pointer to the first byte of the allocated
memory.
2. calloc(): Allocates memory for an array of elements, initializes all bytes to zero, and returns a pointer
to the allocated memory.
3. realloc(): Changes the size of previously allocated memory and returns a pointer to the new block of
memory.
4. free(): Frees the memory allocated dynamically, releasing it back to the system to avoid memory
leaks.
2) Releasing memory (free): When memory locations allocated are no longer needed, they should be
freed by using the predefined function free.
Syntax: free(void*);
Example: int *p,a;
Ms. Gowthami J, Department of ISE(DS), ACET Page 14
Data Structures and Applications/BCS304 Module-1
p=&a;
free(p);
Example: To allocate a one dimensional array of integers whose capacity is n the following code can
be written.
int *ptr
ptr=(int*)calloc(n,sizeof(int))
4) Reallocation of memory(realloc): The function realloc resizes the memory previously allocated by
either malloc or calloc.
Example
int *p;
p=(int*)calloc(n,sizeof(int))
p=realloc(p,s) /*where s is the new size*/
The statement realloc(p,s) -- Changes the size of the memory pointed by p to s. The existing contents
of the block remain unchanged.
➢ When s> oldsize(Block size increases) the additional (s – oldsize )have unspecified value
➢ When s<oddsize (Block size reduces) the rightmost (oldsize-s) bytes of the old block are freed.
➢ When realloc is able to do the resizing it returns a pointer to the start of the new block
➢ When is not able to do the resizing the old block is unchanged and the function returns the
value NULL.
Dangling Reference: Once a pointer is freed using the free function then there is no way to retrieve
this storage and any reference to this location is called dangling reference.
Example2:
int i,*p,*f;
i=2;
p=&i;
f=p;
free(p);
*f=*f+2 /* Invalid dangling reference*/
The location that holds the value 2 is freed but still there exist a reference to this location through f
and pointer f will try to access a location that is freed so the pointer f is a dangling reference
Pointers can be dangerous: When pointers are used the following points needs to be taken care
1. When a pointer is not pointing at any object it is a good practise to set it to NULL so that there
is no attempt made to access a memory location that is out of range of our program or that does
not contain a pointer reference to the legitimate object.
2. Use explicit type casts when converting between pointer types.
int *pi;
float *pf;
Pi= (int*) malloc (sizeof((int));
Pf= (float *)pi;
3. Define explicit return types for functions. If the return type is omitted it defaults to integer
which has the same size as a pointer and can be later interpreted as a pointer.
Arrays
Array is a list of finite number (n) of homogenous data elements.
a. The elements of the array are referenced by an index set consisting of n consecutive
numbers(0. ..(n-1)).
b. The elements of the array are stored in successive memory locations
c. The number n of elements is called the length or size of the array. Length of the array can be
obtained from the index set using the formula
Length = Upper bound – Lowe bound +1
d. The elements of an array may be denoted by a[0],a[2] ............a[n-1]. The number k in a[k] is
called a subscript or index and a[k] is called the subscripted value.
e. An array is usually implemented as a consecutive set of memory locations
Declaration: Linear arrays are declared by adding a bracket to the name of a variable. The size of
the array is mentioned within the brackets.
In C all arrays start at index 0. Therefore, list[0], list[1], list[2], list[3], and list[4] are the names of
the five array elements ,each of which contains an integer value.
Two dimensional arrays: C uses the array of array representation to represent a multidimensional
array. In this representation a 2 dimensional array is represented as a one dimensional array in which
each element is itself a one dimensional array.
• A two dimensional m X n array A is a collection of m* n data elements such that each element
is specified by a pair of integers called subscripts.
• An element with subscript i and j will be represented as A[i][j]
• Declaration: int A[3][5];
// It declares an array A that contains three elements where each element is a one
dimensional array. Each one dimensional array has 5 integer elements.
0 1 2 3
0 A[0][0] A[0][1] A[0][2] A[0][3]
Rows 1 A[1][0] A[1][1] A[1][2] A[1][3]
2 A[2][0] A[2][1] A[2][2] A[2][3]
Example: Representation of the two dimensional array A[3][4] in row major order and column major
order
A Subscript A Subscript
A[0][0] A[0][0] Column1
A[0][1] A[1][0]
Row1
A[0][2] A[2][0]
A[0][3] A[0][1] Column2
A[1][0] A[1][1]
A[1][1] A[2][1]
Row2
A[1][2] A[0][2] Column3
A[1][3] A[1][2]
A[2][0] A[2][2]
A[2][1] A[0][3] Column4
Row3
A[2][2] A[1][3]
A[2][3] A[2][3]
In C we find the element x[i][j] by first accessing the pointer in x[i]. This pointer gives the address
of the zeroth element of row i of the array. Then by adding j*sizeof(int) to this pointer, the address
of the jth element of the ith row is determined
Example to find x[1][3] we first access the pointer in x[1] this pointer gives the address of x[1][0]
now by adding 3*sizeof (int) the address of the element x[1][3] is determined.
#include <stdio.h>
#include <stdlib.h>
int main() {
int rows, cols;
printf("Enter the number of rows and columns: ");
scanf("%d %d", &rows, &cols);
// Dynamically allocate memory for 'rows' pointers to arrays
int **matrix = (int **)malloc(rows * sizeof(int *));
// Allocate memory for each row (array of integers)
for (int i = 0; i < rows; i++) {
matrix[i] = (int *)malloc(cols * sizeof(int));
}
// Initialize and access the 2D array
for (int i = 0; i < rows; i++) {
for (int j = 0; j < cols; j++) {
matrix[i][j] = i + j; // Example initialization
}
}
Output:
Enter the number of rows and columns: 4 2
Matrix elements:
01
12
23
34
Array Operations: Operations that can be performed on any linear structure whether it is
anarray or a linked list include the following
a. Traversal- processing each element in the list
b. Search- Finding the location of the element with a given key.
c. Insertion- Adding a new element to the list
d. Deletion- Removing an element from the list.
e. Sorting- Arranging the elements in some type of order.
f. Merging- combining two list into a single list.
Traversing Linear Arrays: Traversing an array is accessing and processing each element exactly
once. Considering the processing applied during traversal as display of elements the array can be
traversed as follows
void displayarray(int a[])
{
int i;
printf("The Array Elements are:\n");
for(i=0;i<n;i++)
printf("%d\t",a[i]);
}
Insertion
• Inserting an element at the end of the array can be done provided the memory space allocated
for the array is large enough to accommodate the additional element.
• If an element needs to be inserted in the middle then all the elements form the specified position
to the end of the array should be moved down wards to accommodate the new element and to
keep the order of the other element.
• The following function inserts an element at the specified position
for(i=n-1;i>=pos;i--)
a[i+1]=a[i]; //makes space for the new element
a[pos]=element;
*n++;
}
}
Deletion
• If an element needs to be deleted in the middle then all the elements form the specified position
to the end of the array should be moved upwards to fill up the array.
• The following function deletes an element at the specified position
*n--;
}
}
Sorting: Sorting refers to the operation of rearranging the elements of an array in increasing or
decreasing order.
Example: Write a program to sort the elements of the array in ascending order using bubble
sort.
#include<stdio.h>
void main()
{
int a[10],i,j,temp,n;
printf("enter the size of the array : ");
scanf("%d",&n);
printf("enter the elements of the array\n");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
for(i=1;i<=n-1;i++)
for(j=0;j<n-i ;j++)
if (a[j] >a[j+1])
{
temp=a[j];
a[j]=a[j+1];
a[j+1]= temp;
}
printf("the sorted array is \n");
for(i=0;i<n;i++)
printf("%d \t",a[i]);
return(0);
}
Polynomials
Polynomials: A polynomial is a sum of terms, where each term has a form axe, where x is the variable,
a is the coefficient, and e is the exponent.
The largest (or leading) exponent of a polynomial is called its degree. Coefficients that are zero are
not displayed.
• Standard mathematical definitions for the sum and product of polynomials are:
• Assume that we have two polynomials
then
ADT Polynomial is objects: a set of ordered pairs of <ei, ai> where ai is Coefficients and ei is
Exponents, ei are integers >= 0
Functions:
for all poly,poly1,poly2 ∈ Polynomial,coef ∈ Coefficients, expon ∈ Exponents
Polynomial Representation:
• A polynomial can be represented as an array of structures as follows.
• Only one global array, terms, is used to store all the polynomials.
• The C declarations needed are:
For our example, startA = 0, finishA = 1, startB = 2, finishB = 5, and avail =6.
Coef 2 1 1 10 3 1
Exp 1000 0 4 3 2 0
0 1 2 3 4 5 6 7 8
since A (x) = 2x 1000 + 1 uses only six units of storage: one for startA, one for finishA, two for the
coefficients, and two for the exponents. However, when all the terms are nonzero, the current
representation requires about twice as much space as the first one. This representation is useful only
when the number of non zero terms are more.
Polynomial addition
• C function that adds two polynomials, A and B to obtain the resultant polynomial D = A + B.
The polynomial is added term by term.
• The attach function places the terms of D into the array, terms starting at position avail,.
• If there is not enough space in terms to accommodate D, an error message is printed to the
standard error device and we exit the program with an error condition.
void padd(int startA,int finishA,int startB, int finishB, int *startD,int *finishD)
{
/ * add A(x) and B(x) to obtain D(x) */
float coefficient;
*startD = avail;
while (startA <= finishA && startB <= finishB)
{
switch(COMPARE(terms[startA].expon, terms[startB].expon))
{
case -1: attach(terms[startB].coef,terms[startB].expon);
startB++;
break;
case 0: coefficient = terms[startA].coef + terms[startB].coef;
if (coefficient)
attach(coefficient,terms[startA].expon);
startA++;
startB++;
break;
case 1: attach(terms[startA].coef,terms[startA].expon);
startA++;
}
}
while(startA <= finishA)
{
attach(terms[startA].coef,terms[startA].expon); /* add in remaining terms of A(x) */
startA++;
}
*finishD = avail-1;
}
Sparse Matrices
• If a matrix contains m rows and n columns the total number of elements in such a matrix is
m*n. If m equals n the matrix is a square matrix.
When a sparse matrix is represented as a two dimensional array space is wasted for example if we
have 1000x 1000 matrix with only 2000 non zero element, the corresponding 2 dimensional array
requires space for 1,000,000 elements.
ADT Sparse Matrix objects: a set of triples, <row, column, value>, where row and column are
integers and form a unique combination, and value comes from the set item.
Functions:
for all a, b∈SparseMatrix, x∈item, i, j, maxCol, maxRow∈index
Example
Figure 1.4 two dimensional array and its sparse matrix stored as triples
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[j][i] in the transposematrix.
The algorithm finds all the elements in column 0 and store them in row 0 of the transpose matrix, find
all the elements in column 1 and store them in row 1, etc." Since the original matrix was ordered by
rows and the columns were ordered within each row. The transpose matrix will also be arranged in
ascending order. The variable, currentb, holds the position in b that will contain the next transposed
term. The terms in b is generated by rows by collecting the nonzero terms from column i of a
The transpose b of the sparse matrix a of figure 1.4b is shown in figure 1.5
b[5] 2 5 28
b[6] 3 0 22
b[7] 3 2 -6
b[8] 5 0 -15
Analysis of transpose: Hence, the asymptotic time complexity of the transpose algorithm is
O(columns·elements).
It first determines the number of elements in each column of the original matrix. This gives us the
number of elements in each row of the transpose matrix. From this information, we can determine the
starting position of each row in the transpose matrix. We now can move the elements in the original
matrix one by one into their correct position in the transpose matrix. We assume that the number of
columns in the original matrix never exceeds MAX_COL.
• The first two for loops compute the values for rowTerms, the third for loop carries out the
computation of startingPos, and the last for loop places the triples into the transpose matrix.
These four loops determine the computing time of fastTranspose.
• The bodies of the loops are executed numCols, numTerms, numCols - 1, and numTerms times,
respectively. The computing time for the algorithm is O(columns + elements).
• However, transpose requires less space than fastTranspose since the latter function must
allocate space for the rowTerms and startingPos arrays.
Strings
Strings: A string is an array of characters that is delimited by the null character (\0).
C L A S S \0
S[0] S[1] S[2] S[3] S[4] S[5]
Using this declaration the compiler would have reserved just enough space to hold each character
word including the null character. In such cases we cannot store a string of length more than 5 in s
String Null(m) ::= Return a string whose length is m characters long, but is initially set to
NULL. We write NULL as “”
Integer compare(s, t)::= If s equals t return 0
Else if s precedes t return -1
Else return +1
Boolean ISNull(s) ::= If (compare(s, NULL)) return FALSE
Else return TRUE
Integer Length(s) ::= If(compare(s, NULL))
Returns the number of characters in s else returns 0
String concat(s,t) ::= If(compare(t, NULL))
Return a string s whose elements are those of s followed by those of t
C provides several string functions which we access by including the header file string.h
Given below is a set of C string functions :
char *strcat(char *dest, const char Appends the string pointed to, by src to the end of the
*src) string pointed to by dest.
char *strncat(char *dest, const char Appends the string pointed to, by src to the end of the
*src, size_t n) string pointed to, by dest up to n characters long.
int strcmp(const char *str1, const Compares the string pointed to, by str1 to the string
char *str2) pointed to bystr2.
char *strcpy(char *dest, const char Copies the string pointed to, by src to dest and return dest
*src)
char *strncpy(char *dest, const char Copies n characters from the string pointed to,
*src, size_t n) by src to dest and returns dest
size_t strlen(const char *str) Returns the length of the string str . But not including the
terminating null character.
char *strchr(const char *str, int c) Returns pointer to the first occurrence of c in str . Returns
NULL if not present
char *strrchr(const char *str, int c) Returns pointer to the last occurrence of c in str . Returns
NULL if not present
char *strtok(char *str, const char Returns a token from string str . Tokens are separated
*delim) by delim.
char *strstr(char *str, const char Returns pointer to start of pat in str
*pat)
size_t strspn(const char *str, const Scan str for characters in spanset, returns the length of the
char *spanset) span
size_t strcspn(const char *str, const Scans str for character not in spanset, returns the length of
char *spanset) the span
char *strpbrk(const char *str, const Scan str for characters in spanset, returns pointer to first
char *spanset) occurrence of a character from spanset
Storing Strings
Strings are stored in three types of structures
1. Fixed Length structure
2. Variable Length structure with fixed maximums
3. Linked structures
1. Fixed length Storage, record oriented: In this structure each line of text is viewed as a record
where all records have the same length or have the same number of characters
Example: Assuming our record has a maximum of 12 characters per record the strings are stored as
follows
0 D A T A
1 S T R U C T U R E S
2 A N D
3 A P P L L I C A T I O N
4
5
Advantages:
• Ease of accessing data from any given record
• Ease of updating data in any given record( provided the length of the new data does not exceed
the record length
Disadvantages
• Time is wasted reading an entire record if most of the storage consist of in essential blank
spaces
• Certain records may require more space or data than available
• When the correction consist of more or fewer characters than original text, updation requires
the entire record to be changed( the disadvantage can be resolved by using array of pointers)
2. Variable Length storage with fixed maximum: The storage of variable length strings in memory
cells wth fixed lengths can be done in two ways
• Use a marker such as ($) to mark the end of the string
• List the length of the string as an additional field in the pointer array
Example :
0 5 D A T A $
1 11 S T R U C T U R E S $
2 4 A N D $
3 12 A P P L I C A T I O N $
4 0
5 0
3. Linked storage: Linked list is an ordered sequence of memory cells called nodes, where each node
stores two information the data and also stores the address of the next node in the list. Strings may be
stored in linked list as each node storing one character or a fixed number of characters and a link
containing the address of the node containing the next group of characters.
Example:
Pattern Matching
String insertion function
S= A m o b i l e ‘\0\
t= U t o ‘\0\
Initially
Temp= ‘\0\
Strncpy(temp,s,i)
a ‘\0\
Strcat(temp,t)
a U t o ‘\0\
Strcat(temp,(s+i))
a u T o m o b i L e ‘\0\
Consider two string str1 and str2 . insert srting str2 into str1 at position i.
# include<string.h>
# define max_size 100
Char str1[max_size];
Char str2 [max_size];
if (strlen(t))
{
strncpy(temp,s,i);
strcat(temp,t);
strcat(temp,(s+i));
strcpy(s,temp);
}}
Pattern matching : Consider two strings str and pat where pat is a pattern to be searched for in
stri. The easiest way to find if the pat is in str is by using the built in function strstr.
Since there are different methods to find pattern matching discussed below are two functions that
finds pattern matching in a more efficient way.
The easiest and the least efficient method in pattern matching is sequential search. The computing
time is of O(n.m).
for(i=0;endmatch<=lasts;endmatch++,strt++)
{
If(string[endmatch] == pat[lastp])
{
j=0;i= start;
while(j<lastp && string[i]== pat[j])
{
i++;
j++);
}
}
if(j==lastp)
return start;
}
return -1
a A b
j lastp
a B a b b A a b a a
s em ls
a B a b b A a b a a
a B a b b A a b a a
a B a b b A a b a a
a B a b b A a b a a
s em ls
No Match
A B a b b A a b a a
S em ls
Match
Analysis of nfind algorithm: The speed of the program is linear O(m) the length of the string in the
best and average case but the worst case computing is still O(n.m)
Example: For the pattern pat=abcabcacab we have the failure values calculated as below
j 1 2 3 4 5 6 7 8 9 10
pat a b c a b c a c a b
failure 0 0 0 1 2 3 1 3 1 2
Therefore when the failure function is not known in advance the total
computing time isO(strlen(string)) + O(strlem(pa))
Ms. Gowthami J, Department of ISE(DS), ACET Page 37
Data Structures and Applications/BCS304 Module-1
Stack
Stack Definition and Examples
• Stack is an ordered list in which insertions (also called push) and deletions (also called pops )
are made at one end called the top.
• Given a stack S = (a0, ...., an-1), we say that a0 is the bottom element, an-1 is the top
element, and ai is on top of element ai-1, 0 < i < n.
• Since the last element inserted into a stack is the first element removed, a stack is also known
as a Last-In-First-Out (LIFO) list.
Implementation of stack
• The first, or bottom, element of the stack is stored in stack [0], the second in stack [1], and the
ith in stack [i-1].
• Variable top points to the top element in the stack.
• Top= -1 to denote an empty stack.
ADTStack is
objects: a finite ordered list with zero or more elements.
#include <stdio.h>
#include <stdlib.h>
#define MAX_STACK_SIZE 100 // Define the structure for an element in the stack typedef struct
{
int key;
int element;
}
Element;
Element stack[MAX_STACK_SIZE];
int top = -1; // Stack pointer
// Function to check if the stack is empty int IsEmpty()
{
return top < 0;
}
// Function to check if the stack is full int IsFull()
{
return top >= MAX_STACK_SIZE - 1;
}
// Function to push an element to the stack void push(Element item)
{
if (top>=MAX_STACK_SIZE-1)
{
StackFull();
}
stack[++top] = item;
}
// Function to pop an element from the stack Element Pop()
{
if (top == -1)
{
return stackEmpty();
}
return stack[top--];
}
// Function to handle stack full error void StackFull()
{
fprintf(stderr, "Stack is full, cannot add element\n");
exit(EXIT_FAILURE);
}
// Function to handle stack empty error (assuming you have this function) Element stackEmpty()
{
fprintf(stderr, "Stack is empty, cannot pop element\n");
exit(EXIT_FAILURE);
}
If we do not know the maximum size of the stack at compile time, space can be allocated for the
elements dynamically at run time and the size of the array can be increases as needed.
Creation of stack: Here the capacity of the stack is taken as 1. The value of the capacity can be altered
specific to the application
StackCreateS() ::=
int *stack
Stack=(int*)malloc(stack, sizeof(int));
int capacity = 1;
int top = -1;
The function push remains the same except that MAX_STACK_SIZE is replaced with capacity
pop()
{/* delete and return the top element from the stack */
if (top == -1)
return stackEmpty(); /* returns an error key */
return stack[top--];
}
Stackfull with Array doubling:The code for stackFull is changed. The new code for stackFull
attempts to increase the capacity of the array stack so that we can add an additional element to the
stack. In array doubling, the capacity of the array is doubled whenever it becomes necessary to increase
the capacity of an array.
void stackFull()
{
stack=(int*)realloc(stack, 2 * capacity * sizeof(int))
capacity =capacity * 2;
}
Analysis
• In the worst case, the realloc function needs to allocate 2*capacity *sizeof (*stack) bytes of
memory and copy capacity*sizeof (*stack)) bytes of memory from the old array into the new
one.
• Under the assumptions that memory may be allocated in O(1) time and that a stack element
can be copied in O(1) time, the time required by array doubling is O(capacity). The total time
spent in array doubling is of O(n) where n is the total number of push operations.
• Hence even with the time spent on array doubling in the total time of push over all n pushes in
O(n). This conclusion is valid even the stack array is resized by a factor c>1.
Application of stack
➢ Conversion of Expression
➢ Evaluation of expression
➢ Recursion
To convert an expression from infix to prefix or postfix we follow the rules of precedence.
• Precedence : The order in which different operators are evaluated in an expression is called
precendence
• Associativity : The order in which operators of same precedence are evaluated in an expression
is called Associativity.
The operators are listed in the order of higher precedence down to lower precedence
Operator Associativity
--,++ left-to-right
Unary operators ,!,-,+, &, *,sizeof Right to left
*,/,% left-to-right
+,- left-to-right
The operands in the infix and the postfix expression are in the same order. With respect to operators ,
precedence of operators plays an important role in converting an infix expression to postfix expression.
We make use of the stack to insert the operators according to their precedence.
Algorithm Polish(Q,P)
Suppose Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent
Postfix expression P.
char symbol,item;
push('#');
for (i=0;infix[i]!='\0';i++)
{
symbol=infix[i];
switch(symbol)
{
case '(': push(symbol);
break;
case ')':item=pop();
while(item!='(')
{
postfix[p++]=item;
item=pop();
}
break;
case '+':
case '-':
case '*':
case '/':
case '%':while(precd(s[top])>=precd(symbol))
{
item=pop();
postfix[p++]=item;
}
push(symbol);
break;
Analysis: Let n be length of the infix string. (n) time is spent extracting tokens . There are two while
loop where the total time spent is (n) since the number of tokens that get stacked and unstacked is
linear in n . So the complexity of the function is (n)
Each operator in a postfix string refers to the previous two operands in the string. If we are parsing a
string, each time we read operands we push it to the stack and when we read a operator, its operands
will be the two topmost elements in the stack. We can then pop these two operands and perform the
indicated operation and push the result back to the stack so that it can be used by the next operator.
The following function evaluates a postfix expression using a stack and a stack of float elements is
declared globally
float s[25];
int top;
res=pop();
return(res);
}
➢ It does not check if the postfix expression is valid or not. If we input erroneous expression it
returns wrong result
➢ We cannot enter negative numbers, as the symbol to indicate negation will be misinterpreted
as subtraction operation
Ms. Gowthami J, Department of ISE(DS), ACET Page 46
Data Structures and Applications/BCS304 Module-1
Analysis: Let n be length of the postfix string then the complexity of the function is (n)
Algorithm PostfixEval(P)
This algorithm finds the VALUE of an arithmetic expression P written in postfix notation
Recursion: Recursion is the process of defining an object in terms of a simpler case of itself.
Suppose p is a function containing either a call statement to itself (direct recursion) or a call statement
to a second function that may eventually result in a call statement back to the original function
P(indirect recursion). Then the function P is called a recursive function.
• There must be a certain argument called the base value for which the function will not call
itself.
• Each time the function refers to itself the argument of the function must be closer to the base
value.
Factorial function: The factorial of a number n is got by finding the product of all the numberform
1 to n. ie 1*2*3…*n.. It is represented as n!
Example 4!=4*3*2*1=24
5!=5*4*3*2*1=120
0!=1
From a close observation it can be observed that 5!= 5*4! . Therefore n!=n*(n-1)!
• The definition is recursive since the function refers to itself for all value of n>0.
• The value of n! is explicitly given as 1 when the value of n=0 which can be taken as the base
value.
• This can be implemented by the code
factorial(int n)
{
f=1;
for(i=1;i<=n;i++)
f=f*i;
return(f)
}
This is the iterative implementation of the factorial function
For example
factorial (5)=5*factorial(4)
factorial (4)=4*factorial(3)
factorial (3)=3*factorial(2)
factorial(2)=2*factorial(1)
factorial(int n)
{
if (n==0 )
return(1);
else
return(n*factorial(n-1))
}
Fibonacci numbers in C
Example:
Fibo(4)= fibo(3)+fibo(2)
=fibo(2)+fibo(1)+fibo(2)
=fibo(1)+fibo(0)+fibo(1)+ fibo(2)
= 1+ fibo(0)+fibo(1)+ fibo(2)
=1+0 + fibo(1)+ fibo(2)
Ms. Gowthami J, Department of ISE(DS), ACET Page 48
Data Structures and Applications/BCS304 Module-1
=1+0+1+fibo(2)
=2+ fibo(1)+ fibo(0)
=2+1+fibo(0)
=2+1+0= 3
GCD of two numbers: The function accepts two numbers as input and returns the gcd of the two
numbers
gcd(int m, int n)
{
if (n==0)
return m;
retrun(gcd(n,m%n))
}
Example:
gcd(2,0)=2
gcd(0,2)= gcd(2,0)=2
gcd(4,2)= gcd(2,0)=2
gcd(7,3)= gcd(3,1)= gcd(1,0)=1
Binary search in C
Programs
1. Write a program to solve the Tower of Hanoi problem using a recursive function
void tower(int n,char source,char temp,char dest)
{
if(n==1)
{
printf("Move disc 1 from %c to %c\n",source,dest);
count++;
return;
}
tower(n-1,source,dest,temp);
printf("Move disc %d from %c to %c\n",n,source,dest);
count++;
tower(n-1,temp,source,dest);
}
Void main()
{
int n,count;
printf("Enter the number of discs\n");
scanf("%d",&n);
tower(n,'A','B','C');
printf("The number of moves=%d\n",count);
}
Note: Ideal number of moves to solve Tower of Hanoi is given as 2n -1 where n is the total number of
disks
Ackermann Function
The Ackermann function is a function with two arguments each of which can be assigned any non
negative integer 0,1,2… ........... This function finds its application in mathematical logic.
This function is defined as follows
a) If m = 0 then A(m,n) = n + 1
b) If m != 0 but n = 0 then A(m,n) = A(m - 1,1)
c) If m != 0 and n != 0 then A(m,n) = A(m - 1, A(m,n - 1))
Example1:
A(1,2) =A(0,A(1,1))
=A(0,A(0,A(1,0)))
= A(0,A(0,A(0,1))
=A(0,A(0,2)
=A(0,3)
4
Example 2:
A(1,3) = A(0,A(1,2))
=A(0,A(0,A(1,1)))
=A(0,A(0,A(0,A(1,0))))
=A(0,A(0,A(0,A(0,1))))
=A(0,A(0,A(0,2)
=A(0,A(0,3)
=A(0,4)
5
Iterative Recursive
Implemented using looping Implemented using recursive calls to
statements functions
Executes faster Takes more time to execute
Memory utilization is Less Memory utilization is more
Lines of code are more Lines of code are lesser
Does not require stack Implementation requires stack