0% found this document useful (0 votes)
11 views109 pages

Data Structures Unit 1 Notes

Uploaded by

Ns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views109 pages

Data Structures Unit 1 Notes

Uploaded by

Ns
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 109

JEPPIAAR UNIVERSITY

Datastructure

Primitivedata Nonprimitivedata
structures structures
Example:int,char,float

Linear data Non linear data


structures structures
Example:lists, Example:trees,
stacks,queues graphs
Problem

Abstraction

Abstractmodel
Meena Leena Nisha Neeta Geeta Rita

Listofsequentiallystoredelements-UsingArrays

Meena Leena Nisha Neeta Geeta Rita NULL

ListofelementswithassociatedPointers-UsingLinkedList
First node. The next field in this node gives the
index as 0.Then we get the address of next node.
We can further trace the list using next field

Lastnode.Thenextfieldgivestheindexas–1 and–
1isnotanysubscriptinthearray.Hence – 1 is
taken as end of the list.
Thea[i].nextisthenextfieldofcurrentnode.
Byassigningittoiwearegoingtonextarray
index at which next node can be placed
data next
0 –1 0 Searchingfor
if(a[i].data==temp).
1 20 6
2 –1 0
3 –1 0
a[i]=a[1].data= temp
4 10 1 =20
5 –1 0
6 30 7
7 40 –1
8 –1 0
9 –1 0

data next
0 –1 0
Copynext 1 20 6 As i= 1
indexhere a[i].data=20
2 –1 0 a[i].next=6
3 –1 0 We will copy a[i].next
Copynewdata toa[i+1].next.Hence
21 here. 4 10 1 a[2].next = 6
5 –1 0 and
a[2].data=21i.e.new_data
6 30 7
7 40 –1
8 –1 0
9 –1 0

Thisforloopinitializethedata field
of list to –1
Atdatafieldifvalueis–1thenitindicates that
the location is empty.
data link

10 20 30 40 NULL
10 20 30 40 NULL

Firstor
head

10 20 30 40
Null 10 20 30 40 NULL
Heador
first

99

10 20 30 40

10 NULL 20 NULL Gets created after


invokingget_node()
head/temp New

10 20 NULL tempnext= New;

temp/head New

