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

Datastructure Unit 1 SKM

This document provides an overview of data structures and algorithms. It discusses basic terminology like abstract data types and complexity analysis using Big O notation. It then covers various data structures like arrays, linked lists, stacks, queues, trees and graphs. For each data structure, it provides definitions, representations, operations and advantages/disadvantages. Arrays and linked lists are discussed in more detail with information on declaring, accessing and storing data in arrays and the basic structure of linked lists.

Uploaded by

krishna moorthy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
132 views

Datastructure Unit 1 SKM

This document provides an overview of data structures and algorithms. It discusses basic terminology like abstract data types and complexity analysis using Big O notation. It then covers various data structures like arrays, linked lists, stacks, queues, trees and graphs. For each data structure, it provides definitions, representations, operations and advantages/disadvantages. Arrays and linked lists are discussed in more detail with information on declaring, accessing and storing data in arrays and the basic structure of linked lists.

Uploaded by

krishna moorthy
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 110

CORE VI:DATA STRUCTURES

18UCAM302
UNIT I
Introduction to Data Structures and Algorithms:
Basic Terminology - Classification of Data
Structures – Abstract Data Type - Time and
Space Complexity – Big O Notation.
Arrays: Introduction - Declaration of Arrays –
Accessing the Elements of an Array - Storing
Values in Arrays - Operations on Arrays - Two-
dimensional Arrays - Multi-dimensional Arrays.
Linked Lists: Introduction: Linked List Versus
Arrays – Memory Allocation and De-allocation
for a Linked List.
Basic Terminology
Our aim has been to design good programs,
where a good program is defined as a program
that
• runs correctly
• is easy to read and understand
• is easy to debug and
• is easy to modify.
Definition
A data structure is basically a group of data
elements that are put together under one name, and
which defines a particular way of storing and
organizing data in a computer so that it can be used
efficiently.
Data structures are widely applied in the following
areas:

• Compiler design
• Operating system
• Statistical analysis package
• DBMS
• Numerical analysis
• Simulation
• Artificial intelligence
• Graphics
Elementary Data Structure
Organization
 Data structures are building blocks of a
program.
 A program built using improper data
structures may not work as expected.
 So as a programmer it is mandatory to
choose most appropriate data structures for a
program.
The term data means a value or set of values.

It specifies either the value of a variable or a


constant. (e.g., marks of students, name of an
employee, address of a customer, value of pi,
etc.).

While a data item that does not have


subordinate data items is categorized as an
elementary item, the one that is composed of
one or more subordinate data items is called a
group item.
 A record is a collection of data items. For
example, the name, address, course, and
marks obtained are individual data items.

 A file is a collection of related records. For


example, if there are 60 students in a class,
then there are 60 records of the students. All
these related records are stored in a file
CLASSIFICATION OF DATA
STRUCTURES
Data structures are generally categorized into
two classes:

• Primitive data structures

• non-primitive data structures.


linear data structures
non-linear data structures.
Primitive data structures
 Primitive data structures are the fundamental
data types which are supported by a
programming language.
 Some basic data types are

integer,
real,
character
boolean.
Non-primitive data structures
 Non-primitive data structures are those data
structures which are created using primitive
data structures.
 Examples of such data structures include

Linked lists.
Stacks
Trees, and
Graphs.
 Non-primitive data structures can further be

classified into two categories: linear and non-


linear data structures.
Linear and Non-linear Structures

Linear Structures

If the elements of a data structure are stored


in a linear or sequential order, then it is a
linear data structure.

Examples include arrays, linked lists, stacks,


and queues.
Linear data structures can be represented
in memory in two different ways.

• By means of sequential memory locations.

• By means of links
Non-linear data structure
if the elements of a data structure are not
stored in a sequential order, then it is a
non-linear data structure.

The relationship of adjacency is not


maintained between elements of a
non-linear data structure.

Examples include trees and graphs.


Arrays

 An array is a collection of similar data


elements. These data elements have the same
data type.

 The elements of the array are stored in


consecutive memory locations and are
referenced by an index (also known as the
subscript).
 In C, arrays are declared using the following
syntax:
 type name[size];
 For example,
 int marks[10];
Limitations of Array
 Arrays are of fixed size.

 Data elements are stored in contiguous


memory locations which may not be always
available.

 Insertion and deletion of elements can be


problematic because of shifting of elements
from their positions.
Linked Lists

 A linked list is a very flexible, dynamic data


structure in which elements (called nodes)
form a sequential list.

 In a linked list, each node is allocated space


