Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 25 Self-Referential Structures Linked Lists

Similar presentations


Presentation on theme: "Lecture 25 Self-Referential Structures Linked Lists"— Presentation transcript:

1 Lecture 25 Self-Referential Structures Linked Lists
C Programming Lecture 25 Self-Referential Structures Linked Lists

2 Self-Referential Structures
Self-referential structures have pointer members that hold the address of the same structure type. The pointer members allow the linking together of an unspecified number of such structures. Example: struct list { int data; struct list *next; };

3 Pictorial Representation
To help us understand and think about self-referential structures, we use pictures: data next The data members. The pointer member.

4 An Example struct list { int data; struct list *next; }
struct list a, b, c; a.data = 1; The result of these b.data = 2; declarations and c.data = 3; initializations is pictured below: a.next = b.next = c.next = NULL; a b c 1 NULL 2 NULL 3 NULL

5 Continuation of Example
a.next = &b; b.next = &c; a b c 1 2 3 NULL Now we can use the links to retrieve data from successive elements. a.next->data has a value of 2 a.next->next->data has a value of 3

6 Linear Linked Lists A linked list has a head pointer that addresses the first element of the list. Then the pointer member in each structure in the list points to a successor structure. The last structure has its pointer member set to NULL. Typically, a linked list is created dynamically.

7 Dynamic Storage Allocation
“Dynamic” storage allocation refers to allocation of storage during program execution time, rather than during compile time. Utility functions such as malloc() are provided in the standard library to allocate storage dynamically. malloc()stands for “memory allocation”.

8 Header File for Example of Dynamic Creation of a Linked List
#include <stdio.h> typedef char DATA; /* use char in examples */ struct linked_list { DATA d; struct linked_list *next; }; typedef struct linked_list ELEMENT; typedef ELEMENT * LINK;

9 Example of Dynamic Allocation of a Linked List
head = malloc(sizeof(ELEMENT)); head->d = ‘n’; head->next = NULL; head n NULL head->next = malloc(sizeof(ELEMENT)); head->next->d = ‘e’; head->next->next = NULL; head n e NULL head->next->next = malloc(sizeof(ELEMENT)); head->next->next->d = ‘w’; head->next->next->next = NULL; head n e w NULL

10 List Operations Basic List Operations Creating a list
Counting the elements Looking up an element Inserting an element Deleting an element

11 Creating a List from a String using Recursion
#include “list.h” LINK string_to_list(char s[]) { LINK head; if (s[0] == ‘\0’) /* base case */ return NULL; else { head = malloc(sizeof(ELEMENT)); head->d = s[0]; head->next = string_to_list(s + 1); return head; } /* We will walk through this in class for the string “hello” */

12 Counting the Elements in a List
/* Count the elements recursively */ #include “list.h” int count(LINK head) { if (head == NULL) return 0; else return(1 + count(head->next)); }

13 Lookup c in the List Pointed to by head
#include “list.h” LINK lookup(DATA c, LINK head) { if (head == NULL) return NULL; else if (c == head->d) return head; else return(lookup(c, head->next)); }

14 Illustration of Insertion of a New List Element
Before insertion: . . . A C B q NULL After insertion: . . . A C B q

15 Recursive Function to Insert an Element in a List
#include “list.h” void insert(LINK p1, LINK p2, LINK q) { p1->next = q; /* insertion */ q->next = p2; } p1 p2 . . . A C B q

16 Deleting an Element from a List
p->next = q->next; p q . . . A B C p->next prior to delete. p->next after the delete. Note that the deleted node is now garbage (of no use). Its storage can be returned to the system by using the standard library function free().

17 Recursive Deletion of a List
#include “list.h” void delete_list(LINK head) { if (head != NULL) { delete_list(head->next); free(head); /* release storage */ }

18 Demonstration Structures
struct date_rec { int month; int day; int year; }; /* year is stored as yyyy */ struct friend_rec { char fname[15]; /* assume first name is stored in caps */ char phone[9]; /* local number only, such as */ struct date_rec birthday; }; struct my_friends { struct friend_rec a_friend; struct my_friends *next; . . . struct date_rec dob; struct friend_rec friend; struct my_friends *head, *current, *new;

19 Direct Input/Output The functions fread() and fwrite() are used to read and write binary files. No conversions are performed as they are with fscanf(). In certain applications (those that use structures), the use of these functions can save considerable time. Because one fread or fwrite can input or output an entire structure.


Download ppt "Lecture 25 Self-Referential Structures Linked Lists"

Similar presentations


Ads by Google