MA 511: Computer Programming
Lecture 11: Linked list
http://www.iitg.ernet.in/psm/indexing_ma511/y10/index.html
Partha Sarathi Mandal
[email protected] Dept. of Mathematics, IIT Guwahati
Semester 1, 2010-11
Self-Reference Structure
struct tag {
list_of_no structure looks like
member 1;
member 1;
….. number next
struct tag *name;
};
Example:
struct list_of_no { It contains
int number; the info/data It contains an
struct list_of_no *next; address of
}; the structure
It’s a structure of type list_of_no. there are two members;
1. number is a integer type and
2. next is a pointer to a structure of the same type, i.e., a pointer to a structure of
type list_of_no.
MA511: Computer Programming
Partha S Mandal, IITG
Linked Lists using
Self-Reference Structure
struct list_of_no {
int number;
struct list_of_no *next;
};
typedef struct list_of_no node;
node *start;
start = (node *) malloc(sizeof(node));
start
number next number next number next
102 112 999 NULL
MA511: Computer Programming
Partha S Mandal, IITG
How to create a linked list recursively?
void create(node *temp){
#define NULL 0 node *temp1
struct list_of_no {
int data; printf(“Type int for continue Type 999 for stop:”);
struct list_of_no *next; scanf(“%d”, &(temp->data));
}; if(temp->data==999)
typedef struct list_of_no node; temp->next = NULL;
void create(node *pt); else{
void print(node *pt); temp1 = (node *) malloc(sizeof(node));
temp->next = temp1;
create(temp1);
main(){ }
node *start; }
start = (node *) malloc(sizeof(node)); void print(node *temp){
create(start); while(temp->next){
print(start); printf("|%d |->", temp->data);
temp=temp->next;
} }
temp1
start }
number next number next number next
temp
102 112Programming
MA511: Computer 999 NULL
Partha S Mandal, IITG