0% found this document useful (0 votes)
91 views

26.link List

The document discusses linked lists and their implementation in C. It describes how to create a linked list node structure with a data field and pointer to the next node. It then shows how to perform basic linked list operations like insertion at the beginning or end, finding a node, and deletion. The document also demonstrates sorting a linked list by inserting elements in ascending order. Finally, it introduces the concept of implementing sets as linked lists using a set structure containing a pointer to the start node.

Uploaded by

Pijush Karmakar
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)
91 views

26.link List

The document discusses linked lists and their implementation in C. It describes how to create a linked list node structure with a data field and pointer to the next node. It then shows how to perform basic linked list operations like insertion at the beginning or end, finding a node, and deletion. The document also demonstrates sorting a linked list by inserting elements in ascending order. Finally, it introduces the concept of implementing sets as linked lists using a set structure containing a pointer to the start node.

Uploaded by

Pijush Karmakar
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/ 10

Linked Lists:2

P. P. Chakrabarti

20-03-03

P.P.Chakrabarti, IIT Kharagpur

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

p = (struct list *) malloc(sizeof(struct list));


p->data=15;

struct list {
int data ;
struct list * next ;
};
struct list *p, *q;

15

q = (struct list *) malloc(sizeof(struct list));


q->data=18; q->next = NULL;

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;

p = (struct list *) malloc(sizeof(struct list));


p->data=15;

15

q = (struct list *) malloc(sizeof(struct list));


q->data=18; q->next = NULL;
b = *q;

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

P.P.Chakrabarti, IIT Kharagpur

Insert at end
11
4

struct list * insert_end(struct list * r,


NULL
int value)
r
q
{ struct list *p,*q;
p = (struct list *) malloc(sizeof(struct list));
p->data = value;
p
p ->next = NULL;
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

P.P.Chakrabarti, IIT Kharagpur

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

P.P.Chakrabarti, IIT Kharagpur

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

P.P.Chakrabarti, IIT Kharagpur

insert in ascending order & sort


struct list * insert_asc(struct list * r, int value)
{ sruct list *p, *q, *new;
new = (struct list *) malloc(sizeof(struct list));
new->data = value; new ->next = NULL;
p = r; q = p;
while(p!=NULL) {
if (p->data >= value) { /* insert before */
if (p==r) { new->next =r; /* insert at start */
return new; }
new->next = p; /* insert before p */
q->next = new;
return r; }
q = p;
p = p->next; } /* exists loop if > largest */
if (r==NULL) return new; /* first time */
else q->next = new; /* insert at end */
return r; }

20-03-03

main() /* insertion sort */


{
struct list *start;
int i,n,value;
start = NULL;
scanf("%d",&n);
for(i=0; i<n; i++){
printf("Give Data: " );
scanf("%d",&value);
start = insert_asc(start, value);
}
display(start);
}

P.P.Chakrabarti, IIT Kharagpur

Sets ADT using linked lists


struct list{
int data;
struct list *next;
};
typedef struct{
struct list *start;
} set;
set A,B;

Pointer is passed so that


the data inside can be changed.
(Think why passing and returning
a set is not a judicious thing to do!)

void initialize(set * X)
{
X->start=NULL;
return;
}

No need to maintain number of elements

10

20-03-03

P.P.Chakrabarti, IIT Kharagpur

You might also like