Data Structures Unit 1 Notes
Data Structures Unit 1 Notes
Datastructure
Primitivedata Nonprimitivedata
structures structures
Example:int,char,float
Abstraction
Abstractmodel
Meena Leena Nisha Neeta Geeta Rita
Listofsequentiallystoredelements-UsingArrays
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
temp/head New
10 20 NULL temp=New;
/*now'temp'ismoved ahead
head New/temp
*
10 20 30 NULL tempnext=New
head temp New
10 20 30 NULL temp=New;
head New/temp
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=tempnext i.e.20willbedisplay
ed as temp ! =
NULL
10 20 30 40 50 NULL
head temp
4 5
temp data
settemp=tempnext 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
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 Newnext=head; flag
= 0;
10 20 NULL temp=head;
whentempnext=head;
head/temp New
10 20 Newnext=head;
head/temp New
10 20 30 NULL Astempnext=head;
10 20 30 tempnext=New;
Newnext=head;
head temp New
10 20 30 40 NULL
head/temp New
10 20 30 40 NULL
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
tempnext=New;
9 10 20 30 40 Newnext=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 andtempdata= 30
35 NULL
New
10 20 30 40 Newnext=tempnext;
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=tempnext;
head/temp temp1
10 20 30 40 50 while(tempnext!head)
head temp1 temp temp=tempnext;
20 30 40 50 tempnext=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 tempnextdata
head temp =key,hence
temp1=tempnext;
10 20 30 40 50
head temp temp1
10 20 30 40 50 tempnext
head temp temp1 =temp1next;
10 20 30 40 50 temp1next=NULL
temp temp1
NULL 3 NULL
singlenode
Headnode
Data
prev data next
NULL 10 NULL Now,
New/head/temp
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
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=tempnext i.e.20willbedisplaye
d as temp ! = NULL
NULL 10 20 30 40 NULL
head temp
4
5
temp data
settemp=tempnext i.e.30willbedisplaye
d as temp ! = NULL
NULL 10 20 30 40 NULL
temp
6
settemp=tempnext
temp data
i.e.40willbedisplaye
d as temp ! = NULL
NULL 10 20 30 40 NULL
temp
head
settemp=tempnext
NULL 10 20 30 40 NULL
temp temp temp
head/temp
NULL10 20 30 40NULL
temp
1
Astempisnot
NULLdisplay
temp data
i.e.40
NULL10 20 30 40NULL
head temp 2
NULL10 20 30 40NULL
temp
4
5
settemp= temp prev
Display
temp data
i.e.20
NULL10 20 30 40NULL
head temp 6
prev
Display
temp data
i.e. 10as
temp!=NULL
NULL 9 10 20 30 NULL
NULL 9 10 20 30 NULL
New/temp/head
temp=tempprev;
head = temp;
Newprev=temp;
tempnext= New;
NULL 10 20 30 NULL
temp
NULL 10 20 30 NULL
22
(tempnext)prev=New; New
New next = temp next; temp
next = New;
Newprev=temp;
head/prev_node temp
NULL 10 20 30 NULL
prev_node=tempprev;
setprev_node=tempprev;
I
head/prev_node temp
NULL 10 20 30 NULL
II I prev_nodenext=tempnext;
II (tempnext)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
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