13 Lists
13 Lists
Linked List
“All the kids who did great in high school writing pong games in
BASIC for their Apple II would get to college, take CompSci 101,
a data structures course, and when they hit the pointers
business their brains would just totally explode, and the next
thing you knew, they were majoring in Political Science because
law school seemed like a better idea.”
List
• A list refers to a sequence of data items.
• Example: An array
• The array index is used for accessing and manipulation of array elements.
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
• Wasteful of space
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Linked List
• A completely different way to represent a list
• The structure also contains a pointer or link to the structure (of the same
type) next data
• They are ordered by logical links that are stored as part of the data in the
structure itself
Question?
• What are the values of:
• a.next->data
• a.next->next->data
• Declare three nodes n1, n2, and n3: struct stud n1, n2, n3;
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Example:
#include <stdio.h> /* Now traverse the list and print
struct stud { the elements */
int roll; p = head; /* point to head */
char name[30]; while (p != NULL)
int age; {
struct stud *next; printf(“\n %d %s %d”,
}; p->roll, p->name, p->age);
int main() { p = p->next;
struct stud n1, n2, n3; }
struct stud *p, *head; return 0;
scanf (“%d %s %d”, &n1.roll, n1.name, &n1.age); }
scanf (“%d %s %d”, &n2.roll, n2.name, &n2.age);
scanf (“%d %s %d”, &n3.roll, n3.name, &n3.age);
n1.next = &n2 ;
n2.next = &n3 ;
n3.next = NULL ;
head = &n1;
......
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Alternative Way
• Instead of statically declaring the structures n1, n2, n3, dynamically
allocate space for the nodes.
• If you lose the start pointer, you cannot access any element in the list, as
elements are only accessible from the next pointers in the previous
element
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Insertion in a list
• To insert a data item into a linked list involves
• creating a new node containing the data
• finding the correct place in the list, and
• linking in the new node at this place
18 3
head
15 18 3
head
15 18 3
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Using the insert function struct node* insert(struct node *head, int value) {
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
p->data = value;
p->next = head;
return p;
void display(struct node*); }
struct node* insert(struct node *, int);
void display (struct node *head) {
struct node *p = head;
int main() { printf(“List = {”);
while(p != NULL) {
struct node *head = NULL; printf("%d, ", p->data);
p = p->next;
head = insert(head, 10); }
display(head); }
printf(“}\n”);
18 3
head
18 3 15
head
18 3 15
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Using insert at end function struct node* insert(struct node *head, int value) {
struct node *p;
p = (struct node *) malloc(sizeof(struct node));
struct node* insert_end(struct node *head, int value) { p->data = value;
struct node* q = head; p->next = head;
while(q->next != NULL) return p;
q = q->next; /* find the last element */ }
struct node* p = (struct node*) malloc(sizeof(struct node));
p->data = value; p->next = NULL;
q->next = p; void display (struct node *head) {
return head; struct node *p = head;
} printf(“List = {”);
while(p != NULL) {
printf("%d, ", p->data);
void display (struct node *); p = p->next;
struct node* insert(struct node*, int); }
printf(“}\n”);
struct node* insert_end(struct node*, int); }
int main() {
struct node *start = NULL;
start = insert_end(start, 10);
display(start); Output
start = insert_end(start, 11);
display(start);
List = {10, }
start = insert_end(start, 12); List = {10, 11 }
display(start);
return 0; List = {10, 11, 12, }
}
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
3 5 8
7
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
3 5 8
7
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
• Link in new node with previous (even if last) and pointer nodes
3 5 7 8
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
3 5 7 8
head
3 5 7 8
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
3 5 7 8
head
3 5 7 8
head
3 5 8
Chetan Arora and Vireshwar Kumar COL 100: Introduction to Computer Science.
Department of Computer Science and Engineering, IIT Delhi Semester 1, 2023-24
Practice Excercises
• Print a list backwards (also try recursive Print here)
• Count the number of elements in a list (both using and not using recursion)
• Reverse a list