as it is added to the list. Every node in the list
points to the next node in the list
In a linked list, every node contains the
following Two types of data:

• The value of the node.

• A pointer or link to the next node in the list.

• The last node in the list contains a NULL


pointer to indicate that it is the end or tail of
the list.
Simple linked list
Advantage & Disadvantage Of Linked
List

 Advantage: Easier to insert or delete data


elements .

 Disadvantage: Slow search operation and


requires more memory space
Stacks

Definition :
A stack is a linear data structure in which
insertion and deletion of elements are done
at only one end, which is known as the top of
the stack.

Stack is called a last-in, first-out (LIFO)


structure .
Array representation of a stack
Basic operations of Stack.

Push

Pop

 peep.
 The push operation adds an element to the
top of the stack.

 The pop operation removes the element from


the top of the stack.

 peep operation returns the value of the


topmost element of the stack. (without
deleting it)
Queues

 A queue is a first-in, first-out (FIFO) data


structure in which the element that is
inserted first is the first one to be taken out.

 The elements in a queue are added at one


end called the rear and removed from the
other end called the front
Array representation of a queue

Queue after insertion of a new element

Queue after deletion of an element


Trees

 A tree is a non-linear data structure which


consists of a collection of nodes arranged in a
hierarchical order.

 One of the nodes is designated as the root


node, and the remaining nodes can be
partitioned into disjoint sets such that each
set is a sub-tree of the root.
Tree (Binary tree)
Advantage & Disadvantage Of
Tree

 Advantage: Provides quick search, insert, and


delete operations

 Disadvantage: Complicated deletion


algorithm
Graphs

 A graph is a non-linear data structure which is a


collection of vertices (also called nodes) and
edges that connect these vertices.

 A node in the graph may represent a city and the


edges connecting the nodes can represent roads.

 unlike trees, graphs do not have any root node.


Rather, every node in the graph can be connected
with every another node in the graph
Graph
Advantage & Disadvantage Of
GRAPH

 Advantage: Best models real-world situations

 Disadvantage: Some algorithms are slow and


very complex
ABSTRACT DATA TYPE

 An abstract data type (ADT) is the way we


look at a data structure, focusing on what it
does and ignoring how it does its job.

 For example, stacks and queues are perfect


examples of an ADT.
 Data type : Data type of a variable is the set
of values that the variable can take. We have
already read the basic data types in C include
int, char, float, and double.

 Abstract : The word ‘abstract’ in the


context of data structures means considered
apart from the detailed specifications or
implementation.
TIME AND SPACE COMPLEXITY
 Analysing an algorithm means determining
the amount of resources such as time and
memory needed to execute it.

 The time complexity of an algorithm is


basically the running time of a program as a
function of the input size.

 Similarly, the space complexity of an


algorithm is the amount of computer memory
that is required during the program execution
as a function of the input size.
Worst-case, Average-case and
Best-case Time Complexity

 Worst-case running time

 Average-case running time

 Best-case running time


The space needed by a program depends on the
following two parts:
Fixed part:
It varies from problem to problem. It includes
the space needed for storing instructions,
constants, variables, and structured variables.

Variable part:
It varies from program to program. It includes
the space needed for recursion stack, and for
structured variables that are allocated space
dynamically during the runtime of a program.
BIG O NOTATION
The Big O notation, where O stands for ‘order of ’, is
concerned with what happens for very large values
of n.
 If f(n) and g(n) are the functions defined on a

positive integer number n, then f(n) = O(g(n)).

 That is, f of n is Big–O of g of n if and only if positive


constants c and n exist, such that f(n) < cg(n). It
means that for large amounts of data, f(n) will grow
no more than a constant factor than g(n)
ARRAYS
Array : Definition
 An array is a collection of similar data
elements. These data elements have the same
data type.

 The elements of the array are stored in


consecutive memory locations and are
referenced by an index (also known as the
subscript).
DECLARATION OF ARRAYS
Declaring an array means specifying the
following:
 Data type—the kind of values it can store,

for example, int, char, float, double.

 Name—to identify the array.

 Size—the maximum number of values that


the array can hold.
Arrays are declared using the following
syntax:

type name[size];

For example :
int marks[10];
ACCESSING THE ELEMENTS OF AN
ARRAY

 To access all the elements, we must use a


loop.

 That is, we can access all the elements of an


array by varying the value of the subscript
into the array.
CALCULATING THE ADDRESS OF
ARRAY ELEMENTS
 The formula for Address of data element is

