Datastructurescomplete
Datastructurescomplete
INTRODUCTION
Data is the basic entityor fact that is used incalculationor manipulationprocess. There are
two types of data such as numerical and alphanumerical data. Integer and floating-point
numbers are of numericaldatatype and strings are ofalphanumeric data type. Data may be
single or a set of values and it is to be organized in a particularfashion. This organizationor
structuring of data will have profound impact on the efficiency ofthe program.
DATA:
Dataisvalueorsetofvalueswhichdoesnotgive anymeaning.It isgenerallyaraw fact. For
example:
1.34 3.Chintan
2.13/05/2008 4.12,34,43,21
ENTITY
Anentityisathingorobjectintherealworldthatisdistinguishablefromallother objects.
The entity has a set of properties or attributes and the values of some sets of these attributes
may uniquely identify an entity.
Anentitysetisacollectionofentities.
Example:
Entity Student
INFORMATION
It canbe defined as meaningfuldataorprocessed data. Whenthe raw facts are processed, we get
a related piece of information as its output/result.
Example:
Data(01/01/1980)becomesinformationifentityRamisrelatedtoDateofbirthattribute
(01/01/1980) as follows:
DOBofstudentRamis 01/01/1980
DATATYPE
A data type is a term which refers to the kind of data that may appear in computation. Every
programming language has its own set of built-in data types.
Example:
Data Datatype
34 Numeric
Chintech String
21,43,56 Arrayofintegers
12/05/2008 Date
Definition:
Data Structure is a specialized format for storing data so that the data’s can be organized in
an efficient way.
Or
“Thedatastructureisthelogicalormathematicalmodelofparticularorganizationof data.”.
Or
A data structure is a mathematical or logical way of organizing data in the memory that
consider not only the items stored but also the relationship to each other and also it is
characterized by accessing functions.
DataStructure=Organizeddata+AllowedOperations.
OBJECTIVESOFDATASTRUCTURE
To identify and create useful mathematical entities and operations to determine what classes
of problems can be solved by using these entities and operations.
Todeterminetherepresentationoftheseabstractentitiesandtoimplementtheabstract operations on
these concrete representation.
NEEDOFADATASTRUCTURE
To understand the relationship ofone data elements with the other and organize it within the
memory.
Adatastructureshelpstoanalyzethedata,storeitandorganizeitinalogicalor mathematical manner.
Datastructuresallowustoachieveanimportantgoal:componentreuse.
CLASSIFICATIONOFDATASTRUCTURE
Basedonhowthedataitemsareoperated,itwillclassifyintotwo broad categories.
PrimitiveDataStructure
Non-PrimitiveDataStructure
Non-Primitive Data Structure: These are more sophisticated DS and are derived from primitive
DS. Here data items are not operated closest to machine level instruction. It
emphasizeonstructuringofagroupofhomogeneous(sametype) orheterogeneous(different type)
data items.
LinearDataStructure:Inwhichthedataitemsarestored insequenceorder.
Example:Arrays,LinkedLists,StacksandQueues
DATASTRUCTURE OPERATIONS:
Thevariousoperationsthat canbeperformedondifferent datastructuresaredescribed below:
1. Creating–Adatastructure createdfromdata.
2. Traversal–Processingeachelementinthelist
3. Search–Finding thelocationofgivenelement.
4. Insertion –Addinganewelementtothelist.
5. Deletion–Removinganelementfromthelist.
6. Sorting–Arrangingtherecordseitherinascendingordescendingorder.
7. Merging–Combiningtwolistsintoasinglelist.
8. Modifying–thevaluesofDScanbemodifiedby replacing oldvalueswithnewones.
9. Copying–recordsofonefilecanbe copied toanotherfile.
10. Concatenating –Recordsofafile are appendedat the endofanotherfile.
11. Splitting–Recordsofbigfilecanbesplittingintosmallerfiles.
ABSTRACTDATATYPES
Abstract data type (ADT) is a specification of a set of data and the set of operations that can be
performed on the data.
Itisalogical descriptionofhowweviewthedataandtheoperationsthatareallowed without
knowing how they will be implemented.
This meansthat weareconcernedonlywithwhat thedata isrepresentingand not withhow it will
eventually be constructed.
Figshowsapictureofwhatanabstractdatatypeisandhowit operates.
Theuserinteractswiththeinterfacebyusingthe operations.
Theuserisnotconcerned withthedetailsoftheimplementation.
Theimplementationofanabstract datatype,oftenreferred toasadata structure
An ADT is a data declaration packaged together with the operations that are meaningful
onthe data type.
1. DeclarationofData
2. DeclarationofOperations
Examples: Objects such as lists, sets and graphs with their operations can be called as ADT. For
the SET ADT, we might have operations such as union, intersection, and complementetc.
UsesofADT:
1. Ithelpstoefficientlydevelopwelldesignedprogram
2. Facilitatesthedecompositionofthecomplextaskofdevelopingasoftwaresystemintoa number of
simpler subtasks
3. Helpsto reducethenumberofthingstheprogrammershasto keep inmind atanytime.
4. Breaking down a complex task into a number of earlier subtasks also simplifies testing
anddebugging.
ALGORITHM:
Analgorithm isa methodofrepresentingthestepbysteplogicalprocedureforsolvinga problem. It is a
tool for finding the logic of a problem.
Inadditioneveryalgorithmmustsatisfythefollowing criteria:
ALGORITHM COMPLEXITY:
After designing an algorithm, we have to be checking its correctness. This is done by
analyzing the algorithm. The algorithm can be analyzed by tracing all step-by-step
instructions,readingthealgorithmfor logicalcorrectness,andtestingit onsomedatausing
mathematical techniques to prove it correct.
There maybe morethanonealgorithmtosolveaproblem.Thechoiceof aparticular
algorithm depends on its complexity.
Complexityreferstotherateat whichtherequiredstorageorconsumedtimegrowsasa
function of the problem size.
Itistheperformanceevaluationoranalysis/ measurementofanalgorithmisobtained by
totaling the number of occurrences of each operation when running the algorithm.
Twoimportant waysto characterizetheeffectivenessofanalgorithmare itsspace
complexity and time complexity.
SpaceComplexity
TimeComplexity
SPACECOMPLEXITY:
Spacecomplexityofanalgorithmorprogramistheamountofmemoryitneedsto runto
completion.
Wecanmeasurethespacebyfindingoutthathowmuchmemorywillbeconsumedbythe
instructions and by the variables used.
Example:
Supposewewantto addtwo integernumbers.Tosolvethisproblemwehave following
twoalgorithms:
Algorithm1: Algorithm2:
Step1-InputA. Step1-InputA.
Step2-InputB. Step2-InputB.
Step3-Set C:=A+ B. Step3-Write:‘Sumis‘,A+B.
Step4-Write:‘Sumis‘,C. Step
4- Exit.Step 5- Exit.
Both algorithms will produce the same result. But first takes 6 bytes and second takes 4
bytes(2bytesforeachinteger).Andfirsthasmoreinstructionsthanthesecondone.Sowe will
choose the second one as it takes less space than the first one. (Space Complexity).
Suppose 1 second is required to execute one instruction. So the first algorithm will take 4
secondsandthesecondalgorithm willtake3secondsforexecution.Sowewillchoosethe second
one as it will take less time. (Time Complexity).
TimeComplexity
➢ Thetimecomplexityofaprogram /algorithm istheamount ofcomputertimethatisneed to run
to completion.
➢ Butthecalculationofexactamountoftimerequiredbythealgorithm isverydifficult.Sowe can
estimate the time and to estimate the time we use some asymptotic notation, which are
described below. Let the no. of steps of an algorithm is measured by the function f(n).
1. Big‘O’notation
2. Omega(Ω)notation
Thefunctionf(n)=Ω(g(n))ifandonlyifthereexistthe+veconstCand nosuch thatf(n)≥C
* g(n)foralln≥ no
3. Theta(θ)notation:
Thefunctionf(n)=θ(g(n))if andonlyifthereexist3+veconst‘C1’,‘C2’andnosuchthatC1
* g(n)≤f(n) ≤C2*g(n)
SpaceComplexity
Thespacecomplexityistheprogramthattheamountofmemorythatis neededtorunto
completion.The spacecomplexityneedbya programhavetwodifferentComponents.
TIME-SPACETRADE-OFF:
Declarationof strings:
Stringsare declared in Cin similarmanneras arrays. Only difference isthat,stringsare ofchar type.
Syntax:
charstr_name[str_size];
Example:
chars[5];
Initializationofstrings
charc[]="abcd";
Stringhandlingfunctions
Manylibraryfunctionaredefinedunderheaderfile<string.h>toperformdifferenttasks.
Diffrentuserdefinedfunctiosare:
Strlen()
Strcpy()
Strcmp()
Strcat()
Strlen():
Thestrlen( )functionisusedtocalculatethelengthof thestring.
Itmeansthatitcountsthetotalnumber ofcharacterspresentinthestringwhich
includes alphabets, numbers, and all special characters includingblank spaces.
Example:
charstr[]="LearnCOnline"; int
strLength;
strLength= strlen(str); //strLengthcontainsthelengthofthestringi.e.14
Strcpy()
• strcpyfunctioncopies a stringfroma sourcelocationtoadestinationlocationand
provides a null character to terminate the string.
Syntax:
strcpy(Destination_String,Source_String);
Example:
char*Destination_String;
char*Source_String="LearnCOnline";
strcpy(Destination_String,Source_String);
printf("%s", Destination_String);
Output:
LearnC Online
Strcmp()
• Strcmp()inC programminglanguageisusedtocomparetwostrings.
• Ifboththestrings areequalthenit gives theresult aszerobutifnot thenit givesthe
numeric difference between the first non matching characters in the strings .
Syntax:
intstrcmp(string1,string2);
Example:
char*string1="LearnCOnline"; char
*string2 = "Learn C Online";int ret;
ret=strcmp(string1, string2);
printf("%d",ret);
Output:
0
Strcat()
ordered–Alltheelementsarestoredonebyone in acontiguousmemorylocation.
LINEARARRAY
Ifone subscript is required to refer allthe elements in an arraythen this is called Linear arrayor
one-dimensional array.
Letaisthenameofanintegerarray.
Itcontainsasequenceofmemorylocation.
(a[i]) = b + i x w i=indexno.addressof
a[3] = b + i x w
=b+3x2=b+6
OperationonArray
1. TRAVERSAL
Voidtraverse(inta[],intn)
{
inti;
for(i=0;i<n;i++)
Printf(“%d”,a[i]);
}
2. INSERTION
3. DELETION
Deleting an element at the end of the array can be done easily by only decreasing the array size
by 1.
But deleting anelement at the middle ofthe arrayrequire that eachsubsequent element fromthe
position where to be deleted should be moved to fill up the array.
} n--;
return(0);
}
MemoryRepresentationof2DArray
- Thearrayhavingtwosubscriptiscalledas2Darray.
- In2Darraytheelementsarestoredincontiguousmemorylocationasinsingle dimensional
array.
- Thereare2waysofstoring anymatrix in memory.
1. Row-majororder
2. Column-majororder
Row-majorOrder
Inrow-majorordertherowelementsarefocused firstthat meanselementsofmatrixare stored on
a row-by-row basis.
LogicalRepresentationofarraya[3][3]
Rowmajor orderrepresentationofa[3][3]
StorageRepresentation inRow-majororder
Column-majorOrder
In column major order the column elements are focused first that means elements of the matrix
are stored in column-by-column basis.
Ex:Columnmajororderrepresentationofa[3][3]
StorageRepresentationofmatrixinColumn-majorOrder
AddressCalculationofMatrixinMemory
Row-majororder
Supposea[u1][u2]isa2Darrayu1=no.ofrow u2 = no.
of column
Addressofa[i][j]=b+(ixu2+ j)xw
Column-major order
Addressofa[i][j]= b+ (jxu1+i)xw
Pointers
ThepointerinClanguageisavariablewhichstorestheaddressofanothervariable.This variable can be
of type int, char, array, function, or any other pointer.
Syntax:datatype*var_name;
Example:
1. int*a;//pointerto int
2. char*c;//pointertochar
PointerExample
Sparsematrices
Matrix with relatively a high proportion of zero entries are called sparse matrix. In other words,
thesparse matrixcanbedefinedasthe matrixthathasagreaternumber ofzero elementsthanthe non-
zero elements.
Benefitsofusing thesparsematrix
i) Storage -We know that a sparse matrix contains lesser non-zero elements than zero, so less
memory can be used to store elements. It evaluates only the non-zero elements.
ii) Computing time: In the case of searching in sparse matrix, we need to traverse only the non-
zero elements rather than traversing all the sparse matrix elements. It saves computing time by
logically designing a data structure traversing non-zero elements.
Representationofsparsematrix
Therearetwowaystorepresentthesparsematrixthatarelistedasfollows -
o Arrayrepresentation
o Linkedlist representation
Array representationofthesparsematrix
Representing a sparse matrix by a 2D array leads to the wastage of lots of memory. This is
becausezeroesinthematrixareofnouse,sostoringzeroeswithnon-zeroelementsiswastage
of memory. To avoid such wastage, we can store only non-zero elements. If we store only non-
zero elements, it reduces the traversal time and the storage space.
2Darrayisused torepresentasparsematrix inwhichtherearethreerowsnamed as
LinkedListrepresentationofthesparse matrix
Ina linked list representation, the linked list data structure isused to represent the sparse matrix.
The advantage of using a linked list to represent the sparse matrix is that the complexity of
inserting or deleting a node in a linked list is lesser than the array.
Thatmeans the elements are removedfrom a stack in the reverse order of that in which
they were inserted into the stack.
StackiscalledLIFO(Last-in-first-Out)Str.i.e. theitemjustinsertedisdeletedfirst.
Thereare2basic operationsassociatedwithstack
1. Push:-Thisoperationisusedtoinsertanelementintostack.
2. Pop:-Thisoperation isusedto deleteanelement fromstack
WorkingofStackDataStructure
Conditionalsoarise:
1. Overflow:-Whenastackisfullandweareattemptingapushoperation,overflow condition
arises.
2. Underflow:-Whenastackisempty,andweareattemptingapopoperationthen underflow
condition arises.
RepresentationofStackinmemory
Astackmayberepresentedinthememory intwoways:
1. Usingonedimensionalarrayi.e.ArrayrepresentationofStack.
2. Usingsinglelinkedlisti.e.Linkedlist representationofstack.
ArrayRepresentation ofStack :
Toimplement astackinmemory,weneedapointervariablecalledTOPthat holdthe indexof the top
element ofthe stack, a linear arrayto hold the elements ofthe stack and a variable MAXSTK
which contain the size of the stack.
MAXSTK=5
SinglelinkedlistissufficienttorepresentanyStack.
Herethe‘info’fieldfortheitemand‘next’
field is used to print the next item.
INSERTIONINSTACK(PUSHOPERATION)
PUSH(STACK,TOP,MAXSTK, ITEM)
Thisprocedurepushesanitemintostack.
1. IfTOP =0theprintUnderflowand Return.
2. SetITEM=STACK[TOP](AssignsTOPelementtoITEM)
3. SetTOP=TOP-1(DecreasesTOP by1)
4. Return
( WhereStack=Stackisalistoflinear structure
TOP=pointervariablewhichcontainnthe locationofTopelementofthestack MAXSTK=
variable which contain size of the stack
ITEM=Itemtobeinserted)
DELETIONINSTACK(POPOPERATION)
POP(STACK,TOP,ITEM)
ThisproceduredeletesthetopelementofSTACKandassignsittothevariable ITEM.
1. IfTOP =0theprintUnderflowand Return.
2. SetITEM=STACK[TOP](AssignsTOPelementtoITEM)
3. SetTOP=TOP-1(DecreasesTOP by1)
4. Return
DataStructure 20 SwagatikaDalai
StackPushandPopOperations
ARITHMETICEXPRESSION
Thereare3notationtorepresentanarithmetic expression.
1. Infix notation
2. Prefixnotation
3. Postfixnotation
1. INFIXNOTATION
• Theconventionalwayofwritinganexpressioniscalledasinfix.
Ex:A+B, C+D, E*F, G/M etc.
• Herethenotation is
<Operand><Operator><Operand>
• Thisiscalledinfixbecausetheoperatorscomeinbetweentheoperands.
2. PREFIXNOTATION
• Thisnotationisalsoknownas “POLISH NOTATION”
• Herethenotation is
<Operator><Operand><Operand>
• Ex:+AB,-CD,*EF, /GH
21
3. POSTFIXNOTATION
• Thisnotationiscalledaspostfixorsuffixnotationwhereoperatorissuffixed by
operand.
• Herethenotation is
<Operand><Operand><Operator>
• Ex:AB+, CD-,EF*,GH/
• Thisnotationisalsoknownas“REVERSEPOLISH NOTATION.”
CONVERSIONFROM INFIXTOPOSTFIXEXPRESSION
Algorithm:
POLISH(Q,P)
Q is an arithmetic expression written in infix notation. This algorithm finds the equivalent
postfix expression P.
1. Push“(” ontostackand add“)”tothe endofQ.
2. ScanQ fromleft toright andrepeat steps3to6foreachelementofQuntiltheSTACKis empty.
3. Ifanoperandisencountered,addit top.
4. Ifaleftparenthesis isencountered, pushitontostack.
5. IfanoperatorXisencountered,then:
a) Repeatedly POP from STACK and add to P each operator (on the top of the
STACK) which has the same precedence as or higher precedence than the
operator X .
b) Addthe operatorXtoSTACK.
6. Ifarightparenthesisisencounteredthen:
a) RepeatedlyPOPfromSTACKandaddtoPeachoperator(onthetopofthe STACK) until
a left parenthesis is encountered.
b) Removetheleftparenthesis.
[End of ifstr.]
[EndofStep-2Loop]
7. Exit
22
Ex:A+(B*C–(D/E^F)*G)*H)
EquivalentPostfixExpression:ABC*DEF^/G*-H*+
EVALUATIONOFPOSTFIXEXPRESSION
Algorithm:
Thisalgorithm findsthevalueofanarithmeticexpressionP writteninPostfix notation.
1. Addarightparenthesis“) ”attheendofP.
2. Scan P from left to rightand repeat steps 3 and 4 for each element of P until the “ ) ” is
encountered.
3. Ifanoperandisencountered,putitonSTACK.
4. IfanoperatorXisencounteredthen:
a) RemovethetwotopelementofSTACK,whereAistopelementandBisthe next-to-top
element.
23
b) Evaluate BXA.
c) Placetheresultof(b)onSTACK.
[End of if str.]
5. Set value equaltothe topelementonSTACK.
6. Exit
SymbolScanned STACK
5 5
6 5,6
2 5,6,2
+ 5,8
* 40
12 40,12
4 40,12,4
/ 40,3
- 37(Result)
)
APPLICATIONSOFSTACK:
Recursionprocessusesstack for itsimplementation.
Quicksortusesstackfor sortingthe elements.
EvaluationofantitheticexpressioncanbesonebyusingSTACK.
Conversionfrominfixtopostfixexpression
Evaluationofpostfixexpression
Conversionfrominfixtoprefixexpression
Evaluationofprefixexpression.
Backtracking
Keeptrackofpost-visited(historyofaweb-browsing)
IMPLEMENTATIONOFRECURSION
24
I. Thefunctionshouldhaveabase condition.
II. Thefunctionshouldcomeclosertowardsthebase condition.
Thefollowingisoneoftheexampleofrecursivefunctionwhichisdescribedbelow.
Factorialofano.usingRecursion
X 2 X 3 X.....................................X (n-) X n
Physicallyprovedn!= nX (n-1)!
Thefactorialfunctioncanbedefinedasfollows.
I. Ifn=0thenn!=1
II. Ifn>0thenn!=n.(n-1)!
Thefactorialalgorithmisgivenbelowfactorial(fact,n)
Thisprocedurecalculatesn!and returnsthevalueinthevariablefact.
1. Ifn=0thenfact=1and Return.
2. Callfactorial(fact,n-1)
3. Setfact= fact*n
4. Return
25
Ex:Calculatethefactorialof4. 4 ! = 4
x3!
3!=3x2!
2!=2x1!
1!=1x0!
0!=1
1!=1x1=1
2!=2x1=2
3!=3x2=6
4!=4x6=24
26
QUEUE
• Queue is a linear data structure or sequential data structure where insertion take place at
one end and deletion take place at the other end.
• TheinsertionendofQueueiscalledrearendanddeletionendiscalledfrontend.
• Queue is based on (FIFO) First in First Out Concept that means the node i.e. added firstis
processed first.
• HereEnqueueisInsertOperation.
• DequeueisnothingbutDeleteOperation.
Representation OfQueueInMemory
INSERTIONINQUEUE(Enqueue)
Algorithm:
Insert(Q,ITEM,Front,Rear)
ThisprocedureinsertITEMinqueueQ.
1. IfFront=1andRear=Max
Print ‘Overflow’ and Exit.
2. Iffront=NULLthan
Front = 1
Rear=1
else
27
Rear =Rear+1
3. Q[Rear]= ITEM
4. Exit
DELETIONINQUEUE(Dequeue)
Algorithm:
Delete(Q,ITEM, FRONT, REAR)
Thisprocedureremoveelementfromqueue Q.
1. IfFront=Rear=NULL
Print ‘Underflow’ and Exit.
2. ITEM=Q(FRONT)
3. IfFront=Rear
Front = NULLRear
= NULL
else
Front =Front+1
4. Exit
28
EnqueueandDequeue Operations
29
CIRCULARQUEUE
Letwehaveaqueuethatcontain‘n’elementsinwhichQ[1] comesafterQ[n].
Whenthistechnicisusedtoconstructaqueuethenthequeueiscalled circular queue.
InCircular queuewhenREARis‘n’andanyelementisinsertedthenREARwillsetas1.
Similarlywhen the front is n and anyelement is deleted then front will
be 1.
CircularQueuerepresentation
INSERTIONALGORITHMOFCIRCULARQUEUE
30
DELETIONALGORITHMOFCIRCULARQUEUE
Delete(Q, N,ITEM,FRONT,REAR)
Thisproceduredeleteanelementfromacircular queue.
1. IfFront=NULLthenwriteUnderflowand Return.
2. SetITEM=Q[FRONT]
3. IfFront= Rear
ThenFront=NULLandRear=NULL Else if
Front = N then
SetFront=1
else
Front= Front+1
[Endofif str.]
4. Return
PRIORITYQUEUE
Ifisatypeofqueue inwhicheachelement has beenassignedaprioritysuchthatthe order in
which the elements are processed according to the elements are processed according
to the following rule
I. Thiselement ofhighpriorityisprocessedfirst.
II. Theelement havingsamepriorityisprocessedaccordingtotheorderinwhich they
are inserted.
Thelowerrankedno.enjoyhighpriority.
RepresentationofPriorityQueueinmemory
PriorityQueuecanberepresentedinmemoryin2 ways.
I. Onewaylistrepresentation
II. Arrayrepresentation
31
Onewaylistrepresentation
Eachnodeinthelistcontain3fields.i.e.INFO,PRIORITY, LINK
ArrayRepresentation
Herethe priorityqueueisrepresented bymeansof 2D array.
Whererowvaluerepresentstheprioritynumberandeachrowmaintainedasacircularqueue. Each row has
its FRONT and REAR value to represent the starting and ending of row.
FRONT REAR
2 4
1 2
3 1
TypesOfPriority Queue
1. AscendingPriorityQueue:-
Itisthecollectionofitems intowhichitemscanbe insertedarbitrarilyand fromwhichonlythe smallest item
can be removed.
2. DescendingPriorityQueue:-
ThisissimilartoascendingpriorityQueuebutitallowsdeletionofonlythelargestitem.
32
Chapter-5
LINKEDLIST
LinkedListisacollectionofdataelementscalledasnodes. The
node has 2 parts
Infoisthedatapart
Nexti.e. theaddresspartthatmeansitpointstothenext node.
They are designed such that info[K] contains the Kth element and Link[K] contains the next pointer
field i.e. the address of the Kth element in the list.
TheendoftheListisindicatedbyNULLpointeri.e.thepointerfieldofthelastelementwillbe NULLorZero
.
Start=4
info[4]=N Link[4]=3
info[3]=T Link[3]=6
info[6]=E Link[6]=2
info [2] = F Link[2]=0i.e.thenullvalue So
the list has ended .
RepresentationofanodeinaLinkedList Struct
Link
{
intdata;
StructLink *add;
};
SINGLELINKEDLIST
ASingleLinkedListisalsocalledasone-waylist.Itisalinearcollectionofdataelementscallednodes, where the linear
order is given by means of pointers.
Eachnodeisdividedinto2 parts.
I. Information
II. Pointertonextnode.
OPERATIONONSINGLELINKEDLIST
1. Traversal
Algorithm:
Display(Start)This algorithmtraversethe list startingfromthe1 st nodetothe endof thelist. Step-
1 : “Start” holds the address of the first node.
Step-2 : Set Ptr = Start [Initializes pointer Ptr]
Step-3:RepeatStep4to5,WhilePtr ≠NULL
Step-4:Processinfo[Ptr][applyProcesstoinfo(Ptr)]
Step-5:Set Ptr=next[Ptr][movePrint tonext node]
[End of loop]
Step-6:Exit
2. Insertion
The insertion operation is used to add an element in an existing Linked List. Thereis various positions where
node can be inserted.
Insertatthebeginning
Insertatend
Insertatspecificlocation.
InsertAtTheBeginning
Supposethenewmodewhoseinformationfieldcontains20isinsertedasthefirst node.
Algorithm:
This algorithm is used to insert a node at the beginning of the Linked List. Start holds the address of the first
node.
Step-1:Createanewnodenamedas P.
Step-4:Setnext[P]=Start(newnodenowpointstooriginalfirstnode) Step-5 :
Set Start = P
Step-6:Exit
InsertAtTheEnd
Toinsert anodeat theendofthelist, weneedtotraversetheList andadvancethepointer untilthelast node is
reached.
Supposethenewnodewhoseinformationfieldcontains77isinsertedatthelast node.
Algorithm:
This algorithmis usedtoinsert anodeat theendofthelinked list. ‘Start’holds theaddress ofthefirst node. Step-
1 : Create a new node named as P.
Step-4:Setnext[P]=NULL
Step-5 : Set Ptr = Start
Step-6:RepeatStep-7whilePtr≠NULL
Step-7 : Set temp = Ptr
Ptr=next[Ptr](Endofstep-6loop)
Step-8 : Set next [temp] = P
Step-9:Exit
InsertAtAnySpecificLocation
To insert a new node at any specific location we scan the List fromthe beginning and move up to the desired
node where we want to insert a new node.
Inthebelowfig.Whoseinformationfieldcontain88 isinsertedat3 rdlocation.
Algorithm:
‘Start’holdstheaddress ofthe1stnode.
Step-1 : Set Ptr = Start
Step-2:Createanewnodenamedas P.
3. Deletion
Thedeletionoperationsusedto deleteanelement fromasingle linked list.Therearevarious position
where node can be deleted.
Deletethe1stNode
Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set temp = Start
Step-2:IfStart=NULLthenwrite‘UNDERFLOW’&Exit. Step-
3 : Set Start = next[Start]
Step-4:Freethespaceassociatedwithtemp.
Step-5 : Exit
Deletethelastnode
Algorithm:
Startholdstheaddressofthe1st node.
Step-1:SetPtr=Start Step-2: Settemp=Start
Step-3: IfPtr=NULLthenwrite‘UNDERFLOW’&Exit.Step-4:RepeatStep-5 and 6
While next[Ptr] ≠ NULL Step-5 : Set temp = Ptr
Step-6:SetPtr=next[Ptr](Endofstep4loop) Step-
7 : Set next[temp] = NULL
Step-8:FreethespaceassociatedwithPtr.
Step-9 : Exit
Deletethenodeatanyspecificlocation
Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set Ptr = Start
Step-2:Settemp=Start
Step-3:IfPtr=NULLthenwrite‘UNDERFLOW’andExit. Step-
4 : Set i = 1
Step-5:ReadLoc
Step-6:RepeatStep-7to9whilePtr ≠NULLandi<Loc Step-7
: Set temp = Ptr
Step-8:SetPtr= next[Ptr]
Step-9 : Set i = i+1
(EndofStep6loop)
Step-10:Setnext[temp]= next[Ptr]
Step-11:FreethespaceassociatedwithPtr.
Step-12 : Exit
4. SEARCHING
Searchingmeansfindinganelementfromagivenlist.
Algorithm:
Startholdstheaddressofthe1stnode.
Step-1 : Set Ptr = Start
Step-2 : Set Loc = 1
Step-3:Readelement
Step-4:Repeat Step-5and7WhilePtr≠NULL
Step-5:If element=info[Ptr]thenWrite‘Elementfound atposition’, LocandExit. Step-
6 : Set Loc = Loc+1
Step-7:SetPtr=next[Ptr] (End
of step 4 loop)
Step-8:Write‘Elementnotfound’
Step-9 : Exit
Headerlinkedlist
HeaderLinkedListisa modified versionofSinglyLinkedList.
In Header linked list, we have a special node, the Header Nodepresent at the beginning of the
linked list.
The Header Node is an extra node at the front of the list storing meaningful information aboutthe
list.
It does not represent any items of the list like other nodes rather than the information present in
the Header node is global for all nodes such as Count of Nodes in a List,Maximumamong all
Items, Minimum value among all Items etc.
ThisgivesusefulinformationabouttheLinkedlist.
Aheaderlinkedlistcanbedividedintotwotypes:
i) Groundedheaderlinked listthatstoresNULLinthelastnode’snextfield.
When we delete a particular note froman existing linked list or delete the linked list the space
occupied byit must begivenbacktothefreepool.Sothatthe memorycanbetheusedbysome other
program that needs memory space.
Tothefreepoolisdone.
Application:
The followingaretheapplicationsoftrees:
Storingnaturallyhierarchicaldata: Treesareusedtostorethedatainthehierarchical
structure. For example, the file system.
Organizedata: Itisusedtoorganizedataforefficientinsertion,deletionand searching.
Trie:It isaspecialkindoftreethatisusedtostorethedictionary.It isa fast andefficient way for
dynamic spell checking.
Heap:Itisalsoatreedatastructureimplementedusingarrays.It isusedtoimplement priority
queues.
B-TreeandB+Tree: B-TreeandB+Treearethetreedatastructuresusedto implement
indexing in databases.
Routingtable: Thetreedatastructureisalso usedtostorethedatainroutingtables in the
routers.
BasicTreeTerminologies
1. Node
Anodeisanentitythatcontainsakeyorvalue .
The last nodesofeachpatharecalled leafnodesorexternalnodesthat donotcontaina link/pointer to
child nodes.
Thenodehavingatleastachildnodeiscalledaninternalnode.
2. Root Node: The topmost node of a tree or the node which does not have anyparent node is
calledtherootnode. {1} istherootnodeofthetree. Anon-emptytreemust containexactlyone root
node .
3. ParentNode: Thenodewhichisapredecessorofanodeiscalledtheparent nodeofthat node.
{2} is the parent node of {6, 7}.
4. ChildNode:Thenodewhichisthe immediatesuccessorofanodeiscalledthechild nodeof that
node. Examples: {6, 7} are the child nodes of {2}.
5. Degreeof a Node: The totalcount of subtrees attached to that node is called the degree of the
node. The degree ofa leaf node must be 0. The degree ofa tree is the degree of its root. The
degreeofthenode{3} is3. LeafNodeorExternalNode:Thenodeswhichdo nothaveanychild nodes
are called leaf nodes. {6, 14, 8, 9, 15, 16, 4,11, 12, 17, 18, 19} are the leaf nodes ofthe tree
6. Sibling:Childrenofthesameparentnodearecalledsiblings.{8,9,10}arecalled siblings.
7. Depthofanode:Thecountofedgesfromtheroottothe node.Depthofnode{14}is 3.
2. CompleteBinarytree
1. SequentialorLinearRepresentationofBinaryTree.
Let us consider that we have a tree T. let our treeT is a binary tree that us complete binary tree.
Then there is an efficient way of representing T in the memory called the sequential
representation or array representation ofT. This representation uses only a linear array TREE as
follows:
1. TherootNofTisstored inTREE[1].
2. If anode occupiesTREE[k]thenitsleftchildis storedinTREE[2*k] andits right child is
stored into TREE [2 * k + 1].
Itssequentialrepresentation isasfollow:
2. LinkedRepresentationofBinaryTree
ConsideraBinaryTree T.Twillbe maintained inmemorybymeansofa linked list representation
which uses three parallel arrays; INFO, LEFT, and RIGHT pointer
variableROOT as follows. InBinaryTreeeachnode NofT willcorrespondto alocationksuch that
1. LEFT[k]containsthelocationoftheleftchildofnodeN.
2. INFO[k]containsthedataat thenodeN.
3. RIGHT[k]containsthelocationofright childofnode N.
Representation of a node:
Inthis
representationofbinarytreerootwillcontainthelocationoftherootRofT.Ifanyoneof the
subtree isempty, thenthecorrespondingpointerwillcontainthenullvalue ifthetree Titselfis empty,
the ROOT will contain the null value.
Example
Consider thebinarytreeTinthe figure. Aschematicdiagramofthelinked list representationof T
appears in the following figure. Observe that each node is pictured with its three fields, and that
the empty subtree is pictured by using x for null entries.
BinaryTree
BinaryTree Traversal
1. First,visitallthenodesintheleftsubtree
2. Thentherootnode
3. Visitallthenodesintherightsubtree
Left→ Root→Right
Application-
Inordertraversalisusedtoget infixexpressionofanexpressiontree.
2. PreorderTraversal-
Thisisalsoknownasdepthfistorder. This
traversal is referred as NLR.
Algorithm-
1. Visittheroot
2. Traversetheleftsubtreei.e.callPreorder(leftsubtree)
3. Traversetheright subtreei.e.callPreorder(right subtree)
Applications-
Preordertraversalisusedto getprefixexpressionofanexpressiontree.Preordertraversalisused to create a
copy of the tree.
3. PostorderTraversal-
ThistraversalisreferredasLRN.
Algorithm-
1. Traversetheleftsubtreei.e. callPostorder(leftsubtree)
2. Traversetherightsubtreei.e.callPostorder(rightsubtree)
3. Visit the root
Left→Right→Root
Applications-
Postordertraversalisusedto getpostfixexpressionofanexpressiontree.
Postordertraversalisused todelete thetree.
Thisisbecauseitdeletesthechildrenfirstandthenitdeletestheparent.
BinarySearchtree
AbinarytreeTistermedasBinarySearchTreeifeachnodenofTsatisfiesthefollowing property.
Thevalueofn isgreaterthanthevalueofallnodesin itsleftsubtree.
Thevalueofn islessthanthevalueofall nodesin itsrightsubtree.
AdvantagesofBinarysearchtree
o Searchinganelement intheBinarysearchtreeiseasyaswealways haveahint that which
subtree has the desired element.
o Ascomparedtoarrayand linked lists, insertionanddeletionoperationsare fasterinBST.
Example of creating a binary search tree
Supposethe dataelements are-45,15,79,90,10,55,12,20,50
o First,wehavetoinsert45intothetreeastheroot ofthetree.
o Then,readthenext element;ifit issmallerthantheroot node,insert it as therootofthe left
subtree, and move to the next element.
o Otherwise, iftheelement islargerthantheroot node,theninsert it astherootoftheright
subtree.
Now,let'sseetheprocessofcreatingtheBinarysearchtreeusingthegivendataelement. The process of
creating the BST is shown below -
Step1-Insert 45.
Step 2-Insert15.
As15issmallerthan45,soinsertitastherootnodeoftheleftsubtree
Step3-Insert 79.
As79 isgreaterthan45,so insertitastherootnodeoftherightsubtree.
Step4-Insert 90.
90isgreaterthan45and79,soitwillbeinsertedastherightsubtreeof79.
Step5-Insert 10.
10issmallerthan45and15,soitwillbeinsertedasaleftsubtreeof15.
Step6-Insert 55.
Step7-Insert 12.
12issmaller than45and15butgreaterthan10,soitwillbeinsertedastherightsubtreeof10.
Step8-Insert 20.
Now,thecreationofbinarysearchtreeiscompleted.
SearchinginBinarysearchtree
Searching means to find or locate a specific element or nodeina data structure. InBinarysearch
tree, searchinga nodeiseasybecauseelements inBST arestoredinaspecificorder.Thestepsof
searching a node in Binary Search tree are listed as follows –
1. First, comparetheelementtobesearchedwiththerootelementofthetree.
2. Ifrootismatchedwiththetargetelement,thenreturnthenode'slocation.
3. Ifit isnotmatched,thencheckwhethertheitemislessthantherootelement, ifit is smaller
than the root element, then move to the left subtree.
4. Ifitis largerthantherootelement,thenmovetotherightsubtree.
5. Repeattheaboveprocedurerecursivelyuntilthematchisfound.
6. Iftheelementisnot foundor not presentinthetree,thenreturnNULL.
Now,let'sunderstandthesearching inbinarytreeusinganexample.Wearetakingthebinary search tree
formed above. Suppose we have to find node 20 from the below tree.
Step1:
Step2:
Step3:
Now,let'sseethealgorithmtosearchanelement intheBinarysearchtree.
Algorithm to search an element in Binary search tree
1. Search(root,item)
2. Step1-if(item=root→data)or(root=NULL)
3. returnroot
4. elseif(item<root→ data)
5. returnSearch(root→left,item)
6. else
7. returnSearch(root→right,item)
8. ENDif
9. Step2-END
InsertioninBinarySearchtree
AnewkeyinBSTisalwaysinsertedattheleaf.
To insert an element in BST, we have to start searching from the root node; if the node to be
inserted is less than the root node, then search for an empty location in the left subtree. Else,
search for the empty location in the right subtree and insert the data.
Now,let'sseetheprocessofinserting anodeinto BSTusing anexample.
DeletioninBinarySearchtree
In a binary search tree, we must delete a node from the tree bykeeping in mind that the property
of BST is not violated. To delete a node fromBST, there are three possible situations occur -
o Thenodetobedeletedistheleafnode,or,
o Thenodeto bedeleted hasonlyonechild,and,
o Thenodeto bedeleted hastwochildren
1. Whenthenodetobedeleted istheleafnode
o ItisthesimplestcasetodeleteanodeinBST.Here,wehavetoreplacetheleafnode with NULL
and simply free the allocated space.
o Inbelowimage,supposewehavetodeletenode 90,as thenode tobedeletedisaleaf node, so it
will be replaced with NULL, and the allocated space will free.
Formally, a graph is a pair ofsets (V, E), where Vis the set ofvertices and E is the set ofedges,
connecting the pairs of vertices.
In the above Graph, the set of vertices V = {0,1,2,3,4} and the set of edges E = {01, 12, 23, 34,
04, 14, 13}.
Applications
Graphs are used to solve many real-life problems. Graphs are used to represent networks. The
networks may include paths in a city or telephone network or circuit network. Graphs are also
used in social networks like linkedIn, Facebook. For example, in Facebook, each person is
represented witha vertex(ornode). Eachnode is a structure and contains information like person
id, name, gender etc.
GraphTerminologies
1. Undirected: Agraphinwhichalltheedgesarebi-directional.Theedgesdonotpointina specific
direction.
2. Directed Graph orDigraph: AgraphG=(V, E) witha mapping fsuchthat everyedge maps
onto some ordered pair of vertices (Vi, Vj) is called Digraph. It is also called Directed Graph.
Ordered pair (Vi, Vj) means an edge between Vi and Vj with an arrow directed from Vi to Vj.
3. Weighted Graph:A graph that has a value associated with every edge. The values
corresponding to the edges are called weights. A value in a weighted graph can represent
quantities such as cost, distance, and time, depending on the graph. Weighted graphs aretypically
used in modeling computer networks.
4. Unweighted Graph:A graph in which there is no value or weight associated with the
edge. All the graphs are unweighted by default unless there is a value associated.
5. Complete Graph:A simple graph with n vertices is called a complete graph if the degree
of each vertex is n-1, that is, one vertex is attach with n-1 edges. A complete graph is also called
Full Graph.
6. CyclicGraph:Foragraphtobecalledacyclicgraph,itshouldconsistofatleastonecycle.Ifa graph
hasaminimumofonecycle present, it is called acyclic graph.
7. Acyclic Graph:A graph is called an acyclic graph if zero cycles are present, and an acyclic
graph is the complete opposite of a cyclic graph.
RepresentationofGraph
Thegraphcanberepresentedasmatrices.
Adjacency Matrix
Adjacency Matrix is used to represent a graph. We can represent directed as well as undirected
graphs using adjacency matrices..
If agraph hasn numberofvertices,then theadjacency matrix of thatgraphisnx n,andeach entry of
the matrix represents the number of edges from one vertex to another.
AdjacencyMatrixRepresentationforundirectedgraph
IfanUndirectedGraphGconsistsofnverticesthentheadjacencymatrixofagraphisnxn matrix A = [aij]
and defined by -
aij=1{ifthere isapathexists fromVitoVj} aij = 0
{Otherwise}
Inanundirectedgraph,ifthereisanedgeexistsbetweenVertexAandVertexB,thenthe vertices can be
transferred from A to B as well as B to A.
Theadjacencymatrixoftheabovegraphwillbe-
Adjacencymatrixrepresentationforadirectedgraph
Theadjacency matrixoftheabovegraphwillbe-
Propertiesoftheadjacencymatrix
Someofthepropertiesoftheadjacencymatrixarelistedasfollows:
o An adjacency matrix is a matrix that contains rows and columns used to represent a
simple labeledgraphwiththenumbers0and1 inthepositionof(VI, Vj),accordingtothe
condition of whether or not the two Vi and Vjare adjacent.
o For adirected graph, ifthere isanedgeexistsbetween vertex ior Vito Vertexjor Vj, then
the value of A[Vi][Vj] = 1, otherwise the value will be 0.
WorkingofBubblesortAlgorithm
Tounderstandtheworkingofbubblesort algorithm, let'stakeanunsortedarray. Let the
elements of array are -
FirstPass
Sortingwillstartfromtheinitialtwoelements.Letcomparethemtocheckwhichis greater.
Now,movetotheseconditeration.
Second Pass
Thesameprocesswillbefollowedforsecond iteration.
Now,movetothethirditeration. Third
Pass
Thesameprocesswillbefollowedforthirditeration.
Now,movetothefourthiteration.
Fourth pass
Similarly,afterthefourthiteration,thearraywill be-
Case TimeComplexity
BestCase O(n)
AverageCase O(n2)
WorstCase O(n2)
SpaceComplexity
SpaceComplexity O(1)
Quicksort
Quick sort is a faster and highly efficient sorting algorithm which follows the divide and
conquers approach.
Divide and conquer is a technique of breaking down the algorithms into sub problems, then
solving the sub problems, and combining the results back together to solve the originalproblem.
quickSort(a[],beg,end)
1. if(beg<end)
2. pivot=partition(a,beg,end)
3. quickSort(a,beg,pivot-1)
4. quickSort(a,pivot+1,end)
End
partition(a,beg, end)
1.pivotItem=a[beg]
2. i=beg
3. for(j=beg+1;j<=end;j++)
4. if(a[j]<=pivotItem)
5. i=i+1
6. if(i!=j)
7. swap(a[i],a[j])
8. swap(a[beg],a[i])
9. returni
End
WorkingofQuickSortAlgorithm
To understand the working of quick sort, let's take an unsorted array. It will make the concept more
clear and understandable.
Lettheelementsofarrayare-
Now,a[pivot]<a[right], soalgorithmmovesforwardonepositiontowardsleft,i.e.–
Now, a[left] = 19, a[right] = 24, and a[pivot] = 24. Since, pivot is at right, so algorithm startsfrom
left and moves to right.
Asa[pivot] >a[left], soalgorithmmovesonepositiontorightas -
Now, a[left] = 9, a[right] = 24, and a[pivot]= 24. As a[pivot] >a[left], so algorithmmoves one
position to right as -
Now, a[left] = 29, a[right] = 24, and a[pivot] = 24. As a[pivot] < a[left], so, swap a[pivot] and
a[left], now pivot is at left, i.e. –
Since, pivot is at left, so algorithm starts from right, and move to left. Now, a[left] = 24,a[right]
= 29, and a[pivot]= 24. As a[pivot]< a[right], so algorithm moves one positionto left, as –
Now, a[pivot] =24, a[left] = 24, and a[right]= 14. As a[pivot] >a[right], so, swap a[pivot]and
a[right], now pivot is at right, i.e. –
Now, a[pivot]= 24, a[left] = 14, and a[right] =24. Pivot is at right, sothe algorithmstarts from
left and move to right.
Now,a[pivot]=24,a[left]=24,anda[right]=24.So,pivot,leftandrightarepointingthe same element. It
represents the termination of procedure.
Element 24,whichisthepivotelementisplacedatitsexactposition.
Elements that are right side of element 24 are greater than it, and the elements that are left side
of element 24 are smaller than it.
Now, in a similarmanner, quick sort algorithm isseparately applied to the leftand rightsub-
arrays. After sorting gets done, the array will be –
1. TimeComplexity
Case TimeComplexity
BestCase O(n*logn)
AverageCase O(n*logn)
WorstCase O(n2)
2. SpaceComplexity
SpaceComplexity O(n*logn)
MERGING:
Theoperationofsorting iscloselyrelatedto theprocessofmerging. The merging oftwoorder table
which can be combined to produce a single sorted table.
Thisprocesscanbeaccomplisheseasilybysuccessivelyselectingtherecord withthesmallest key
occurring by either of the table and placing this record in a new table.
SIMPLEMERGE
SIMPLE MERGE[FIRST,SECOND,THIRD,K]
Giventwoordersintable sortedina vectorKwithFIRST,SECOND,THIRD
ThevariableI&JdenotesthecurserassociatedwiththeFIRST&SECONDtable respectively. L is the
index variable associated with the vector TEMP.
Algorithm
Step1:[Initialize]
Set I = FIRST
SetJ =SECOND
SetL=0
Step2:[Comparecorrespondingelementsandoutputthesmallest]
Repeat while I < SECOND & J < THIRD
IfK[I]<=K[J],thenL=L+1
TEMP [L]=K[I]
I=I+1
Else L=L+1
TEMP[L]=K[J]
J=J+1
Step3:[Copyremainingunprocessedelement inoutputarea] If
I>=SECOND
ThenrepeatwhileJ<=THIRD L=L+1
TEMP[L]=K[J]
J=J+1
Else
RepeatwhileI<SECOND
L=L+1
TEMP[L]=K[I]
I=I+1
Step4:[Copytheelement intotemporaryvectorintooriginalarea]
Repeat for I=1,2,…..L
K[FIRST-I+1]=TEMP[I]
Step5:Exit
SEARCHING
Findingthe locationofagiveniteminaparticulararrayiscalledassearching. These are 2
types Searching
1. LinearSearch
2. BinarySearch
LinearSearch
Suppose‘a’isanarraywithnelement.
Tofindanitemwehavetosearchthearray‘a’ fromthefirstelementsequentially.
Thissequentialsearch isknownasLinearSearch.
Algorithm:
voidlinearsearch(int a[],int n,intno)
{
intloc= -1,i;for (i=0;i<n;i++)
{
if(a[i]==no)loc=i;
}
if(loc==-1)
Printf(“Datanotfound”); else
Printf(“Datafoundat%dposition/location”,loc);
}
Wherea[]= nameofthe array
n=no.ofelementpresentinarray‘a’
no =numbertobesearchedloc=locationtobesearched
BinarySearch
- SequentialSearchisasimplemethodforsearchinganelementinarray.
- Butthisisnotefficientforlargelistbecausewewillhavetomakencomparisonto search for the
last element in the list.
- Thebinarysearchtechnique isverymuchefficient andappliedonlyinsortedarray. Ex:
Searching a name in telephone directory or searching a word in dictionary.
- In this method we make a comparison between the key and the middle elementof
thearray.
- Since the array is sorted this comparison results either in a match between key and the
middle element of the arrayor identifying the left half or result half of the array.
- Whenthe current element is not equalto the middle element ofthe array, the procedure is
repeated on the half in which the desired element is likely to be present.
- Proceedingin thisway,eithertheelementisdetected orfinal divisionleadstoahalf consisting
of no element.
Algorithm:
intBinarySearch(intkey,inta[],int n)
{
intlow,hi,mid; low = 0
;
hi= n-1;
while(low<=hi)
{
mid = (low+hi)/2;
if(key==a[mid])
return(mid);
if(key<a[mid])
hi = mid-1;else
low=mid+1;
}
return(-1);
}
Example:
Supposethearrayelementsare 11,
22, 33, 44, 55, 66, 77
Is33== a[3]No
Is33<a[3] yes
thestepswillberepeatedforlowerhalf hi =
mid-1=3-1=2
low=0
Is low <= hi
Mid=(0+2)/2=1
Is33==a[2]
Yesthesearchissuccessfulatindex 2.
Chapter-9
FILE ORGANIZATION
File:
Afileisanexternalcollectionofrelateddatatreatedas a unit.
Filesare storedinauxiliary/secondarystoragedevices.
– Disk
– Tapes
Afileisacollectionofdatarecordswitheachrecord consistingofoneormorefields.
A file is a collection of data records grouped together for purpose of access control
andmodification.
It is the primaryresource inwhichwe canstore information and canretrievethe
information when it is required.
Theabsolutefile nameconsistsof:
• drivename
• directoryname(s)
• filename
• Extension
Forexample:d:/network/LAN.doc
TermsUsedwithFiles
Field: This is the basic element of data which contains a single value
andcharacterizedby itslength and data type.
Record: This is the collection of related fields that can be treated as a unit bysome
applicationprogram.
Extension TypeofDocument
TypeofDocument Application
Word- MicrosoftWord2003
MicrosoftWord2003
.doc
processingdocument
Word- MicrosoftWord2007
MicrosoftWord2007
.docx
processingdocument
Word- MicrosoftWorkswordprocessing
MicrosoftWorkswordprocessing
.wks
processingdocument
Word- CorelWordPerfect
CorelWordPerfect
.wpd
processingdocument
PortableDocumentFor
PortableDocumentFor AdobeAcrobatorAdobeReader
AdobeAcrobatorAdobeReader
.pdf
mat
File Operations:
Differenttypesofoperationscanbeperformedonthefile.
• Create
• Delete
• Open
• Close
• Read
• Write
FILEACCESSINGMETHODS
Information is stored in files and files reside on secondary storage. When this information is to
beused, it hasto beaccessedand brought intoprimarymain memory.Informationin filescould be
accessed in many ways. There are 3 different file access methods.
Sequentialaccessmethod
Directaccessmethod
Indexedsequentialaccessmethod
Sequential Access Method:
This method is simplest among all methods. Information in the file is processed in order,
one record after the other.
Magnetictapesaresupportingthistypeoffile accessing.
Ex:Afileconsisting of100 records,thecurrentposition
of read/write head is 45th record, suppose we want to read the 75th record, then it
accesssequentially from 45,46,. 74,75.
So,theread/writehead traversealltherecordsbetween45 to75.
Sequentialfilesaretypicallyusedin batchapplicationand payrollapplication.
Directaccess/Randomaccessmethod:
Direct accessisalso calledasrelativeaccess.
Inthismethod recordscanread/write randomlywithoutanyorder.
The directaccessmethodis basedon diskmodel of a file,because disks allow random access
to any file block.
Adirectaccessfileallowsarbitraryblocksto bereadorwritten.
Forex.Adiskconsistingof256blocks, thecurrentpositionof
read/write head isat 95th block. The blockto be read or write is250th block. Thenwe can
access the 250th block directly without any direction.
Best example for direct access is a CD consisting of10 songs, at present we are listening
the song no.3, suppose we want listening the song no.9, thenwe canshift fromsong no.3 to
9 without any restriction.
Indexedsequentialaccessmethod:
The main disadvantage in the sequential file is, it takes more time to access a record, to
overcome this problem, we are using this method.
In this method, the records are stored sequentially for efficient processing. But they
canbe accessed directlyusing indexorkeyfield. That’s whythis method is said to bethe
indexed sequential file.
Keysarepointerwhichcontainsaddressofvariousblocks.
Recordsareorganizedinsequencebased onakeyfield.
Generallyindexedfilesareusedinairlinereservationsystemandpayrollpackages
ADDRESSCALCULATIONORHASHING
Sohashingisaprocesstogeneratethephysicaladdressfromkeyvalueindirectaccessfile.
H(k)=L
WhereH->function
K->key
L->address
Characteristicsofhashing function
- Itshouldbeveryeasyandquick tocompute.
- Thehashfunctionshouldbeasfaraspossibleuniformlydistributethehashaddress throughout
the set L, so that there will be minimum no. of collision.
Typesofhash function
1) DivisionRemaindermethod
2) Midsquaremethod
3) Folding method
Division- RemainderMethod
chooseanumbermlargerthanthenumbernofkeysinK.(misusuallyeitheraprime numberor a number
without small divisor) the hash function H is defined by
H(k)=k(modm)orH(k)=k(modm)+1.
here k (mod m) denotes the reminder when kis dividedby m. the secondformulais used whenwe
want a hash address to range from 1 to m rather than 0 to m-1.
Theremainder iscalculatedphysicaladdress forthatrecord. Suppose
key value of the record is 2345.
i.e. k=2345andm= 97thenH(k)=2345 %97=17
Sothat particularrecordwillbesortedin17 locationofmemory.
Generally m should be a prime number.
MidSquaremethod
Thekeyvalueissquared.Thenthemiddigitsaretheaddressforthatkey.
Supposekeyvalue=2345 k2
= 5499025
Ifthememoryisoftwobitthen mid will be99.
FoldingMethod
Infolding methodthekeyvalue is folded into no.ofparts,whereeachpartcontainssame no.of digits
except possibly the last.
H(k) = k1+
k2+k3…..knSupposekeyvalue=23
56
k=2356 k1=23,k2=56
23+56=79
Sotheaddressforthatrecordis79.
Collision
Supposewehavecalculatedanaddress Lforagivenkeyvaluek.Butifthat locationLis previously
occupied by some other record then that situation is known as collision.
Soifthe hashfunctionwillgeneratesameaddressfortwodifferent keyvalues.Thenit is said tobe
collision.
Consider the division remainder
method.Letk1=150,k2=184, m
=17
H (k1)=150mod17=14
H (k2)=184mod17=14
Sofor keyvalues150and184thecalculatedaddress is14.Thisiscollision.
Therearevariouscollisionresolutiontechnique.
- Linearprobing( alsocalledopenaddressing orarraymethod)
- QuadraticProbing
- Doublehashing
- SeparateChaining(alsocalledclosedhashingorlinked listmethod)
Advantages
- Accessingarecordis faster.
- Noneedtoarrangetherecordonasortedorder.
- Ifrequired,recordscanbeprocessed sequentially.
- Insertion,deletioniseasier
Disadvantage
- ExpensiveI/Odeviceascomparedtosequentialfile structure.
- Addressgenerationoverheadduetohashingfunction.
- Updationofallrecordisinefficientascomparedtosequential file.
Application
Interactiveonlineapplicationsuchasairlineandrailwayreservationsystem.
References
1. BookReference:DataStructurebyS. LipschutzSchaumSeries
2. BookReference:DataStructuresUsingC
byAmiyaKumarRath(Author),AlokKumarJagadev(Author)
3. BookReference:DataStructuresUsingCbyUditAgrawal
4. https://nptel.ac.in
5. https://www.javatpoint.com
6. https://www.geeksforgeeks.org