0% found this document useful (0 votes)
12 views18 pages

DSA Unit 1

The document provides an overview of data structures, defining simple data, data types, and their classifications into primitive and non-primitive structures. It explains various data structures like arrays, linked lists, stacks, queues, trees, and graphs, along with their operations such as insertion, deletion, and searching. Additionally, it discusses the advantages and disadvantages of arrays, emphasizing their importance in programming for efficient data management.

Uploaded by

sairamnagarajan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

DSA Unit 1

The document provides an overview of data structures, defining simple data, data types, and their classifications into primitive and non-primitive structures. It explains various data structures like arrays, linked lists, stacks, queues, trees, and graphs, along with their operations such as insertion, deletion, and searching. Additionally, it discusses the advantages and disadvantages of arrays, emphasizing their importance in programming for efficient data management.

Uploaded by

sairamnagarajan7
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Data structure
First we define the meaning of simple data. Data are simply values or set of values. A data item
is either the value of a variable or a constant. For example, the value of a variable x is 5 which
is described by the data type integer, a data item is a row in a database table, which is described
by a data type. A data item that does not have subordinate data items is called an elementary
item. A data item that is composed of one or more subordinate data items is called a group
item. A record can be either an elementary item or a group item. For example, an employee’s
name may be divided into three sub items – first name, middle name and last name but the
social_security_number would normally be treated as a single item. Data may be organized in
many different ways: • The logical or mathematical model of a particular organization of data
is called a data structure. • A data structure is an arrangement of data in a computer’s memory
or even disk storage.
Data structure is the method to store and organize data to facilitate access and modifications •
A data structure, sometimes called data type, can be thought of as a category of data. Like,
Integer is a data category which can only contain integers. String is data category holding only
strings. A data structure not only defines what elements it may contain, it also supports a set of
operations on these elements, such as addition or multiplication. • Data structures are ways to
organize data ( information) for example: o Simple variables are consider as the Primitive types
o Array, the collection of data items of the same type, stored in memory at contiguously o
Linked list, the sequence of data items, each one points to the next one, stored in memory at
non-contiguously. • Data structures are building blocks of a program. If program is built using
improper data structures, then the program may not work as expected always. • The possible
ways in which the data items are logically related define different data structures. • A data
structure is a collection of different data items that are stored together in a clearly defined way.
The examples of several common data structures are string, arrays, Stacks, Queues, Linked list,
Binary Trees, Graph and Hash Tables.
In combination with Algorithm we may define the data structures as:
• Algorithms go with the data structures to manipulate the data i.e. Algorithms are used to
manipulate the data contained in these data structures as in the form of sorting and searching.
• More generally we can say: Algorithms + Data Structures = Programs.
Data Types
A data type is a classification of data, which can store a specific type of information. Data
types are primarily used in computer programming, in which variables are created to store data.
Each variable is assigned a data type that determines what type of data the variable may contain.
Thus a data type is a method of interpreting a pattern of bits. There are numerous different data
types. They are used to make the storage and processing of data easier and more efficient. A
data type is a term which refers to the kinds of data that variables may hold. With every
programming language there is a set of built-in data types. This means that the language allows
variables to name data of that type and provides a set of operations which meaningfully
manipulates these variables. Some data types are easy to provide because they are built-in into
the computer’s machine language instruction set, such as integer, character etc. Other data
types require considerably more efficient ways to implement. In some languages, these are
features which allow one to construct combinations of the built-in types (like structures in ‘C’).
However, it is necessary to have such mechanism to create the new complex data types which
are not provided by the programming language. The new type also must be meaningful for
manipulations. Such meaningful data types are referred as abstract data type. Different
programming languages have their own set of basic data types. Basic data types or primitive
data types The most common basic or intrinsic data types or primitive data types are as follows:
Integer: It is a positive or negative number that does not contain any fractional part.
Real: A number that contains the decimal part
Boolean: It is a data type that can store one of only two values, usually these values are TRUE
or FALSE
Character: It is any letter, number, punctuation mark or space, which takes up a single unit of
storage, usually a byte
String: It is sometimes just referred to as ‘text’. Any type of alphabetic or numeric data can be
stored as a string: “Delhi City”, “30/05/2013” and “459.78” are all examples of the strings.
Each character within a string will be stored in one byte using its ASCII code. The maximum
length of a string is limited only by the available memory.
Structure data types or Non Primitive data types There is another class of data types which is
considered as structure data types or non primitive data types. These data types are user defined
data types. Structured data types hold a collection of data values. This collection will generally
consist of the primitive data types. Examples of this would include arrays, records, list, tree
and files. These data types, which are created by programmers, are extremely important and
are the building block of data structures. These are more complex data structures. They stress
on formation of sets of homogeneous and heterogeneous data elements.
Type of Data Structures

