Download as pdf or txt
Download as pdf or txt
You are on page 1of 19

UNIT -I

Data:
A collection of facts, concepts, figures, observations, occurrences or instructions in a
formalized manner.
Information:
The meaning that is currently assigned to data by means of the conventions applied to those
data(i.e. processed data)
Record:
Collection of related fields.
Data type:
Set of elements that share common set of properties used to solve a program.
Data Structures:
Data Structure is the way of organizing, storing, and retrieving data and their relationship with
each other.
Characteristics of data structures:
1. It depicts the logical representation of data in computer memory.
2. It represents the logical relationship between the various data elements.

3. It helps in efficient manipulation of stored data elements.

4. It allows the programs to process the data in an efficient manner.

Operations on Data Structures:

1. Traversal

2.Search

3.Insertion

4.Deletion
CLASSIFICATION OF DATA STRUCTURES

DATA
STRUCTURES

PRIMARY DATA SECONDARY DATA


STRUCTURES STRUCTURES
INT, CHAR, FLOAT
DOUBLE LINEAR NON LINEAR
DATA DATA

LIST ARRA STAC QUEU TREE GRAP


YS KS ES S HS
S

Primary Data Strucutres/Primitive Data Structures:


Primitive data structures include all the fundamental data structures that can be directly

manipulated by machine-level instructions. Some of the common primitive data structures


include integer, character, real, boolean etc

Secondary Data Structures/Non Primitive Data Structures:


Non primitive data structures, refer to all those data structures that are derived from one or
more primitive data structures. The objective of creating non-primitive data structures is to form
sets of homogeneous or heterogeneous data elements.

Linear Data Structures:


Linear data structures are data strucutres in which, all the data elements are arranged in i, linear
or sequential fashion. Examples of data structures include arrays, stacks, queues, linked lists, etc.

Non Linear Structures:


In non-linear data strucutres, there is definite order or sequence in which data elements are
arranged. For instance, a non-linear data structures could arrange data elements in a hierarchical
fashion. Examples of non-linear data structures are trees and graphs.

Static and dynamic data structure:

Static Ds:

If a ds is created using static memory allocation, ie. ds formed when the number of data items
are known in advance ,it is known as static data static ds or fixed size ds.
Dynamic Ds:

If the ds is created using dynamic memory allocation i.e ds formed when the number of data
items are not known in advance is known as dynamic ds or variable size ds.

Application of data structures:


Data structures are widely applied in the following areas:

Compiler design
Operating system
Statistical analysis package
DBMS
Numerical analysis
Simulation
Artificial intelligence
Graphics

ABSTRACT DATA TYPES (ADTS):

An abstract Data type (ADT) is defined as a mathematical model with a collection of operations
defined on that model. Set of integers, together with the operations of union, intersection and set
difference form a example of an ADT. An ADT consists of data together with functions that
operate on that data.
Advantages/Benefits of ADT:
1. Modularity
2.Reuse
3. code is easier to understand

4.Implementation of ADTs can be changed without requiring changes to the program that uses the
ADTs.

THE LIST AI)T:


List is an ordered set of elements.

The general form of the list is A1 ,A2 , ……,AN

A1 - First element of the list


A2- 1st element of the list

N –Size of the list

If the element at position i is Ai, then its successor is Ai+1 and its predecessor is Ai-1
Various operations performed on List

1. Insert (X, 5)- Insert the element X after the position 5.

2. Delete (X) - The element X is deleted

3. Find (X) - Returns the position of X.

4. Next (i) - Returns the position of its successor element i+1.

5. Previous (i) Returns the position of its predecessor i-1.

6. Print list - Contents of the list is displayed.

7. Make empty- Makes the list empty.

Implementation of list ADT:

1. Array based Implementation

2. Linked List based implementation


Array Implementation of list:
Array is a collection of specific number of same type of data stored in consecutive memory
locations. Array is a static data structure i.e., the memory should be allocated in advance and the
size is fixed. This will waste the memory space when used space is less than the allocated space.

