lecture24-LL-mar27 - Announcements Lecture 24 Exam 2 in a...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
Announcements Lecture 24 • Exam 2 in a week • Assignment 5 – Designs due today in recitation –Q&A • Topics for today Pointers - advanced uses Linked lists – another way to represent a list Function pointers Linked Lists • Dynamic storage allocation is useful for building lists, trees, graphs, and other linked dynamic data structures. • A linked structure consists of a collection of nodes. Each node contains one or more pointers to other nodes. In C, a node can be represented by a structure. • The simplest linked structure is the linked list, which consists of a chain of nodes, with each node pointing to the next node in the chain. • A node in a linked list might have the following definition: struct node { int data; struct node *next; } a, b; – The use of a struct tag is mandatory, since the node structure contains a reference to itself. ab a.next = &b; Linked Lists (2) • An ordinary pointer variable points to the first node – to indicate an empty list, the variable is assigned a NULL struct node *first = NULL; • Nodes can be created by calling malloc: struct node *temp; temp = malloc(sizeof(struct node)); • The . operator can be used to select a member (or field) in the node that temp points to inorder to set it: (*temp).data = n; (*temp).next = NULL; first = temp; • Because pointers often point to structures, C provides a special notation (the right arrow selection operator -> ) for selecting members of these structures: temp -> data = n; /* means the same as (*temp).data = n; */ n temp first data next Example 1
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

lecture24-LL-mar27 - Announcements Lecture 24 Exam 2 in a...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online