A data structure is the portion of memory allotted for a model, in which the required data can
be arranged in a proper fashion. Normally The data structures are of two types or it can be
broadly classified into two types of data structures:
(i) Primitive data structure
(ii) Non-primitive data structure
1. Primitive data structure: The data structures that typically are directly operated upon by
machine level instruction. Examples: Integers, Real numbers, Characters and pointers, and their
corresponding storage representation. Programmers can use these data types when creating
variables in their programs. For example, a programmer may create a variable say “z” and
define it as a real data type. The variable will then store data as a real number. 2. Non primitive
data structure: N
2. Non primitive data structure: Non – primitive data structures are not defined by the
programming language, but are instead created by the programmer. They are also called as the
reference variables, since they reference a memory location, which stores the data. These data
structures are derived from the primitive data structures. Examples: Array, Stack, Queues,
Linked list, Tree, Graphs and hash table.
There are two type of-primitive data structures.
a) Linear Data Structures:- In linear data structure the elements are stored in sequential order.
Hence they are in the form of a list, which shows the relationship of adjacency between
elements and is said to be linear data structure. The most, simplest linear data structure is a 1-
D array, but because of its deficiency, list is frequently used for different kinds of data.
The linear data structures are:
Array: The Array is a collection of data of same data type stored in consecutive
memory location and is referred by common name.
Stack: A stack is a Last-In-First-Out or First-In-Last-Out linear data structure in
which insertion and deletion takes place at only from one end called the top of the
stack.
Queue: A Queue is a First-In-First-Out or Last-In-Last-Out data structure in which
insertion takes place from one end called the rear and the deletions takes place at
one end called the Front.
Linked List: Linked list is a collection of data of same type but the data items need
not be stored in consecutive memory locations. It is linear but non-contiguous type
data structure. A linked list may be a single list or double list.
Single Linked list: - A single list is used to traverse among the nodes in one
direction.
Double linked list: - A double linked list is used to traverse among the nodes in
both the directions.

b) Non-linear data structure:-


In non linear data structure the elements are stored based on the hierarchical relationship among
the data. A list, which doesn’t show the relationship of adjacency between elements, is said to
be non-linear data structure. The nonlinear data structures are:
Tree: This data structure is used to represent data that has some hierarchical
relationship among the data elements. Thus, it maintains hierarchical relationship
between various elements.
Graph: This data structure is used to represent data that has relationship between
pair of elements not necessarily hierarchical in nature. It maintains random
relationship or point-to-point relationship between various elements. For example
electrical and communication networks, airline routes, flow chart and graphs for
planning projects.
Data structure Operations
The data elements appearing in the data structure is processed by means of certain operations.
In fact the particular data structure that one chooses for a given situation depends largely on
the frequency with which specific operations are performed. The following major operations
performed on data structures are:
Insertion It provides the means for adding new details or new node into the existing data
structure.
Deletion It provides the means for removing a node from the data structure.
Traversing It provides the means for accessing each node exactly once so that the nodes of a
data structure can be processed. It is also called the visiting to data structure.
Searching It provides the means for finding the location of node for a given key value or
finding the locations of all records, which satisfy one or more conditions.
Sorting It provides the means for arranging the data in a particular order in given data structure.
Merging It provides the means for joining the two data structures. Note: Sometimes two or
more data structure of operati
ARRAYS

Array is a container which can hold fix number of items and these items should be of same type.
Most of the datastructure make use of array to implement their algorithms. Following are
important terms to understand the concepts of Array.
Element − Each item stored in an array is called an element.

Index − Each location of an element in an array has a numerical index which is used to identify
the element.

Properties of array
There are some of the properties of an array that are listed as follows -

o Each element in an array is of the same data type and carries the same size that is 4 bytes.
o Elements in the array are stored at contiguous memory locations from which the first element is stored
at the smallest memory location.
o Elements of the array can be randomly accessed since we can calculate the address of each element of
the array with the given base address and the size of the data element.
Why are arrays required?
Arrays are useful because -

o Sorting and searching a value in an array is easier.


o Arrays are best to process multiple values quickly and easily.
o Arrays are good for storing multiple values in a single variable - In computer programming, most cases
require storing a large number of data of a similar type. To store such an amount of data, we need to
define a large number of variables. It would be very difficult to remember the names of all the variables
while writing the programs. Instead of naming all the variables with a different name, it is better to
define an array and store all the elements into it.