Insertion and Deletion operation are expensive as it requires more data movements

Find and Print list operations takes constant time.

The basic operations performed on a list of elements are


a. Creation of List.
b. Insertion of data in the List
c. Deletion of data from the List
d. Display all data‟s in the List
e. Searching for a data in the list
Declaration of Array:

#define maxsize 10
int list[maxsize], n ;
Create Operation:

Create operation is used to create the list with „ n „ number of elements .If „ n „ exceeds the
array‟s maxsize, then elements cannot be inserted into the list. Otherwise the array elements are
stored in the consecutive array locations (i.e.) list [0], list [1] and so on.
void Create ( )
{

int i;
printf("\nEnter the number of elements to be added in the list:\t"); scanf("%d",&n);
printf("\nEnter the array elements:\t");
for(i=0;i<n;i++)
scanf("%d",&list[i]);
}

If n=6, the output of creation is as follows:


list[6]

Insert Operation:
Insert operation is used to insert an element at particular position in the existing list. Inserting the
element in the last position of an array is easy. But inserting the element at a particular position
in an array is quite difficult since it involves all the subsequent elements to be shifted one
position to the right.
Routine to insert an element in the array:
void Insert( )
{

int i,data,pos;
printf("\nEnter the data to be inserted:\t"); scanf("%d",&data);
printf("\nEnter the position at which element to be inserted:\t"); scanf("%d",&pos);
if (pos==n)
printf (“Array overflow”); for(i = n-1
; i >= pos-1 ; i--)
list[i+1] = list[i]; list[pos-
1] = data; n=n+1;
Display();}
Consider an array with 5 elements [ max elements = 10 ]

10 20 30 40 50

If data 15 is to be inserted in the 2nd position then 50 has to be moved to next index position, 40
has to be moved to 50 position, 30 has to be moved to 40 position and 20 has to be moved to
30 position.

10 20 30 40 50

10 20 30 40 50

After this four data movement, 15 is inserted in the 2nd position of the array.
10 15 20 30 40 50

Deletion Operation:
Deletion is the process of removing an element from the array at any position.
Deleting an element from the end is easy. If an element is to be deleted from any particular
position ,it requires all subsequent element from that position is shifted one position towards
left.
Routine to delete an element in the array:
void Delete( )
{

int i, pos ;
printf("\nEnter the position of the data to be deleted:\t");
scanf("%d",&pos);
printf("\nThe data deleted is:\t %d", list[pos-1]);
for(i=pos-1;i<n-1;i++)
list[i]=list[i+1]; n=n-1;
Display();
}

Consider an array with 5 elements [ max elements = 10 ]

10 20 30 40 50

If data 20 is to be deleted from the array, then 30 has to be moved to data 20 position, 40 has to
be moved to data 30 position and 50 has to be moved to data 40 position.

10 20 30 40 50
After this 3 data movements, data 20 is deleted from the 2nd position of the array.

10 30 40 50

Display Operation/Traversing a list


Traversal is the process of visiting the elements in a array.
Display( ) operation is used to display all the elements stored in the list. The elements are stored
from the index 0 to n - 1. Using a for loop, the elements in the list are viewed
Routine to traverse/display elements of the array:
void display( )
{
int i;
printf("\n**********Elements in the array**********\n"); for(i=0;i<n;i++)
printf("%d\t",list[i]);
}

Search Operation:
Search( ) operation is used to determine whether a particular element is present in the list or not.
Input the search element to be checked in the list.
Routine to search an element in the array:
void Search( )
{
int search,i,count = 0;
printf("\nEnter the element to be searched:\t");
scanf("%d",&search);
for(i=0;i<n;i++)
{
if(search == list[i]) count++;
}

if(count==0)
printf("\nElement not present in the list"); else
printf("\nElement present in the list");
}
Advantages of array implementation:
1. The elements are faster to access using random access
2.Searching an element is easier

