0% found this document useful (0 votes)
11 views

Introduction to DS

The document provides an introduction to data structures, defining them as efficient ways to store and organize data for computer programs. It categorizes data structures into linear (e.g., arrays, stacks, queues, linked lists) and non-linear (e.g., trees, graphs), detailing their characteristics and operations such as traversing, searching, insertion, deletion, and modification. Additionally, it explains the concept of primitive and non-primitive data structures, along with examples and initialization methods for arrays.

Uploaded by

shreyassupe346
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)
11 views

Introduction to DS

The document provides an introduction to data structures, defining them as efficient ways to store and organize data for computer programs. It categorizes data structures into linear (e.g., arrays, stacks, queues, linked lists) and non-linear (e.g., trees, graphs), detailing their characteristics and operations such as traversing, searching, insertion, deletion, and modification. Additionally, it explains the concept of primitive and non-primitive data structures, along with examples and initialization methods for arrays.

Uploaded by

shreyassupe346
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/ 32

Introduction to Data Structures

• A computer program is a collection of


instructions to perform a specific task.

• For this, a computer program may need to


store data, retrieve data, and perform
computations on the data.
Definition
• Data Structure can be defined as the group of
data elements which provides an efficient way
of storing and organising data in the
computer so that it can be used efficiently.

• Some examples of Data Structures are arrays,


Linked List, Stack, Queue, etc.
Linear data structures
In linear data structures, the elements are arranged in
sequence one after the other. Since elements are
arranged in particular order, they are easy to
implement.
Array Data Structure
• In an array, elements in memory are arranged in
continuous memory(Consecutive blocks of
memory).

• An array is a data structure that contains a group of


elements. Typically these elements are all of the
same data type
Stack Data Structure
In stack data structure, elements are stored in the LIFO
(Last-in-first-out) principle. That is, the last element
stored in a stack will be removed first.
Queue Data Structure
• A Queue is a linear structure which follows a
particular order in which the operations are
performed. The order is First In First Out (FIFO).
• Note: queue is open at both its ends
Linked List Data Structure
• A linked list is a linear data structure that
includes a series of connected nodes. Here,
each node stores the data and the address of
the next node.
Non linear data structures
• The data structure where data items are not
organized sequentially is called non linear data
structure.
• Non-linear data structures are
– Trees
– Graphs
Graph Data Structure
• A Graph is a non-linear data structure consisting of
nodes and edges.
• In graph data structure, each node is called vertex
and each vertex is connected to other vertices
through edges.
Tree Data Structure
• A tree is a nonlinear hierarchical data structure that
consists of nodes connected by edges.
Operations on Data Structure
The most commonly used operations on data
structure are broadly categorized into following
types
o Traversing

o Searching

o Insertion

o Deletion

o Modification
• Traversing: Traversing a Data Structure means
to visit (Accessing) the element stored in it.
This can be done with any type of Data
Structure.
• Searching: Searching means to find a
particular element in the given data-
structure. It is considered as successful when
the required element is found.
• Insertion: It is the operation which we apply on all the
data-structures. Insertion means to add an element in
the given data structure. The operation of insertion is
successful when the required element is added to the
required data-structure.
• Deletion: It is the operation which we apply on all the
data-structures. Deletion means to delete an element in
the given data structure. The operation of deletion is
successful when the required element is deleted from
the data structure.
• Modification: Modification(Update) operation refers to
updating an existing element in the given data
structure.
Linear Data Structure and Non-linear Data Structure
Linear Data Structure Non-linear Data Structure
In a linear data structure, data elements In this structure, the elements are arranged
are arranged in a linear order hierarchically or non-linear manner.
Arrays, linked list, stack, queue are the Trees and graphs are the types of a non-
types of a linear data structure. linear data structure.

Due to the linear organization, they are Due to the non-linear organization, they are
easy to implement. difficult to implement.

While in non-linear data structure, data


In linear data structure, data elements can
elements can’t be traversed in a single run
be traversed in a single run only.
only.

In a linear data structure, memory is not While in a non-linear data structure,


utilized in an efficient way. memory is utilized in an efficient way.

Applications of linear data structures are Applications of non-linear data structures


mainly in application software are in Artificial Intelligence and image
development. processing.
Primitive data structure Non-primitive data structure
Primitive data structure is a kind of data Non-primitive data structure is a type
structure that stores the data of only one of data structure that can store the
type. data of more than one type.

Examples of primitive data structure are Examples of non-primitive data


integer, character, float. structure are Array, Linked list, stack.

Primitive data structure will contain some Non-primitive data structure can
value, i.e., it cannot be NULL. consist of a NULL value.