A[k] = BA(A) + w(k – lower_bound)


 Here, A is the array.
 k is the index of the element of which we have
to calculate the address.
 BA is the base address of the array A and
 w is the size of one element in memory.
Given an array
int marks[] = {99,67,78,56,88,90,34,85},
calculate the address of
marks[4] if the base address = 1000.
 We know that storing an integer value
requires 2 bytes, therefore, its size is 2 bytes.

marks[4] = 1000 + 2(4 – 0)


= 1000 + 2(4) = 1008
Calculating the Length of an array
STORING VALUES IN ARRAYS
Initializing Arrays during Declaration

type array_name[size]={list of values};

int marks[5]={90, 82, 78, 95, 88};


Inputting Values from the Keyboard

An array can be initialized by inputting


values from the keyboard.

Code for inputting each element of the array


Assigning Values to Individual Elements

A simple assignment statement can be written


as
marks[3] = 100;

100 is assigned to the fourth element of the


array which is specified as marks[3].
OPERATIONS ON ARRAYS
 Traversing an array
 Inserting an element in an array
 Searching an element in an array
 Deleting an element from an array
 Merging two arrays
 Sorting an array in ascending or descending

order
Traversing an array

 Traversing an array

Traversing an array means accessing each


and every element of the array for a specific
purpose.
Algorithm for array traversal

 Step 1: [INITIALIZATION] SET I= lower_bound


 Step 2: Repeat Steps 3 to 4 while I<=

upper_bound
 Step 3: Apply Process to A[I]
 Step 4: SET I=I+1
 [END OF LOOP]
 Step 5: EXIT
Write a program to read and display n numbers
using an array.
#include <stdio.h>
#include <conio.h>
int main()
{
int i, n, arr[20];
clrscr();
printf("\n Enter the number of elements in
the array : ");
scanf("%d", &n);
for(i=0;i<n;i++)
{
printf("\n arr[%d] = ", i);
scanf("%d",&arr[i]);
}
printf("\n The array elements are ");
for(i=0;i<n;i++)
printf("\t %d", arr[i]);
return 0;
}
 Output
 Enter the number of elements in the array : 5
 arr[0] = 1
 arr[1] = 2
 arr[2] = 3
 arr[3] = 4
 arr[4] = 5
 The array elements are 1 2 3 4 5
INSERTING AN ELEMENT IN AN ARRAY

If an element has to be inserted at the end of


an existing array, then the task of insertion is
quite simple.

We just have to add 1 to the upper_bound


and assign the value.

Here, we assume that the memory space


allocated for the array is still available
Algorithm to append a new element
to an existing array

 Step 1: Set upper_bound=upper_bound+1

 Step 2: Set A[upper_bound]=VAL

 Step 3: EXIT
Algorithm to Insert an Element in the
Middle of an Array
The algorithm INSERT will be declared as
INSERT (A, N, POS, VAL). The arguments are
(a) A, the array in which the element has to be
inserted
(b) N, the number of elements in the array
(c) POS, the position at which the element has
to be inserted
(d) VAL, the value that has to be inserted
Step 1: [INITIALIZATION] SETI=N
Step 2: Repeat Steps 3and4 while I>= POS
Step 3: SET A[I+1]=A[I]
Step 4: SET I=I–1
[END OF LOOP]
Step 5: SET N=N+1
Step 6: SET A[POS]=VAL
Step 7: EXIT
program to insert a number at a given location in an array.

int main()

int i, n, num, pos, arr[10];

clrscr();

printf("\n Enter the number of elements in the array : ");

scanf("%d", &n);

for(i=0;i<n; i++)

printf("\n arr[%d] = ", i);

scanf("%d", &arr[i]);

}
printf("\n Enter the number to be inserted : ");
scanf("%d", &num);
printf("\n Enter the position at which the
number has to be added : ");
scanf("%d", &pos);
for(i=n–1;i>= pos; i––)
arr[i+1] = arr[i];
arr[pos] = num;
n = n+1;
printf("\n The array after insertion of %d is : ",
num);
for(i=0;i< n; i++)
printf("\n arr[%d] = %d", i, arr[i]);
getch();
return 0;
}
Output
Enter the number of elements in the array : 5
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 4
arr[4] = 5
Enter the number to be inserted : 0
Enter the position at which the number has to be
added : 3
The array after insertion of 0 is :
arr[0] = 1
arr[1] = 2
arr[2] = 3
arr[3] = 0
arr[4] = 4
arr[5] = 5
Deleting an element from an array
 Deleting an element from an array means
