Data structure
Data structure is a particular way of organizing data in a computer so that it can be used
effectively.
Data Structures are widely used in almost every aspect of Computer Science i.e.
Operating System, Compiler Design, Artificial intelligence, Graphics and many more.
Data Structures are the main part of many computer science algorithms as they enable the
programmers to handle the data in an efficient way. It plays a vital role in enhancing the
performance of a software or a program as the main function of the software is to store and
retrieve the user's data as fast as possible
Example of data structures
1. Arrays
An array is a structure of fixed-size, which can hold items of the same data type. It can be an
array of integers, an array of floating-point numbers, an array of strings or even an array of
arrays (such as 2-dimensional arrays). Arrays are indexed, meaning that random access is
possible.
2. Linked Lists
A linked list is a sequential structure that consists of a sequence of items in linear order which
are linked to each other. Hence, you have to access data sequentially and random access is not
possible.
3. A stack is a LIFO (Last In First Out — the element placed at last can be accessed at first
structure which can be commonly found in many programming languages. This structure is
named as "stack" because it resembles a real-world stack — a stack of plates.
4.A queue is a FIFO (First In First Out — the element placed at first can be accessed at first
structure which can be commonly found in many programming languages. This structure is
named as "queue" because it resembles a real-world queue — people waiting in a queue.
5.A tree is a hierarchical structure where data is organized hierarchically and are linked together.
This structure is different than a linked list whereas, in a linked list, items are linked in a linear
order.
6.A graph consists of a finite set of verticesor nodes and a set of edges connecting these vertices.
Elementary Data Organization
Data: Data can be defined as an elementary value, for example, student's name and his roll
number are the data about the student.
Group Items: Data items which have subordinate data items are called Group item, for example,
name of a student can have first name and the last name.
Record: Record can be defined as the collection of various data items, for example, if we talk
about the student entity, then his name, address, course and marks can be grouped together to
form the record for the student.
File: A File is a collection of various records of one type of entity, for example, if there are 60
employees in a Bank, then there will be 60 records in the related file where each record contains
the data about each employee.
Entity: An entity is a person, place, thing, event or concept about which information recorded.
difference between a data type and data structure?
A data type describes pieces of data that all A data structure is a way of describing a
share a common property. certain way to organize pieces of data so that
operations and algorithms can be more easily
applied.
For example an integer data type describes For example tree type data structures often
every integer that the computer can handle. allow for efficient searching algorithms.
Values can directly be assigned to the data type The data is assigned to the data structure object
variables. using some set of algorithms and operations
like push, pop and so on.
No problem of time complexity Time complexity comes into play when
working with data structures.
Int, Float ,Double Stacks Queues,Trees
Primitive Data Structures
These are the structures which are supported at the machine level, they can be used to make non-
primitive data Structures. These are integral and are pure in form. They have predefined behavior
and specifications.
Examples: Integer, float, character etc
Non-primitive Data Structures
The non-primitive data structures cannot be performed without the primitive
data structures. Although, they too are provided by the system itself yet they are derived data
structures and cannotybe formed without using the primitive data structures.
The Non-primitive data structures are further divided into the following
categories:
1. Linear data structure
Data structure where data elements are arranged sequentially or linearly where the elements are
attached to its previous and next adjacent in what
is called a linear data structure. In linear data structure, single level is
involved. Therefore, we can traverse all the elements in single run only.
Linear data structures are easy to implement because computer memory is arranged in a linear
way. Its examples are array, stack, queue, linked list
2. Non Linear data structure
Data structures where data elements are not arranged sequentially or linearly are called non-
linear data structures. In a non-linear data structure, single level is not involved. Therefore, we
can't traverse all the elements in single run only. Non-linear data structures are not easy to
implement in comparison to linear data structure. It utilizes computer memory efficiently in
comparison to a linear data structure. Its examples are trees and graphs.
Data structure operations
1. Traversing- It is used to access each data item exactly once so that it can
be processed.
2. Searching- It is used to find out the location of the data item if it exists in
the given collection of data items.
3. Inserting- It is used to add a new data item in the given collection of data
items.
4. Deleting- It is used to delete an existing data item from the given
collection of data items.
5. Sorting- It is used to arrange the data items in some order i.e. in ascending
or descending order in case of numerical data and in dictionary order in case
of alphanumeric data.
6. Merging- It is used to combine the data items of two sorted files into
single file in the sorted form.
Application of Data structures
* To search and find a specified target value, such as maximum, minimum,mean, median,
average, count from a group of data objects the arrays are very efficient.
* A stack can be used as an "undo" mechanism in text editors; thisoperation has been
accomplished by keeping all text changes in a stack.
* Queues are used for CPU job scheduling and in disk scheduling
* Linked lists are used in Symbol Tables for balancing parenthesis and in representing Sparse
matrix.
* An operating system maintains a disk's file system as a tree, where file folders act as tree
nodes. The tree structure is useful because it easily accommodates the creation and deletion of
folders and files.
* Shortest Path algorithm is the commonly used algorithm for determining the shortest path
between any two points. This is practically used in applications such as network stations,
computer games, maps, satellite navigation system etc.
Algorithm
* Algorithm is a step by step Procedure, which defines set of instruction to be executed in a
certain order to get the desired output algorithm generally created independent of underlying
languages. Example and algorithm can be implemented in more than one programming language.
* From the data structure point of view, following are some important categories of algorithms.
* • Search - Algorithm to search an item in a data structure.
* Search - Algorithm to search an item in a data structure.
* Sort - Algorithm to sort items in a certain order.
* Insert - Algorithm to insert item in a data structure.
* Update - Algorithm to update an existing item in a data structure.
* Delete - Algorithm to delete an existing item from a data structure.
Characteristics of an Algorithm
Not all procedures can be called an algorithm. An algorithm should have the
following characteristics -
* Unambiguous - Algorithm should be clear and unambiguous. Each of its steps (or phases), and
their inputs/outputs should be clear and must lead to only one meaning.
* Input - An algorithm should have 0 or more well-defined inputs.
* Output - An algorithm should have 1 or more well-defined outputs, ands hould match the
desired output.•
* Finiteness - Algorithms must terminate after a finite number of step.
* As we know that all programming languages share basic code constructs like loops (do, for,
while), flow-control (if-else), etc. These common constructs can
* Feasibility - Should be feasible with the available resources.
* Independent - An algorithm should have step-by-step directions, which should be independent
of any programming code.
* How to Write an Algorithm?
* There are no well-defined standards for writing algorithms. Rather, it is
* problem and resource dependent. Algorithms are never written to support a particular
programming code particular programming code.
* As we know that all programming languages share basic code constructs like loops (do, for,
while), flow-control (if-else, etc. These common constructs can be used to write an algorithm.
* Example
* Let's try to learn algorithm-writing by using an example.
* Problem - Design an algorithm to add two numbers and display the result.
* Step 1 - START
* Step 2 - declare three integers a, b & c
* Step 3 - define values of a & b
* Step 4 - add values of a & b
* Step 5 - store output of step 4 to c
* Step 6 - print c
* Step 7 - STOP
Algorithms tell the programmers how to code the program. Alternatively, the algorithm
can be written as -
Step 1 - START ADD
Step 2 - get values of a & b
Step 3 - c- a + b
Step 4 - display c
Step 5 - STOP
In design and analysis of algorithms, usually the second method is used to describe an
algorithm. It makes it easy for the analyst to analyze the algorithm ignoring all unwanted
definitions. He can observe what operations are being used and how the process is flowing.
Writing step numbers, is optional.
We design an algorithm to get a solution of a given problem. A problem can be solved in
more than one ways.
UNIT II
Array
Array is a container which can hold a fixed number of items and these items should be of
the same type. Most of the data structures make use of arrays to implement their
algorithms. Following are the important Terms to understand the concept 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.
Representation of linear array in memory
Arrays can be declared in various ways in different languages. For illustration, let's take
Carray declaration.
Name
Elements
int array [10] = { 35, 33, 42, 10, 14, 19, 27, 44, 26, 31 }
As per the above illustration, following are the important points to be
considered.
• Index starts with 0.
• Array length is 10 which means it can store 10 elements.
• Each element can be accessed via its index. For example, we can fetch an
element at index 6 as 27.
Basic Operations
Following are the basic operations supported by an array.
Basic Operations
Following are the basic operations supported by an array.
• Traverse - print all the array elements one by one.
• Insertion - Adds an element at the given index.
• Deletion - Deletes an element at the given index.
• Search - Searches an element using the given index or by the value.
• Traverse Operation
• This operation is to traverse through the elements of an array.
• Example
• Following program traverses and prints the elements of an array:
#include <<stdio.h>
int main()
int age[5]={20,21,19,18,25};
int i;
for(i=0;i<5,it+)
printf("d", age[i]);
return 0;
When we compile and execute the above program, it produces the following result -
Output
20 21 19 18 25
• Insertion operation
• Given an array arr of size n, this article tells how to insert an element x in
this array arr at a specific position pos.
Approach:
Here's how to do it.
1. First get the element to be inserted, say x
2. Then get the position at which this element is to be inserted, say pos
3. Then shift the array elements from this position to one position forward, and
do this for all the other elements next to pos.
4. Insert the element x now at the position pos, as this is now empty.
#include <stdio.h> 1. We are using the maximum array size
#define MAX_ARRAY_SIZE 100 as 100 which is defined by
int main() the MAX_ARRAY_SIZE
{ 2.
3. Define a few integer variables to use in the
//2
program. size is the size of the
int size; array, newNumber is the number to
int newNumber, position; insert,position is the position to insert the
int i; number, and numArray is the array of
int numArray[MAX_ARRAY_SIZE]; size 100 to store all user provided int values.
//3 4.
printf("Enter size of the array : "); 5. Ask the user to enter the size of the array.
scanf("%d", &size); Store it in the size variable.
6.
//4 7. Run one for loop to read all contents of the
for (i = 0; i < size; i++) array. In the loop, ask the user to enter each
{ array elements and insert them to the array on
printf("Enter value for position %d : ", each loop.
i); 8.
scanf("%d", &numArray[i]); 9. Ask the user to enter the number to add in the
array. Store it in newNumber variable.
}
Ask the user again to enter the position in the
//5
array for the new number to insert. Store it
printf("Enter the number you want to in position variable.
add to this array : ");
scanf("%d", &newNumber); Start one for loop and move each number of
the array starting from that position to one step
//6 right. Starting from the last current element
printf("Enter the position in the array to (size - 1), shift all elements from the
add this number : "); desired position to the right by one index. This
scanf("%d", &position); loop must run in reverse to avoid overwriting
elements before they are moved:
//7
for (i = size; i >= position - 1; i--) Insert the newNumber to its position.
{
numArray[i + 1] = numArray[i]; Finally, print out the new array to the user.
}
//8
numArray[position - 1] = newNumber;
//9
for (i = 0; i < size + 1; i++)
{
printf("%d ", numArray[i]);
}
}
Deletion operation
A user will enter the position at which the array element deletion is required.
Deleting an element does not affect the size of the array. It also checks whether deletion is
possible or not, for example, if an array contains five elements and user wants to delete the
element at the sixth position, it isn't possible.
we need to shift array elements which are after the element to be deleted, it is very
inefficient if the size of the array is large or we need to remove elements from an array
repeatedly. In linked list data structure shifting isn't required only pointers are adjusted. If
frequent deletion is required and the number of elements is large, it is recommended to use
a linked list.