linklist - CHAPTER 16 LINKED LISTS In this chapter, you...

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

View Full Document Right Arrow Icon
CHAPTER 16 LINKED LISTS In this chapter, you will: Learn about linked lists Become aware of the basic properties of linked lists Explore the insertion and deletion operations on linked lists Discover how to build and manipulate a linked list Learn how to construct a doubly linked list Storing and processing data in an array has limitations. Array size is fixed. So only a fixed number of items can be stored in an array. Searching for an item in an array is time consuming if data is not sorted. If data is sorted, item insertion and deletion becomes time consuming. LINKED LISTS A linked list is a collection of components, called nodes. Every node (except the last node) contains the address of the next node. Every node in a linked list has two components. The address of the first node of the list is stored in a separate location, called head or first . Linked list: A list of items, called nodes , in which the order of the nodes is determined by the address, called the link , stored in each node. The down arrow in the last node indicates that this link field is NULL . The arrow in each node indicates that the address of the node to which it is pointing is stored in that node. struct nodeType { int info; nodeType *link; }; //variable declaration nodeType* head; Linked Lists: Some Properties • This linked list has four nodes. • The address of the first node is stored in the pointer head . • Each node has two components, a component, info , to store the info and another component, link , to store the address of the next node. • Assume that info is of the type int . • Suppose that the first node is at location 2000 , the second node is at location 2800 , the third node is at location 1500 , and the fourth node is at location 3600 . current = head; current = current->link;
Background image of page 1

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

View Full DocumentRight Arrow Icon
Traversing a Linked List current = head; while(current != NULL) { //Process current current = current->link; } The following code outputs the data stored in each node: current = head; while(current != NULL) { cout<<current->info<<" "; current = current->link; } Item Insertion and Deletion Assume info type is int . struct nodeType { int info; nodeType *link; }; nodeType *head, *p, *q, *newNode; Insertion Consider the following linked list. A new node with info 50 is to be created and inserted after p . newNode = new nodeType; //create newNode newNode->info = 50; //store 50 in the new node newNode->link = p->link; p->link = newNode; newNode->link = p->link; p->link = newNode; If we reverse the sequence of the statements, that is, suppose we execute the statements in the following order. p->link = newNode; newNode->link = p->link; The resulting figure is: With two pointers we can somewhat simplify the insertion code. q
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 04/18/2008 for the course CS 201 taught by Professor Markhieber during the Spring '08 term at University of Missouri-Kansas City .

Page1 / 32

linklist - CHAPTER 16 LINKED LISTS In this chapter, you...

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

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