removing a data element from an already
existing array.

 If the element has to be deleted from the end


of the existing array, then the task of deletion
is quite simple.

 We just have to subtract 1 from the


upper_bound.
Algorithm to delete the last
element of an array

if we have to delete an element from the


middle of an array, then it is not a trivial
task.
Algorithm to Delete an Element from the Middle of an Array

The algorithm DELETE will be declared as


DELETE(A, N,POS). The arguments are:
(a) A, the array from which the element has to
be deleted

(b) N, the number of elements in the array

(c) POS, the position from which the element


has to be deleted
Algorithm to delete an element from the
middle of an array
Merging Two arrays
 Merging two arrays in a third array means
first copying the contents of the first array
into the third array and then copying the
contents of the second array into the third
array.

 Hence, the merged array contains the


contents of the first array followed by the
contents of the second array.
 If the arrays are unsorted, then merging the
arrays is very simple
PROGRAM TO MERGE TWO UNSORTED
ARRAYS.
MERGING OF TWO SORTED ARRAYS
TWO-DIMENSIONAL ARRAYS

A two-dimensional array is specified using

two subscripts where the first subscript

denotes the row and the second denotes the

column.
DECLARING TWO-DIMENSIONAL ARRAYS
 A two-dimensional array is declared as:

 data_type array_name[row_size][column_size];

 we can declare a two dimensional array as:

 int marks[3][5];
Two-dimensional array
There are two ways of storing a two-
dimensional array in the memory.

The first way is the row major order and


the second is the column major order.
Consider a 20 X 5 two-dimensional array marks which
has its base address = 1000 and the size of an
element = 2. Now compute the address of the
element, marks[18][ 4] assuming that the elements
are stored in row major order.

Solution
Address(A[I][J]) = Base_Address + w{N (I – 1) + (J – 1)}
Address(marks[18][4]) = 1000 + 2 {5(18 – 1) + (4 – 1)}
= 1000 + 2 {5(17) + 3}
= 1000 + 2 (88)
= 1000 + 176 = 1176
Initializing Two-dimensional arrays
 A two-dimensional array is initialized in the
same way as a one-dimensional array is
initialized.

 For example,
int marks[2][3]={90, 87, 78, 68, 62, 71};

 The above statement can also be written as:


int marks[2][3]={{90,87,78},{68, 62, 71}};
ACCESSING THE ELEMENTS OF TWO-
DIMENSIONAL ARRAYS

 The two-dimensional array contains two


subscripts, we will use two for loops to scan
the elements.

 The first for loop will scan each row in the 2D


array and the second for loop will scan
individual columns for every row in the array
PROGRAM TO PRINT THE ELEMENTS OF A 2D ARRAY.
MULTI-DIMENSIONAL ARRAYS
 A multi-dimensional array in simple terms is
an array of arrays.

 we have one index in a one-dimensional


array, two indices in a two-dimensional array,
in the same way, we have n indices in an n-
dimensional array or multi-dimensional array
Three-dimensional array
Linked Lists

 A linked list, is a linear collection of data


elements. These data elements are called nodes.

 we can implement a linked list using the


following code:
struct node
{
int data;
struct node *next;
};
Simple linked list
 how a linked list is maintained in the
memory.
 In order to form a linked list, we need a

structure called node which has two fields,


DATA and NEXT.
 DATA will store the information part and

NEXT will store the address of the next


node in sequence.
START pointing to the first element
of the linked list in the memory
Linked Lists versus Arrays
 Both arrays and linked lists are a linear
collection of data elements.

 But unlike an array, a linked list does not


store its nodes in consecutive memory
locations

 linked list does not allow random access of


data.
 But like an array, insertions and deletions can
be done at any point in the list in a constant
time.

 Another advantage of a linked list over an


array is that we can add any number of
elements in the list.
MEMORY ALLOCATION AND DE-ALLOCATION
FOR A LINKED LIST
 If we want to add a node to an already
existing linked list in the memory, we first
find free space in the memory and then use it
to store the information.

 The computer maintains a list of all free


memory cells. This list of available space is
called the free pool.
 Every linked list has a pointer variable START
which stores the address of the first node of
the list.

 Likewise, for the free pool (which is a linked


list of all free memory cells),we have a pointer
variable AVAIL which stores the address of
the first free space.
 when a new record has to be added, the
memory address pointed by AVAIL will be
taken and used to store the desired
information.

 After the insertion, the next available free


space’s address will be stored in AVAIL.

You might also like