Escolar Documentos
Profissional Documentos
Cultura Documentos
P. P. Chakrabarti
20-03-03
Linked List
Storing a set of elements = {15,18,12,7}
15
18
12
7
NULL
struct list {
int data ;
struct list * next ;
};
struct list *p, *q;
2
data
int
20-03-03
next
list *
P.P.Chakrabarti, IIT Kharagpur
Manipulation
struct list {
int data ;
struct list * next ;
};
struct list *p, *q;
15
15
18
NULL
q
p->next = q;
15
18
q
20-03-03
NULL
P.P.Chakrabarti, IIT Kharagpur
Pointers &
Structures
struct list {
int data ;
struct list * next ;
};
struct list *p, *q;
struct list a,b;
15
15
18
p->next = &b;
15
18
q
NULL
20-03-03
NULL
P.P.Chakrabarti, IIT Kharagpur
Insert in front
struct list * insert(struct list * r, int value)
{
r
struct list * p;
p = (struct list *) malloc(sizeof(struct list));
p->data = value;
p ->next = r;
15
18
p
return p;
r
}
15
18
18
20-03-03
Insert at end
11
4
15
if (r==NULL) return p;
NULL
NULL
r
q
q=r;
while(q->next!=NULL) q=q->next;
q->next =p;
p
return r;
11
4
15
}
NULL
q
r
6
20-03-03
find
struct list * find (struct list * r, int value)
{
struct list *p;
p = r;
while(p!=NULL){
if (p->data == value) return p;
p = p->next;
}
return p;
}
20-03-03
delete
struct list * delete(struct list * r, int value)
{ struct list *p, *q;
p =r;
q = p;
11
while(p!=NULL) {
q
if (p->data == value){
if (p==r) r = p->next;
else q->next = p->next;
p->next = NULL;
11
free(p);
return r; }
q
else { q = p;
p = p->next; }
}
return r;
11
}
delete(r,4)
15
15
15
4
p NULL
20-03-03
20-03-03
void initialize(set * X)
{
X->start=NULL;
return;
}
10
20-03-03