Académique Documents
Professionnel Documents
Culture Documents
DATA STRUCTURES
UNIT I-LINEAR STRUCTURES
DATA STRUCTURES
Data structure is defined as a system of organizing related piece of information. It is the
specialized format for organizing and storing data.
Data Structures are the methods of representing a logical relationships between the
individual data element related to the solution of the given problem.
TYPES OF DATA STRUCTURES
Data Structures
Primitive data structure Non primitive data structure
Int Float Char inear DS Non inear DS
!rrays Stac" #ueue $rees %raphs Sets
Primitive Data structure - It is the basic data structure which can be directly operated by the
machine instruction.
Non Primitive Data structure - It emphasize on structuring of a group of homogenous or heterogeneous
data item.
Linear data structures - inear arrangements of elements in memory.
Non Linear Data structures - It represents hierarchical arrangements of elements.
ABSTRACT DATA TYPE
It is a user defined data type in which it performs a set of operation.
It is a mathematical abstraction how the set of operations is implemented.
&b'ects such as lists( sets and graphs along with their operations can be viewed as abstract
data type
Set !D$( we have such operations as union( intersection( size and complement.
Implementation of these operations is written once in a program( any other part of the
program that needs to perform an operation on the !D$ by calling the appropriate
function.
!"# $ DATA STRUCTURES )
UNIT I-LINEAR STRUCTURES
LIST ADT
ist is an ordered set of elements( the general form of the list is !)(!*(!+,,!n
!)First element of the list
!nast element of the list
nSize of the list
If the element at position I is !i( then it successor is !i-) and its predecessor is !i.).
%ARIOUS OPERATIONS PERFOR&ED ON LIST
). Insert/0(12 Insert element 0 after the position 1
*. Delete/023lement 0 is deleted
+. Find/024eturns the position of 0
5. Ne0t/i24eturns the position of its successor element i-)
1. Previous/i24eturns the position of its predecessor i.)
6. Print listContents of the list is displayed
7. 8a"e empty8a"es the list empty
ARRAY BASED I&PLE&ENTATION
!rray is a collection of specified number of data stored in a consecutive memory location.
If an array is dynamically allocated( an estimate of the ma0imum size of the list is re9uired.
It re9uires a high over estimate( which waste considerable space.
It allows Print ist and Find to be carried out in linear time
Find "th operation ta"es constant time.
:ut insertion and deletion are e0pensive( Inserting at position ; re9uires )
st
pushing the
entire array down one spot.
Deleting the )
st
element re9uires shifting all the elements in the list up one( worst case of
these operation is &/N2
!n average( half of the list needs to be moved for either operation so linear time is still
re9uired.
4unning time for insertion and deletion is so slow( ist size must be "nown in advance.
Simple arrays are generally not used to implement list.
!"# $ DATA STRUCTURES *
UNIT I-LINEAR STRUCTURES
a<;= a<)= a<*= a<+= a<5= a<1=
*; ); +; 5; 1; 6;
LIN'ED LIST I&PLE&ENTATION
$o avoid the cost of insertion and deletion need to ensure that the list is not stored
contiguously.
&therwise entire part of the list will need to be moved.
in"ed list consists of a series of structure( not ad'acent in memory.
3ach structure contains elements and pointer to a structure containing its successor called
as Ne0t Pointer.
ast cells Ne0t Pointer points to N>.
C Specifies that N> is ;
! Pointer variable is a variable that contains the address where some other data are stored.
If P is declared to be a pointer to a structure then value stored in P is interpreted as the
location in 88. Field of the structure can be accessed by PField Name( where field
name is the name of the field.
(ENERAL IDEA OF LL
LIN'ED LIST )IT* ACTUAL POINTER %ALUES
### +## ,- ..-
From the pictorial representations among 5 structures the ne0t pointer in the )
st
structure as a
value ?;;( indicates where the second structure is.
INSERTION IN TO A LIN'ED LIST
!"# $ DATA STRUCTURES
A A! A" A-
+
A +## A! nu// A" ..- A- ,-
UNIT I-LINEAR STRUCTURES
DELETION FRO& A LIST
$o e0ecute Print ist/2 or Find/( @ey2 we merely pass a pointer to the first element in the
list and then traverse the list by following the ne0t pointer.
Delete30ecuted in one pointer change
Insertobtaining a new cell from the system by using malloc call
TYPES OF LIN'ED LIST
Singly in"ed ist
Doubly in"ed ist
Circular in"ed ist
SIN(LY LIN'ED LIST
! Singly in"ed ist is a list in which each node /or2 structure contains only one lin" field
pointing to the ne0t node of the list. Aeader is in Position ; and a with a header representing
the list !)(!*,,!1
TYPE DECLARATION OF LIN'ED LIST0
!"# $ DATA STRUCTURES
Y
A A! A" A-
5
A A! A" A-
A A! A" A-
*eader
UNIT I-LINEAR STRUCTURES
BifndefCistCA
struct Node
typedef struct node DPtr$oNodeE
typedef Ptr$oNode istE
typredef Ptr$oNode PositionE
ist 8a"e3mpty/ist 2E
Int Is3mpty/ist 2E
Int Isast/Position P(ist 2E
PositionFind/3lement $ype F(ist 2E
Goid delete/3lement $ype F(ist 2E
Position FindPrevious/3lement $ype F(ist 2E
Goid Insert/3lement $ype F(ist (Position P2E
Goid deletelist/istl2E
Position Aeader/ist 2E
Position First/ist 2E
Position !dvance/Position P2E
3lement$ype 4etrieve/Position P2E
Bendif
Struct Node
H
3lement$ype 3lementE
Position Ne0tE
IE
ROUTINE TO C*EC' )*ET*ER T*E LIST IS E&PTY
!"# $ DATA STRUCTURES 1
int Isempty/ist 2 <4eturn $rue if is 3mpty=
H
return Ne0tJJNIE
I
Em1t2 /ist 3it4 *eader
L
*eader
UNIT I-LINEAR STRUCTURES
ROUTINE TO C*EC' )*ET*ER T*E CURRENT POSITION IS LAST
FIND ROUTINE
ROUTINE TO INSERT AN ELE&ENT IN T*E LIST
!"# $ DATA STRUCTURES
int Isast/position P(ist 2 <4eturn true if P is in ast position in ist (
Parameter is >nused=
H
return P ne0tJJN>
I
### -### "###
P Pne5t
6
Position Find/3lement type F( ist 2 <4eturn position of F in EN> if not
found=
H
Position PE
PJNe0tE
Khile/PLJN> MM P3lementLJF2
PJPNe0tE
return PE 6
I
);;; *;;; +;;;
P P7P Ne5t P
Goid Insert /3lement type F( ist ( Position P2
H
Position $mpcellE
$mpcellJmalloc/sizeof/struct node22E
If/$mpcelJJN>2
Fatal 3rror/N&ut of spaceO2E
$mpcell3lementJFE
$mpcellNe0tJPNe0tE
P.PNe0tJ$mpcellE
I
678
### -### "###
P
8## Tm1ce//
UNIT I-LINEAR STRUCTURES
ROUTINE TO FIND NE6T
DELETE ROUTINE FOR LIN'ED LIST
!"# $ DATA STRUCTURES 7
)1
AFTER INSERTIN( T*E ELE&ENT 678
### 8## -### "###
PNe5t
Position FindNe0t/int F(ist 2 <4eturns the position of its successor=
H
PJNe0tE
Khile/PNe0tLJN> MM P3lement LJF2
PJPNe0tE
return PNe0tE
I
67-#
### -### "###
L P P7PNe5t 6
UNIT I-LINEAR STRUCTURES
ROUTINE TO DELETE T*E LIST
!"# $ DATA STRUCTURES ?
Aeader *; null )1 *;;; ); )1;;
Goid delete /3lement $ype F(ist 2
H
Position P($mpcellE
PJFindPrevious/F(2
If /LIslast/P(22
H
$mpcellJPNe0tE
PNe0tJ$mpcellnet0E
free/$mpcell2E
I
I
Be9ore De/etion PNe5t 678
### 8## -###
P $mpcell
Goid Deletelist/ist 2
H
Position P( $mpcellE
PJNe0tE
Ne0tJN>
Khile/PLJN>2
H
$mpcellJPNe0tE
free/P2E
PJ$mpcellE
I
I
Ne0tJN>
);;; P *;;;
$mpcellJPNe0t
P $mpcell
free/P2
P
$mpcell(PJN>E
A9ter De/etion
### -###
UNIT I-LINEAR STRUCTURES
!"# $ DATA STRUCTURES Q
UNIT I-LINEAR STRUCTURES
DOUBLY LIN'ED LIST
! doubly in"ed ist is a list in which each structure or node has three fields namely data field(
forward lin" <Flin"=(:ac"ward in" <:lin"=. Flin" points to the successor node in the list ( where :lin"
points to the predecessor node.
Re1resentation o9 dou:/2 /in;ed /ist
Aeader
Ne0t Pointer
Node ) Node * Node +
Previous
Pointer
$he previous pointer is the :lin" which contains the address of the previous node( the ne0t pointer
is the Flin" which contains the address of the ne0t node.
P*YSICAL REPRESENTATION OF DOUBLY LIN'ED LIST
Node )
Node *
Node +
Data
Previous
Pointer
Ne0t
Pointer
!ddressJ);;
!ddressJ));
!ddressJ)*;
);
*;
+;
N>
);;
));
));
)*;
N>
STRUCTURE DECLARATION
Struct Node
H
int 3lementE
Struct Node DFlin"E
Struct Node D:lin"E
IE
!"# $ DATA STRUCTURES
BLIN' DATA POINTER FLIN'
);
# # ## -# -# # "# nu//
Aeader/);;2
UNIT I-LINEAR STRUCTURES
ROUTINE TO INSERT AN ELE&ENT IN A DOUBLY LIN'ED LIST
R
Insert an e/ement 8# a9ter t4e 1osition o9 e/ement # in a Lin;ed List
E5am1/e0 Ne3node is created 3it4 t4e e/ement 8#
)1;
Ne3nodeE/ement7678#
!"# $ DATA STRUCTURES ))
Goid Insert/int F(ist (Position P2
H
Struct Node DNewnodeE
NewnodeJmalloc/Sizeof/Struct Node22E
if/NewnodeLJN>2
H
Newnode3lementJFE
NewnodeFlin"JPFlin"E
PFlin":lin"JNewnodeE
PFlin"JNewnodeE
Newnode:lin"JPE
I
I
Insert an E/ement 8#
*eader
P );; )); )*;
)1;
FJ1;
B/in; Ne3node F/in;
1;
1;
UNIT I-LINEAR STRUCTURES
*ere t4e 1osition P is insert t4e ne3 e/ement a9ter P
##
P
Ne3nodeF/in;7PF/in;7#
PF/in;B/in;7Ne3mode78#
<#B/in;=
Ne3nodeB/in;7##
Aeader
Node ) Node + Node 5
);; )); )*;
Node *
)1;
AFTER INSERTION OF A LIS$
Aeader