Limitation of array implementation


An array store its nodes in consecutive memory locations.
The number of elements in the array is fixed and it is not possible to change the
number of elements .
Insertion and deletion operation in array are expensive. Since insertion is performed
by pushing the entire array one position down and deletion is performed by shifting the entire
array one position up.
Applications of arrays:
Arrays are particularly used in programs that require storing large collection of similar type data
elements.

Differences between Array based and Linked based implementation


Array Linked List
Definition Array is a collection of Linked list is an ordered
elements having same data collection of elements
type with common which are connected by
name links/pointers
Access Elements can be Sequential access
accessed using
index/subscript,
random access
Memory Elements are stored in Elements are stored at
structure contiguous memory available memory space
locations
Insertion & Insertion and deletion takes Insertion and deletion are
Deletion more time in array fast and easy
Memory Memory is allocated at Memory is allocated at
Allocation compile time run time i.e dynamic
i.e static memory allocation memory allocation
Types 1D,2D,multi-dimensional SLL, DLL circular linked
list
Dependency Each elements is Each node is dependent
independent on each other as address
part contains address of
next node in the list

Linked list based implementation:

Linked Lists:

A Linked list is an ordered collection of elements. Each element in the list is referred as a node.
Each node contains two fields namely,
Data field-The data field contains the actual data of the elements to be stored in the list
Next field- The next field contains the address of the next node in the list
DATA NEXT

A Linked list node

10 40 20 30 Null

L
Advantages of Linked list:
1. Insertion and deletion of elements can be done efficiently
2. It uses dynamic memory allocation
3. Memory utilization is efficient compared to arrays
Disadvantages of linked list:
1. Linked list does not support random access
2. Memory is required to store next field
3.Searching takes time compared to arrays
Types of Linked List
1. Singly Linked List or One Way List
2. Doubly Linked List or Two-Way Linked List
3. Circular Linked List
Singly linked list or One way chain

Singly linked list can be defined as the collection of ordered set of elements. The number of
elements may vary according to need of the program. A node in the singly linked list consist of
two parts: data part and link part. Data part of the node stores actual information that is to be
represented by the node while the link part of the node stores the address of its immediate
successor.

One way chain or singly linked list can be traversed only in one direction. In other words, we can
say that each node contains only next pointer, therefore we can not traverse the list in the reverse
direction.

Consider an example where the marks obtained by the student in three subjects are stored in a
linked list as shown in the figure.

Operations on Singly Linked List

There are various operations which can be performed on singly linked list. A list of all such
operations is given below.

Node Creation
1. struct node
2. {
3. int data;
4. struct node *next;
5. };
6. struct node *head, *ptr;
7. ptr = (struct node *)malloc(sizeof(struct node *));

Insertion

The insertion into a singly linked list can be performed at different positions. Based on the
position of the new node being inserted, the insertion is categorized into the following categories.

S Operation Description
N

1 Insertion at It involves inserting any element at the front of the list.


beginning We just need to a few link adjustments to make the
new node as the head of the list.
2 Insertion at It involves insertion at the last of the linked list. The
end of the list new node can be inserted as the only node in the list or
it can be inserted as the last one. Different logics are
implemented in each scenario.

3 Insertion after It involves insertion after the specified node of the


specified node linked list. We need to skip the desired number of
nodes in order to reach the node after which the new
node will be inserted. .

Deletion and Traversing

The Deletion of a node from a singly linked list can be performed at different positions. Based on
the position of the node being deleted, the operation is categorized into the following categories.

SN Operation Description

1 Deletion at It involves deletion of a node from the


beginning beginning of the list. This is the simplest
operation among all. It just need a few
adjustments in the node pointers.

2 Deletion at It involves deleting the last node of the list.


the end of the The list can either be empty or full. Different
list logic is implemented for the different
scenarios.

3 Deletion after It involves deleting the node after the