The size depends on the type of the data In case of non-primitive data
structure. structure, size is not fixed.
Array
• Why do we need arrays?
– We can use normal variables (v1, v2, v3, ..) when we
have a small number of objects, but if we want to
store a large number of instances, it becomes
difficult to manage them with normal variables.
– The idea of an array is to represent many instances in
one variable.
Array
• The fundamental data types, namely char, int, float, double,
variable of these types can store only one value at any
given time.

• To process such large amounts of data, C supports a derived


data type known as array that can be used.

• In its simplest form, an array can be used to represent a list


of numbers, or a list of names.

Ex:
• List of employees in an organization.
• Test scores of a class of students.
• List of customers and their telephone numbers.
• As we mentioned earlier, an array is a sequenced collection of
related data items that share a common name.

• Ex: an array name salary to represent a set of salaries of a


group of employees in an organization.

• We can refer to the individual salaries by writing a number


called index or subscript in brackets after the array name.
salary [10]

• represents the salary of 10 employee. While the complete set


of values is referred to as an array, individual values are called
elements.

Array will be of
• One-dimensional arrays
• Two-dimensional arrays
• Multidimensional arrays
ONE-DIMENSIONAL ARRAYS
• A list of items can be given one variable name
using only one subscript and such a variable is
called a single-subscripted variable or a one-
dimensional array.

• For example, if we want to represent a set of


five numbers, say (35, 40, 20, 57, 19), then we
may declare the variable number as follows
int number [5];
• and the computer reserves five storage locations as shown below:
number [0]
number [1]
number [2]
number [3]
number [4]

• The values to the array elements can be assigned as follows:


number [0] =35;
number [1] =40;
number [2] =20:
number [3] =57:
number [4] =19;

• This would cause the array number to store the values as shown below:
number [0] 35
number [1] 40
number [2] 20
number [3]
57
number [4]
19
Declaring Arrays
• Like any other variable, arrays must be declared before they
are used so that the compiler can allocate space for them in
memory.
• The general form of array declaration is
type arrayName [ arraySize ];

• The type specifies the type of element that will be contained


in the array, such as int, float, or char

• The size indicates the maximum number of elements that


can be stored inside the array.
• For example, to declare a 10-element array called RollNo of
type integer, use this statement −
int RollNo [10];
Initializing Arrays
An array can be initialized at either of the following
stages:
➢ At compile time
➢ At run time
Compile Time Initialization
The general form of initialization of arrays is:
type array-name[size] = { list of values };
The values in the list are separated by commas.

EX: int number[3] = { 0,10,20);


will declare the variable number as an array of size 3 and will assign
0,10,20 to each element.

int number[5] = {10, 20};


The First 2 elements will be assigned number[0]=10, number[1]=20
and remaining will be assigned as zero.
char city[5] = {‘B‘};
NULL if the type is char.
Run Time Initialization

• An array can be explicitly initialized at run time. This


approach is usually applied for initializing large arrays.
-------------------------
for (i = 0; i < 100; i= i+1)
{
if(i < 50)
sum[i] = 0.0;
else
sum[i] = 1.0;
}
• The first 50 elements of the array sum are initialized to
zero while the remaining 50 elements are initialized to
1.0 at run time,
• We can also use a read function such as scanf
to initialize an array.
int x[3];
scanf ("%d%d%d", &x[0], &[1], &[2]);
• will initialize array elements with the values
entered through the keyboard.
// Program to take 5 values from the user and store them in an array
// Print the elements stored in the array

#include <stdio.h>
int main()
{
int values[5];
printf("Enter 5 integers: ");
// taking input and storing it in an array
for(int i = 0; i < 5; ++i)
{
scanf("%d", &values[i]);
}

printf("Displaying integers: ");


// printing elements of an array
for(int i = 0; i < 5; ++i)
{
printf("%d\n", values[i]);
}
return 0;
}
TWO-DIMENSIONAL ARRAYS
• There could be situations where a table of values will
have to be stored.
• We can think the table as a matrix consisting of rows
and columns, can be declare by using two-dimensional
arrays.
• Two-dimensional arrays are declared as follows:
type array_name [row_size][column_size];

int x[3][4];
• Here, x is a two-dimensional (2d) array. The array can
hold 12 elements.
• You can think the array as a table with 3 rows and each
row has 4 columns.
//Example on 2D Array
#include<stdio.h>
int main()
{
int arr[10][10], row, col, i, j;
printf("Enter Row Size of Array (max. 10): ");
scanf("%d", &row);
printf("Enter Column Size of Array (max. 10): ");
scanf("%d", &col);
printf("\nEnter %d Array Elements: ", row*col);
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
scanf("%d", &arr[i][j]);
}
printf("\nThe Array is:\n");
for(i=0; i<row; i++)
{
for(j=0; j<col; j++)
printf("%d ", arr[i][j]);
printf("\n");
}
return 0;
}

You might also like