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

Info icon This 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. 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
Image of page 1

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern