MCA Data Structures With Algorithms 01
MCA Data Structures With Algorithms 01
Names of Sub-Units
Introduction to Data Structure: Classi�ication of Data Structures, Data Structure Operations, Basic
Concepts of Pointers, Structures and Union, Algorithm, Characteristics of the Algorithm.
Overview
This unit begins by discussing about the concept of Stacks. Next, the unit discusses the Operations
of stack, representing stack using static arrays. Further the unit explains the Dynamic array for
representing stack. Towards the end, the unit discusses the application of stack.
Learning Objectives
Learning Outcomes
https://www.studytonight.com/data-structures/introduction-to-data-structures
https://www.geeksforgeeks.org/data-structures/
1.1 INTRODUCTION
A data structure is a de�ined format for managing, assessing, retrieving and storing data. Data
structures make it easy for users to work with the data they require in different ways. data structure
is designed or selected for storing the data to use different algorithms, in Computer programming. The
basic algorithm operations are integrated into the design of the data structure. Each data structure has
information related to the data values, the association between data and functions which are applied
to the data.
2
UNIT 01: Introduction to Data Structures
1.6 POINTERS
A Pointer is a derived data type that stores the address of another variable. A Pointer contains memory
addresses as their values. Pointers can be used to assign, access, and manipulate data values stored in
the memory allotted to a variable since it can access the memory address of that variable.
3
Data Structures with Algorithms
4
UNIT 01: Introduction to Data Structures
int temp;
// dereferenced pointers refers to the function is working with values at
addresses which can be passed in
temp=*firstvar;
*firstvar=*secondvar;
*secondvar=temp;
return;
}
int main(void)
{
int m = 100;
int n = 200;
printf("before swap: value of m: %d \n", m);
printf("before swap: value of n: %d \n", n);
// using "address of" operator to pass in the address of each variable
swap(&m, &n);
//check values outside the function after swap function.
printf("after swap: value of m: %d \n", m);
printf("after swap: value of n: %d \n", n);
return 0;
}
5
Data Structures with Algorithms
Pointer to an array is also acknowledged as array pointer. We are using the pointer to access the
constituents of the array. We have a pointer that emphasis to the (0th) component of the array. We can
similarly declare a pointer that can point to entire array rather than just a single component of the
array. Declaration of the pointer to an array. Declaration of pointer to an array:
extern char (*p)[];
char arr[10];
char (*p)[10] = &arr;
The given declaration refers to the pointer to an array of four integers. In this case, we use parenthesis
to denote pointer to an array. It is important to encounter pointer name and indirection operator inside
brackets. Sample program for pointer to array is given below:
#include <iostream>
using namespace std;
int main () {
// an array with 5 elements.
double balance[5] = {1000.0, 5.0,2.4, 27.0, 56.0};
double *p;
p = balance;
cout << "Array values using pointer " << endl;
for ( int i = 0; i < 5; i++ ) {
cout << "*(p + " << i << ") : ";
cout << *(p + i) << endl;
}
cout << "Array values using balance as address " << endl;
for ( int i = 0; i < 5; i++ ) {
cout << "*(balance + " << i << ") : ";
cout << *(balance + i) << endl;
}
return 0;
}
6
UNIT 01: Introduction to Data Structures
From this program, we can see pointer, which denotes 0th element of the array. We declare a pointer that
can point to an array instead of one element of an array. This pointer is used in the multidimensional
arrays. Syntax for a pointer to array of integer 5
data_type (*var_name)[size_of_array];
Example:
int (*ptr)[5];
From the given example, ptr is the pointer which refers to an array of 5 integers. The given subscript has
high precedence than indirection and it is essential to use the pointer name and indirection operation
inside parentheses. Data type of ptr is a pointer to an array of 5 integers.
7
Data Structures with Algorithms
}
}
The running of a Malloc() function is given below:
Lets intilize 5 memory blocks with odd numbers
Lets see the values
Value at position 0 is 1
Value at position 1 is 3
Value at position 2 is 5
Value at position 3 is 7
Value at position 4 is 9
8
UNIT 01: Introduction to Data Structures
9
Data Structures with Algorithms
1.8 STRUCTURES
Structure is a user de�ined data type that allows storing the amount of different data types. In structure, each
element is called a member. It can assess the use of templates and classes as it stores different information.
The keywords struct is used to de�ine the structure.
Syntax for structures is given as:
struct structurename
{
Datatype member1;
Datatype member2;
...
Datatype member;
};
Example of a structure is as follows:
struct employee
{ int id;
char name[10];
float salary;
};
10
UNIT 01: Introduction to Data Structures
In the given structure, k is a pointer to struct node variable. It is similar to the pointer to structure
and pointer to any other variable. It is a structure de�inition that has one member that is a pointer to
a structure of its kind. These structures are essential in applications of linked data structures such as
trees and lists. Contrary, the static data structure such as array where several elements that can be
inserted in the array is restricted by size of the array. It can be expanded or contracted. Operations such
as deletion or insertion of nodes in self-referential structures are straight forward alteration of pointers.
1.9 UNIONS
Union can be de�ined as user de�ined data type which has a collection of different variables of different
data types in a same memory location. It can be de�ined as several members but one member has a
value at a speci�ic point in time. It is a user de�ined data type but structures share the same memory
location.
For example :
Union
{
Char y;
int x;
} u;
The given example is user de�ined structure that has two members such as ‘x’ of type int and ‘y’ of type
character. If we evaluate the addresses of ‘x’ and ‘y’, we can see that the addresses are distinct. We can
conclude that members in structure do not share the same memory location. Like structure, we de�ine
the union in the same way but union keyword is used for de�ining union data type. Union contains the
data members, i.e., ‘x’ and ‘y’, also evaluate the addresses of both variables and identi�ied that both
variables have the same addresses. It states that union members share the same memory location.
1.10 ALGORITHM
Algorithm is de�ined as a �inite sequence of instructions that can be performed in a �inite amount of
effort in a given length of time. Algorithm should be simple and easy to understand. To execute by the
computer, we need a program that needs to be written in a formal language. Computers are not �lexible
compared to the human mind so programs must contain more information than algorithms. Here, we
may ignore the programming details and focus on the design of algorithms than programs.
11
Data Structures with Algorithms
Effectiveness: If the algorithm wants to be effective, it is signi�icant to get output to be feasible with
the available resources. It does not have redundant and unnecessary steps which could make an
algorithm ineffective.
Finiteness: The algorithm should stop eventually. Stopping refers to that you get expected output
that has no possible solution. Algorithm should terminate after a �inite number of steps is made.
Algorithm should always terminate after a de�ined number of steps and not be in�inite. It is
signi�icant to create a �inite algorithm.
De�initeness: Algorithm should specify every step and the steps must be involved in the process.
De�initeness means mentioning the sequence of operations for making input into output. Algorithm
should be unambiguous. Each step should be spelled out and must have quantitative data.
A data structure is a de�ined format for managing, assessing, retrieving and storing data. Data
Structures make it easy for users to work with the data they require in different ways.
The needs of data structures contain the following: ef�iciency, re-usability, and invisibility.
In the classi�ication of data structure, Trees also originate in the non-primitive and non-linear group
of data structure.
Data Structure is well-de�ined as a mathematical or logical model to store data and perform
operation on the stored data.
A Pointer is a derived data type that stores the address of another variable.
1.12 GLOSSARY
12
UNIT 01: Introduction to Data Structures
13
Data Structures with Algorithms
https://www.iare.ac.in/sites/default/�iles/PPT/IARE_DS_PPT_3.pdf
https://www.iare.ac.in/sites/default/�iles/DS.pdf
You can discuss with your friends the applications of data structures in a real-life environment,
Classi�ication of data structure and its need . Also discussed on pointers and Algorithm of data
structure.
14