specified node specified node in the list. we need to skip the
desired number of nodes to reach the node
after which the node will be deleted. This
requires traversing through the list.

4 Traversing In traversing, we simply visit each node of


the list at least once in order to perform some
specific operation on it, for example, printing
data part of each node present in the list.

5 Searching In searching, we match each element of the


list with the given element. If the element is
found on any of the location then location of
that element is returned otherwise null is
returned. .

Circular Linked List:- Circular Linked List is a special type of linked list in which all the
nodes are linked in continuous circle. Circular list can be singly or doubly linked list. Note
that, there are no Nulls in Circular Linked Lists. In these types of lists, elements can be added
to the back of the list and removed from the front in constant time.
Both types of circularly-linked lists benefit from the ability to traverse the full list beginning
at any given node. This avoids the necessity of storing first Node and last node, but we need
a special representation for the empty list, such as a last node variable which points to some
node in the list or is null if it's empty. This representation significantly simplifies adding and
removing nodes with a non-empty list, but empty lists are then a special case. Circular
linked lists are most useful for describing naturally circular structures, and have the
advantage of being able to traverse the list starting at any point. They also allow quick
access to the first and last records through a single pointer (the address of the last element)

Circular single linked list:

Circular linked list are one they of liner linked list. In which the link fields of last node
of the list contains the address of the first node of the list instead of contains a null
pointer.
Advantages:- Circular list are frequency used instead of ordinary linked list because in
circular list all nodes contain a valid address. The important feature of circular list is as
follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more efficient in
circular list.
Disadvantages: Without some conditions in processing it is possible to get into an
infinite Loop.
Circular Double Linked List :- These are one type of double linked list. In which the
rpt field of the last node of the list contain the address of the first node ad the left points
of the first node contains the address of the last node of the list instead of containing null
pointer.

Advantages:- circular list are frequently used instead of ordinary linked list because in
circular list all nodes contained a valid address. The important feature of circular list is
as follows.
(1) In a circular list every node is accessible from a given node.
(2) Certain operations like concatenation and splitting becomes more
efficient in circular list.
Disadvantage:-Without some conditions in processes it is possible to get in to an infant glad.
DOUBLE LINKED LIST (Or) TWO WAY LINKED LIST
In certain applications it is very desirable that list be traversed in either forward direction
or Back word direction. The property of Double Linked List implies that each node must
contain two link fields instead of one. The links are used to denote the preceding and
succeeding of the node. The link denoting the preceding of a node is called Left Link.
The link denoting succeeding of a node is called Right Link. The list contain this type of
node is called a “Double Linked List” or “Two Way List”. The Node structure in the
Double Linked List is as follows:

Lptr contains the address of the before node. Rptr contains the address of next node.
Data Contains the Linked List is as follows.

In the above diagram Last and Start are pointer variables which contains the address of
last node and starting node respectively.
Insertion in to the Double Linked List:Let list be a double linked list with successive
modes A and B as shown in the following diagram. Suppose a node N is to be inserted
into the list between the node s A and B this is shown in the following diagram.
As in the new list the right pointer of node A points to the new node N ,the
Lptr of the node „N‟ points to the node A and Rptr of node „N‟ points to the
node „B‟ and Lpts of node B points the new node „N‟
Deletion Of Double Linked List :- Let list be a linked list contains node N

between the nodes A and B as shown in the following diagram.

Support node N is to be deleted from the list diagram will appear as the above mention
double linked list. The deletion occurs as soon as the right pointer field of node A
charged, so that it points to node B and the lift point field of node B is changed. So that
it pointes to node A.
Application of Linked List

o Polynomial Manipulation representation


o Addition of long positive integers
o Representation of sparse matrices
o Addition of long positive integers
o Symbol table creation
o Mailing list
o Memory management
o Linked allocation of files
o Multiple precision arithmetic etc

Polynomial Manipulation