Array Representation

Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.

Arrays can be declared in various ways in different languages. For illustration, let's take C array
declaration.

As per above shown illustration, following are the important points to be considered.

Index starts with 0.

Array length is 8 which means it can store 8 elements.

Each element can be accessed via its index. For example, we can fetch element at index 6 as
9.

Types of Arrays on the basis of Dimensions


There are majorly three types of arrays on the basis of dimensions:
1. One-dimensional array (1-D arrays):
You can imagine a 1d array as a row, where elements are stored one after another.
1D array

Syntax for Declaration of Single Dimensional Array


Below is the syntax to declare the single-dimensional array
data_type array_name[array_size];

where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.
 array_size: is the number of blocks of memory array going to have.
For Example
int nums[5];
2. Two-dimensional (2D) array:
Multidimensional arrays can be considered as an array of arrays or as a matrix consisting of rows and
columns.

Syntax for Declaration of Two-Dimensional Array


Below is the syntax to declare the Two-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension];
where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.
 sizeof_dimension: is the number of blocks of memory array going to have in the corresponding
dimension.
For Example
int nums[5][10];
3. Three-dimensional array:
A 3-D Multidimensional array contains three dimensions, so it can be considered an array of two-
dimensional arrays.
3D array

Syntax for Declaration of Three-Dimensional Array


Below is the syntax to declare the Three-dimensional array
data_type array_name[sizeof_1st_dimension][sizeof_2nd_dimension][sizeof_3rd_dimension];
where,
 data_type: is a type of data of each array block.
 array_name: is the name of the array using which we can refer to it.
 sizeof_dimension: is the number of blocks of memory array going to have in the corresponding
dimension.
For Example
int nums[5][10][2];

Basic Operations

Following are the basic operations supported by an array.

Traverse − print all the array elements one byone.

Insertion − add an element at given index.

Deletion − delete an element at given index.


Search − search an element using given index or by value.

Update − update an element at given index.

In C, when an array is initialized with size, then it assigns defaults values to its elements in following
order.

Insertion Operation

Insert operation is to insert one or more data elements into an array. Based on the requirement,
new element can be added at the beginning, end or any given index of array.

Here, we see a practical implementation of insertion operation, where we add data at the end of
the array −

Algorithm

Let Array is a linear unordered array of MAX elements.

Let LA is a Linear Array unordered with N elements and K is a positive integer such that K<=N.
Below is the algorithm where ITEM is inserted into the Kth position of LA −

1. Start
2. Set J=N
3. Set N = N+1
4. Repeat steps 5 and 6 while J >= K
5. Set LA[J+1] = LA[J]
6. Set J = J-1
7. Set LA[K] = ITEM
8. Stop

Deletion Operation

Deletion refers to removing an existing element from the array and re-organizing all elements of
an array.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Below is the algorithm to delete an element available at the Kth position of LA.

1. Start
2. Set J=K
3. Repeat steps 4 and 5 while J < N
4. Set LA[J-1] = LA[J]
5. Set J = J+1
6. Set N = N-1
7. Stop

Search Operation

You can perform a search for array element based on its value or its index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N.
Below is the algorithm to find an element with a value of ITEM using sequential search.

1. Start
2. Set J=0
3. Repeat steps 4 and 5 while J < N
4. IF LA[J] is equal ITEM THEN GOTO STEP 6
5. Set J = J +1
6. PRINT J, ITEM
7. Stop
Update Operation

Update operation refers to updating an existing element from the array at a given index.

Algorithm

Consider LA is a linear array with N elements and K is a positive integer such that K<=N. Below is
the algorithm to update an element available at the Kth position of LA

1. Start
2. Set LA[K-1] = ITEM
3. Stop

Advantages of the Array

o Arrays perform very well in the program because they have a very good ability to cache locality.
o We can represent many values of the same data type with just one variable name, so it makes it easier to
understand the code.
o We can access any element of the array randomly using index values, so it makes it very easy to operate
on arrays.

Disadvantages of the Array

o Insertion or deletion operation can be costly because the size of the array is fixed, and memory
allocation is static in the array.
o So if we want a data structure where insertion and deletion are easy, we can use the stack data structure,
which is based on the LIFO principle.

Applications of the Array

o We can use the array for CPU scheduling algorithms.