10 20 NULL temp=New;
/*now'temp'ismoved ahead
head New/temp
*


10 20 NULL 30 NULL byinvoking


 get_node()
head temp New

10 20 30 NULL tempnext=New
head temp New

10 20 30 NULL temp=New;
head New/temp

10 20 30 NULL 40 NULL byinvoking


get_node()
head temp New

10 20 30 40 NULL temp next=New;


head New/temp temp = New

Nextifweenter50 then

10 20 30 40 50 NULL
head temp/New
10 20 30 40 50 NULL
head/t
emp
1 temp data
i.e.10willbedisplay
ed as temp ! =
NULL

10 20 30 40 50 NULL
head temp
2 3 temp data
settemp=tempnext i.e.20willbedisplay
ed as temp ! =
NULL

10 20 30 40 50 NULL
head temp
4 5
temp data
settemp=tempnext i.e.30willbedisplay
ed as temp ! =
NULL

10 20 30 40 50 NULL temp
head
settemp=temp next

    


 Nonodeinthelinkedlist.i.e.
when linked list is empty

10 20 30 40 50 NULL
head/temp
If want to insert this
9 NULL nodeasaheadnode
then
New

9 10 20 30 40 50 NULL

New/head temp

Node is attachedto
thelinkedlistas a
head node.
Finding end of the linked list.
Thentempwillbealastnode

10 20 30 40 50 NULL
head/temp
If we want to insert 60 NULL
thisnodeasalastnode
then New

10 20 30 40 50 NULL
temp



10 20 30 40 50 60 NULL
temp New



10 20 30 40 50 NULL
head/temp

31 NULL
New

10 20 30 40 50 NULL
temp

31 NULL
New
10 20 30 40 50NULL
temp
This can be
31 doneusing
these'C'
New statements



 


Alwayscheckbeforedeletionwhether the
linked list is empty or not.
Becauseifthelistisemptythereis no
point in performing deletion
If we want to delete head node
thensetadjacentnodeasanew
head node and then deallocate
the previous head node

10 20 30 40 50 NULL

10 20 30 40 50 NULL
prev temp

 
10 20 30 40 50 NULL
prev temp

10 20 40 50 NULL

10 20 30 40 50 NULL

head/ Wewantto
temp delete this
node.
10 20 30 40 50 NULL
temp head

Compare each node with the


keyvalue.Ifnotmatchingthen
move to next node

Ifthenodecontainingdesired
data is obtained in the linked
list then found variable is
settoTRUE.
10 20 30 40 50 NULL
head/temp

10 20 30 40 50 NULL
temp

 

10 20 30 40 50 NULL

temp

 

10 20 30 40 50 NULL
temp

 
head1
10 30 50 NULL

head2
20 40 NULL

onmerging
two lists

10 20 30 40 50 NULL

























 


 





 

 

















10 20 30 40 50
 Node
 a[0] a[1] a[2] a[3] a[4]
 Data Nextlink

Index














 10 20 30 40 50
head
node

 –999 10 20 30 40 50
head
node














Supposewehavetaken
element'10'theflag=1,
 10
 head=New;
New/head Newnext=head; flag
= 0;


10 20 NULL temp=head;
whentempnext=head;
head/temp New

10 20 NULL tempnext= New;


head/ New
temp

10 20 Newnext=head;
head/temp New

10 20 30 NULL Astempnext!=head; temp =


temp next;
head/temp New

10 20 30 NULL Astempnext=head;

head temp New

10 20 30 tempnext=New;
Newnext=head;
head temp New
10 20 30 40 NULL
head/temp New

10 20 30 40 NULL

head temp New

10 20 30 40 NULL
head temp New

10 20 30 40
head temp New

10 20 30 40 50
head

Thenextnodeoflast
node is head node
10 20 30 40
head/temp

9 NULL
New

10 20 30 40 9 NULL
head temp New

tempnext=New;
9 10 20 30 40 Newnext=head;
head/New temp head = New;

10 20 30 40 50 NULL
head temp New

10 20 30 40 50
head temp New

10 20 30 40
Askey=30
head temp andtempdata= 30

35 NULL
New

10 20 30 40 Newnext=tempnext;
temp temp next = New;
head
35
New
If a single node is
presentinthelistand
we want to delete it

Thepreviousnodeofthe
node to be deleted is
searched.
temp1isthenodetobe
deleted
10 20 30 40 50
head/temp

10 20 30 40 50 temp1=tempnext;
head/temp temp1

10 20 30 40 50 while(tempnext!head)
head temp1 temp temp=tempnext;

20 30 40 50 tempnext=temp1; head
temp1/head temp = temp1;

10 20 30 40 50 Wewanttodeletenode
head/temp with '30' then

Key=30 and
10 20 30 40 50 tempnextdata
head temp =key,hence

temp1=tempnext;
10 20 30 40 50
head temp temp1
10 20 30 40 50 tempnext
head temp temp1 =temp1next;

10 20 30 40 50 temp1next=NULL
temp temp1

 

10 20 40 50 Thus node with


value 30 is
head deletedfromCLL.

Prev Data Next

NULL 3 NULL
singlenode

NULL 55 73 108 28 NULL

Headnode

Data
prev data next
NULL 10 NULL Now,
New/head/temp

gets created after


NULL 10 NULL NULL 20 NULL invokingget_node()
head/temp New

NULL 10 20 NULL temp next=New


New prev=temp;
head/temp New

temp=New;
NULL 10 20 NULL /*nowtempismoved ahead
*/
head New/ temp

gets created
NULL 10 20 NULL NULL 30 NULL afterinvoking
head temp New get_node()

temp next=New;
NULL 10 20 30 NULL New prev=temp;
head temp New

NULL 10 20 30 NULL temp=New;


head New/temp

NULL10 20 30 40 NULL
head
NULL 10 20 30 40 NULL

head/temp

1 temp data
i.e.10willbedisplaye
d as temp ! = NULL