Polynomial manipulations are one of the most important applications of linked lists.
Polynomials are an important part of mathematics not inherently supported as a data type by
most languages. A polynomial is a collection of different terms, each comprising coefficients,
and exponents. It can be represented using a linked list. This representation makes polynomial
manipulation efficient.

While representing a polynomial using a linked list, each polynomial term represents a node in
the linked list. To get better efficiency in processing, we assume that the term of every
polynomial is stored within the linked list in the order of decreasing exponents. Also, no two
terms have the same exponent, and no term has a zero coefficient and without coefficients. The
coefficient takes a value of 1.

Each node of a linked list representing polynomial constitute three parts:

o The first part contains the value of the coefficient of the term.
o The second part contains the value of the exponent.
o The third part, LINK points to the next term (next node).

The structure of a node of a linked list that represents a polynomial is shown below:

Consider a polynomial P(x) = 7x2 + 15x3 - 2 x2 + 9. Here 7, 15, -2, and 9 are the coefficients,
and 4,3,2,0 are the exponents of the terms in the polynomial. On representing this polynomial
using a linked list, we have
Observe that the number of nodes equals the number of terms in the polynomial. So we have 4
nodes. Moreover, the terms are stored to decrease exponents in the linked list. Such
representation of polynomial using linked lists makes the operations like subtraction, addition,
multiplication, etc., on polynomial very easy.

Addition of Polynomials:

To add two polynomials, we traverse the list P and Q. We take corresponding terms of the list P
and Q and compare their exponents. If the two exponents are equal, the coefficients are added to
create a new coefficient. If the new coefficient is equal to 0, then the term is dropped, and if it is
not zero, it is inserted at the end of the new linked list containing the resulting polynomial. If
one of the exponents is larger than the other, the corresponding term is immediately placed into
the new linked list, and the term with the smaller exponent is held to be compared with the next
term from the other list. If one list ends before the other, the rest of the terms of the longer list is
inserted at the end of the new linked list containing the resulting polynomial.

Let us consider an example an example to show how the addition of two polynomials is
performed,

P(x) = 3x4 + 2x3 - 4 x2 + 7

Q (x) = 5x3 + 4 x2 - 5

These polynomials are represented using a linked list in order of decreasing exponents as
follows:

To generate a new linked list for the resulting polynomials that is formed on the addition of
given polynomials P(x) and Q(x), we perform the following steps,

1. Traverse the two lists P and Q and examine all the nodes.
2. We compare the exponents of the corresponding terms of two polynomials. The first term
of polynomials P and Q contain exponents 4 and 3, respectively. Since the exponent of the first
term of the polynomial P is greater than the other polynomial Q, the term having a larger
exponent is inserted into the new list. The new list initially looks as shown below:

We then compare the exponent of the next term of the list P with the exponents of the present
term of list Q. Since the two exponents are equal, so their coefficients are added and appended
to the new list as follows:

Then we move to the next term of P and Q lists and compare their exponents. Since exponents
of both these terms are equal and after addition of their coefficients, we get 0, so the term is
dropped, and no node is appended to the new list after this,

Moving to the next term of the two lists, P and Q, we find that the corresponding terms have the
same exponents equal to 0. We add their coefficients and append them to the new list for the
resulting polynomial as shown below:
Polynomial of Multiple Variables

We can represent a polynomial with more than one variable, i.e., it can be two or three
variables. Below is a node structure suitable for representing a polynomial with three variables
X, Y, Z using a singly linked list.

Consider a polynomial P(x, y, z) = 10x2y2z + 17 x2y z2 - 5 xy2 z+ 21y4z2 + 7. On represnting


this polynomial using linked list are:

Terms in such a polynomial are ordered accordingly to the decreasing degree in x. Those with
the same degree in x are ordered according to decreasing degree in y. Those with the same
degree in x and y are ordered according to decreasing degrees in z.

Operations on the Data Structures:


Following operations can be performed on the data structures:
1. Traversing
2. Searching
3. Inserting
4. Deleting
5. Sorting
6. Merging
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. 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

You might also like