ROUTINE BASED SINGLE LINKED LIST, DOUBLY LINKED LIST
Operations
|
Single Linked
List
|
Doubly Linked
List
|
|
Structure
|
Generally
data in the single linked list are maintained in the form of nodein which
each node consists of data and link
Link
represents address to the next node.
|
·
Generally data in the doubly linked list are maintained in the form of
node in which each node consists of data and forward link and backward link
·
Forward Link represents address to the next node.
And backward link represents the address of previous node in the list
|
|
struct
node
{
int
data;
struct
node *link;
}
|
struct
dnode
{
int
data;
struct
dnode *flink;
struct
dnode *flink;
}
|
||
Find
|
·
This function is used to identify the position
for the corresponding data in the list
·
This function is used by the insert and delete
operation in the list
·
In which while inserting the data into the list,
it need thelocation(Address) to store the
after the location.
·
It accepts data and the head address as
parameters.
|
||
struct
node *find(int pos,struct node *head)
{
struct
node *p;
p=headèlink;
while(p!=null)
{
if(pèdata==pos)
return
p;
else
p=pèlink;
}
return
null;
}
|
Struct
dnode *find(int pos,struct node *head)
{
Struct
dnode *p,*q;
p=headèflink;
while(p!=null)
{
if(pèdata==pos)
return
p;
else
p=pèflink;
}
return
null;
}
|
|
|
Operations
|
Single Linked
List
|
Doubly Linked
List
|
|
Find previous
|
·
This function is used to identify the previous
node address (position) for the corresponding node.
·
This information is used to perform delete
operation so as to maintaining the list we are in need of previous node to
be linked with next node for the deleted node.
·
This function is not for doubly linked list since
each consists of blink and flink.
|
||
struct
node *findpre(int x,struct node *head)
{
struct
node *pre,*p;
pre=head;
p=headà link;
while(p!=null)
{
if(pèdata==x)
return
pre;
else
{
pre=p;
p=pèlink;
}
}
return
NULL;
}
|
structdnode
*findpre(int x,struct node *head)
{
Struct
dnode *pre,*p;
pre=head;
p=headà flink;
while(p!=null)
{
if(pèdata==x)
return
pre;
else
{
pre=p;
p=pèflink;
}
}
return
NULL;
}
|
|
|
Insert
|
·
This function is used to insert an element into a
list
·
Before inserting an element we have to identify
the position where we have to insert.
·
So this function accepts data, position (data)
and head address.
|
|
|
void
insert(int n,int pos,struct node *head)
{
struct
node *p,*q;
p=malloc(sizeof(struct
node));
pèdata=n;
q=find(pos,head);
pèlink=qèlink;
qèlink=p;
}
|
void
insert(int n,int pos,struct node *head)
{
struct
dnode *p,*q;
p=malloc(sizeof(struct
dnode));
pèdata=n;
q=find(pos,head);
pèflink=qèflink;
qèflinkèblink=p
qèflink=p;
pèblink=q;
}
|
|
|
Operations
|
Single Linked
List
|
Doubly Linked
List
|
|
Delete
|
|
||
void
delete(int n,struct node *head)
{
struct
node *pre,*p;
pre=findprevious(n,head);
p=find(p,head);
preèlink=pèlink;
}
|
void
delete(int n,struct dnode *head)
{
struct
dnode *p;
p=find(p,head);
pèflinkèblink=pèblink;
pèblinkèflink=pèflink;
}
|
|
|
Display
|
|
||
void
display(struct node *head)
{
struct
node *p;
p=headèlink;
while(p!=null)
{
printf(“%d”,pèdata);
p=pèlink;
}
}
|
void
display(struct dnode *head)
{
struct
dnode *p;
p=headèflink;
while(p!=null)
{
printf(“%d”,pèdata);
p=pèflink;
}
}
|
|
Note: In case of
cursor address can be replaced by index.
0 comments:
Post a Comment