lecture24-LL-mar27

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

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

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. a b 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

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern