11. linked list

11. linked list - ENGG1002 Computer Programming and...

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

View Full Document Right Arrow Icon
ENGG1002 Computer Programming and Applications Prepared by Dr. K.K.Y. Wong
Background image of page 1

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

View Full DocumentRight Arrow Icon
Listed Lists Linked lists A fundamental data structure Consists of a sequence of nodes , each containing an arbitrary number of data fields and one or more references ( links ) to other nodes Allow insertion and removal of nodes at any point in the list in constant time Do NOT allow random access 2 data data data × data A linked list a node a reference to another node
Background image of page 2
Linked Lists in C++ A linked list can be implemented in C++ using pointers and structures (or classes ) A node is defined using structure , with one or more member variables being pointer variables pointing to the same structure type , e.g., 3 struct Node { string title; int year; Node* next; } typedef Node* NodePtr;
Background image of page 3

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

View Full DocumentRight Arrow Icon
Building a Linked List In order to access a linked list, a pointer variable often called head is used to store a pointer to the first node of the list, e.g., Initially when the linked list is empty (i.e., a linked list with no nodes), head will simply contain a null pointer 4 NodePtr head; head = NULL; head data data data NULL head
Background image of page 4
Building a Linked List Starting from an empty list, new nodes may be created and inserted into the linked list A null pointer is assigned the pointer variable of the last node to indicate the end of the linked list Similarly, nodes may be removed from the linked list and destroyed Both node insertion and removal may take place at the head or any other position of the linked list 5 head NULL data
Background image of page 5

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

View Full DocumentRight Arrow Icon
To insert a node at the head 1. Create a new node using the new operator
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/26/2011 for the course BENG 1002 taught by Professor Dr.x during the Winter '10 term at HKU.

Page1 / 20

11. linked list - ENGG1002 Computer Programming and...

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

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