o Sorting algorithms can be implemented using the array data structure.
o An array is used to solve the complex problems related to matrices.
o Database records are arranged using an array data structure.
o Other data structures, like a queue, stack, hashmap, etc., are implemented by the array.
o Only one name is used for many variables.
o It can store the elements which have the same data type.
Sparse Matrix

What is a matrix?

A matrix can be defined as a two-dimensional array having 'm' rows and 'n' columns. A matrix
with m rows and n columns is called m×n matrix. It is a set of numbers that are arranged in the
horizontal or vertical lines of entries.

For example -

What is a sparse matrix?

Sparse matrices are those matrices that have the majority of their elements equal to zero. In
other words, the sparse matrix can be defined as the matrix that has a greater number of zero
elements than the non-zero elements.

Now, the question arises: we can also use the simple matrix to store the elements, then why is
the sparse matrix required.

Why is a sparse matrix required if we can use the simple matrix to store elements?

There are the following benefits of using the sparse matrix -

Storage - We know that a sparse matrix contains lesser non-zero elements than zero, so less
memory can be used to store elements. It evaluates only the non-zero elements.

Computing time: In the case of searching in sparse matrix, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elements. It saves computing time by
logically designing a data structure traversing non-zero elements.

Representation of sparse matrix

Now, let's see the representation of the sparse matrix. The non-zero elements in the sparse
matrix can be stored using triplets that are rows, columns, and values. There are two ways to
represent the sparse matrix that are listed as follows -

o Array representation
o Linked list representation
Array representation of the sparse matrix
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is
because zeroes in the matrix are of no use, so storing zeroes with non-zero elements is wastage
of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-
zero elements, it reduces the traversal time and the storage space.

In 2D array representation of sparse matrix, there are three fields used that are named as -

o Row - It is the index of a row where a non-zero element is located in the matrix.
o Column - It is the index of the column where a non-zero element is located in the
matrix.
o Value - It is the value of the non-zero element that is located at the index (row, column).
Example -

Let's understand the array representation of sparse matrix with the help of the example given
below -

Consider the sparse matrix -

In the above figure, we can observe a 5x4 sparse matrix containing 7 non-zero elements and
13 zero elements. The above matrix occupies 5x4 = 20 memory space. Increasing the size of
matrix will increase the wastage space.

The tabular representation of the above matrix is given below -


In the above structure, first column represents the rows, the second column represents the
columns, and the third column represents the non-zero value. The first row of the table
represents the triplets. The first triplet represents that the value 4 is stored at 0th row and 1st
column. Similarly, the second triplet represents that the value 5 is stored at the 0th row and 3rd
column. In a similar manner, all triplets represent the stored location of the non-zero elements
in the matrix.

The size of the table depends upon the total number of non-zero elements in the given sparse
matrix. Above table occupies 8x3 = 24 memory space which is more than the space occupied
by the sparse matrix. So, what's the benefit of using the sparse matrix? Consider the case if the
matrix is 8*8 and there are only 8 non-zero elements in the matrix, then the space occupied by
the sparse matrix would be 8*8 = 64, whereas the space occupied by the table represented using
triplets would be 8*3 = 24.

Linked List representation of the sparse matrix

In a linked list representation, the linked list data structure is used to represent the sparse matrix.
The advantage of using a linked list to represent the sparse matrix is that the complexity of
inserting or deleting a node in a linked list is lesser than the array.

Unlike the array representation, a node in the linked list representation consists of four fields.
The four fields of the linked list are given as follows -

o Row - It represents the index of the row where the non-zero element is located.
o Column - It represents the index of the column where the non-zero element is located.
o Value - It is the value of the non-zero element that is located at the index (row, column).
o Next node - It stores the address of the next node.
The node structure of the linked list representation of the sparse matrix is shown in the below
image -

Example -

Let's understand the linked list representation of sparse matrix with the help of the example
given below -

Consider the sparse matrix -


In the above figure, we can observe a 4x4 sparse matrix containing 5 non-zero elements and
11 zero elements. Above matrix occupies 4x4 = 16 memory space. Increasing the size of matrix
will increase the wastage space.

The linked list representation of the above matrix is given below -

In the above figure, the sparse matrix is represented in the linked list form. In the node, the first
field represents the index of the row, the second field represents the index of the column, the
third field represents the value, and the fourth field contains the address of the next node.

In the above figure, the first field of the first node of the linked list contains 0, which means
0th row, the second field contains 2, which means 2nd column, and the third field contains 1 that
is the non-zero element. So, the first node represents that element 1 is stored at the 0th row-
2nd column in the given sparse matrix. In a similar manner, all of the nodes represent the non-
zero elements of the sparse matrix.
DATA STRUCTURE - STACK
http://www.tutorialspoint.com/data_structures_algorithms/stack_algorithm.htm Copyright © tutorialspoint.com

