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

MA 511: Computer Programming: Partha Sarathi Mandal

This document summarizes a lecture on linked lists using self-referencing structures in C programming. It introduces the concept of a node structure that contains an integer data field and a pointer to the next node. It shows how to create a linked list recursively by allocating memory for each new node and linking it to the next node until a null terminator is reached. Code examples are provided to create a linked list by recursively taking user input and to print out the linked list by traversing from the start node to the null terminator.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views

MA 511: Computer Programming: Partha Sarathi Mandal

This document summarizes a lecture on linked lists using self-referencing structures in C programming. It introduces the concept of a node structure that contains an integer data field and a pointer to the next node. It shows how to create a linked list recursively by allocating memory for each new node and linking it to the next node until a null terminator is reached. Code examples are provided to create a linked list by recursively taking user input and to print out the linked list by traversing from the start node to the null terminator.

Uploaded by

Naveen Gupta
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

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

You might also like