NULL 10 20 30 40 NULL
head temp
2 3 temp data
settemp=tempnext i.e.20willbedisplaye
d as temp ! = NULL

NULL 10 20 30 40 NULL
head temp
4
5
temp data
settemp=tempnext i.e.30willbedisplaye
d as temp ! = NULL

NULL 10 20 30 40 NULL
temp
6

settemp=tempnext
temp data
i.e.40willbedisplaye
d as temp ! = NULL

NULL 10 20 30 40 NULL
temp
head

settemp=tempnext

  
NULL 10 20 30 40 NULL
temp temp temp
head/temp

temp=temp next temp=temp next


settemp=temp next

NULL10 20 30 40NULL

temp
1

Astempisnot
NULLdisplay
temp data
i.e.40

NULL10 20 30 40NULL

head temp 2

Astemp!=NULL settemp= temp prev


display 3
temp data
i.e.30

NULL10 20 30 40NULL
temp
4

5
settemp= temp prev
Display
temp data
i.e.20

NULL10 20 30 40NULL

head temp 6

set temp = temp

prev

Display
temp data
i.e. 10as
temp!=NULL


  

NULL 9 NULL NULL 10 20 30 NULL


New head/temp

NULL 9 10 20 30 NULL

New head/temp Newnext=temp;


tempprev=New;

NULL 9 10 20 30 NULL

New/temp/head
temp=tempprev;
head = temp;

NULL 10 20 30 NULL NULL 40 NULL


head temp New

NULL 10 20 30 NULL 40 NULL

head temp New

Newprev=temp;
tempnext= New;
NULL 10 20 30 NULL

NULL 22 NULL get this node


Obtainthis 2 usingget_node()
node New 1
itastemp

temp
NULL 10 20 30 NULL

22
(tempnext)prev=New; New
New next = temp next; temp
next = New;
Newprev=temp;

head/prev_node temp
NULL 10 20 30 NULL

prev_node=tempprev;

setprev_node=tempprev;

I
head/prev_node temp
NULL 10 20 30 NULL

II I prev_nodenext=tempnext;

II (tempnext)prev=prev_node;

head/prev_node
NULL 10 30 NULL
Previous Data Next
Data Nextlink linkfield linkfield
Coef exp next

 
3 2 5 1 7 0 NULL

 

 
coef exp next

pnode

p1 3 3 2 2 1 1 NULL

Similarlythep2willbe
p2 5 5 3 2 7 0 NULL

p1 3 3 2 2 1 1 NULL

Readthe
node

p2 5 5 3 2 7 0 NULL

Read
node Ifp2 exp>p 1 exp
p3 coef=p2 coef
p3 5 5 NULL
p3 exp = p 2 exp
temp p2=p2 next

p1 3 3 2 2 1 1 NULL
Ifp1 exp=p 2c exp
p3 oef = coef
p3 p1exp = exp
p1
7 0 p1=p1 next
p2 5 5 3 2

5 5 NULL 3 3 NULL
temp New

p3 5 5 3 3 NULL
p1 3 3 2 2 1 1 NULL
Ifp 1 exp=p 2c expcoef
p3 oef = +p2exp coef
p3 p1exp =
p1
p2 5 5 3 2 7 0 NULL Then
Incrementboththepointers

5 5 3 3 NULL 5 2 NULL Then


Attachtemplist
temp New withNewnode.

p3 5 5 3 3 5 2 NULL
temp

p1 3 3 2 2 1 1 NULL

p2 5 5 3 2 7 0 NULL

p3 5 5 3 3 5 2 1 1 NULL

Transferp 1node
to p3

p1 3 3 2 2 1 1 NULL NULL

p2 5 5 3 2 Transferp 2node
7 0 NULL
to p3

p3 5 5 3 3 5 2 1 1 7 0 NULL
p1 3 3 2 2 1 1 NULL NULL

p2 5 5 3 2 7 0 NULL NULL

p3 5 5 3 3 5 2 1 1 7 0 NULL
temp return(temp)



















































































































 
























  


Coeffexp
6 3 9 2 7 1 1 0 NULL

Single term
inpolynomial
=one node
–999 10 20 30 NULL
Header
node

You might also like