A stack is an abstract data type ADT, commonly used in most programming languages. It is named
stack as it behaves like a real-world stack, for example − deck of cards or pile of plates etc.

A real-world stack allows operations at one end only. For example, we can place or remove a card
or plate from top of the stack only. Likewise, Stack ADT allows all data operations at one end only.
At any given time, We can only access the top element of a stack.

This feature makes it LIFO data structure. LIFO stands for Last-in-first-out. Here, the element which
is placed insertedoradded last, is accessed first. In stack terminology, insertion operation is called
PUSH operation and removal operation is called POP operation.

Stack Representation
Below given diagram tries to depict a stack and its operations −

A stack can be implemented by means of Array, Structure, Pointer and Linked-List. Stack can
either be a fixed size one or it may have a sense of dynamic resizing. Here, we are going to
implement stack using arrays which makes it a fixed size stack implementation.

Basic Operations
Stack operations may involve initializing the stack, using it and then de-initializing it. Apart from
these basic stuffs, a stack is used for the following two primary operations −

push − pushing storing an element on the stack.

pop − removing accessing an element from the stack.

When data is PUSHed onto stack.

To use a stack efficiently we need to check status of stack as well. For the same purpose, the
following functionality is added to stacks −

peek − get the top data element of the stack, without removing it.

isFull − check if stack is full.

isEmpty − check if stack is empty.

At all times, we maintain a pointer to the last PUSHed data on the stack. As this pointer always
represents the top of the stack, hence named top. The top pointer provides top value of the stack
without actually removing it.

First we should learn about procedures to support stack functions −

peek
Algorithm of peek function −

begin procedure peek

return stack[top]

end procedure

Implementation of peek function in C programming language −

int peek() {
return stack[top];
}

isfull
Algorithm of isfull function −

begin procedure isfull

if top equals to MAXSIZE


return true
else
return false
endif

end procedure

Implementation of isfull function in C programming language −

bool isfull() {
if(top == MAXSIZE)
return true;
else
return false;
}

isempty
Algorithm of isempty function −

begin procedure isempty

if top less than 1


return true
else
return false
endif
end procedure

Implementation of isempty function in C programming language is slightly different. We initialize


top at -1, as index in array starts from 0. So we check if top is below zero or -1 to determine if stack
is empty. Here's the code −

bool isempty() {
if(top == -1)
return true;
else
return false;
}

PUSH Operation
The process of putting a new data element onto stack is known as PUSH Operation. Push operation
involves series of steps −

Step 1 − Check if stack is full.

Step 2 − If stack is full, produce error and exit.

Step 3 − If stack is not full, increment top to point next empty space.

Step 4 − Add data element to the stack location, where top is pointing.

Step 5 − return success.

if linked-list is used to implement stack, then in step 3, we need to allocate space dynamically.

Algorithm for PUSH operation


A simple algorithm for Push operation can be derived as follows −

begin procedure push: stack, data

if stack is full
return null
endif

top ← top + 1

stack[top] ← data

end procedure

Implementation of this algorithm in C, is very easy. See the below code −


void push(int data) {
if(!isFull()) {
top = top + 1;
stack[top] = data;
}else {
printf("Could not insert data, Stack is full.\n");
}
}

Pop Operation
Accessing the content while removing it from stack, is known as pop operation. In array
implementation of pop operation, data element is not actually removed, instead top is
decremented to a lower position in stack to point to next value. But in linked-list implementation,
pop actually removes data element and deallocates memory space.

A POP operation may involve the following steps −

Step 1 − Check if stack is empty.

Step 2 − If stack is empty, produce error and exit.

Step 3 − If stack is not empty, access the data element at which top is pointing.

Step 4 − Decrease the value of top by 1.

Step 5 − return success.

Algorithm for POP operation


A simple algorithm for Pop operation can be derived as follows −

begin procedure pop: stack

if stack is empty
return null
endif

data ← stack[top]

top ← top - 1

return data

end procedure

Implementation of this algorithm in C, is shown below −

int pop(int data) {


if(!isempty()) {
data = stack[top];
top = top - 1;
return data;
}else {
printf("Could not retrieve data, Stack is empty.\n");
}
}

For a complete stack program in C programming language, please click here.


Loading [MathJax]/jax/output/HTML-CSS/jax.js

You might also like