C Programming: From Problem Analysis to Program Design, Second Edition PowerPoint PPT Presentation

presentation player overlay
1 / 81
About This Presentation
Transcript and Presenter's Notes

Title: C Programming: From Problem Analysis to Program Design, Second Edition


1
C Programming From Problem Analysis to
Program Design, Second Edition
0
  • Chapter 17 Linked Lists

2
Objectives
0
  • 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

3
Introduction
0
  • Data can be organized and processed sequentially
    using an array, called a sequential list
  • Problems with an array
  • Array size is fixed
  • Unsorted array searching for an item is slow
  • Sorted array insertion and deletion is slow

4
Linked Lists
0
  • Linked list collection of components (nodes)
  • Every node (except last) has address of next node
  • Every node has two components
  • Address of the first node of the list is stored
    in a separate location, called head or first

5
A Linked List
0
  • a linked list is a list in which the order of the
    components is determined by an explicit link
    member in each node
  • the nodes are structs--each node contains a
    component member and also a link member that
    gives the location of the next node in the list

6
Dynamic Linked List
0
  • in a dynamic linked list, nodes are linked
    together by pointers, and an external pointer (or
    head pointer) points to the first node in the
    list

7
Nodes can be located anywhere in memory
0
  • the link member holds the memory address of the
    next node in the list

head
8
Declarations for a Dynamic Linked List
0
  • // Type DECLARATIONS
  • struct NodeType
  • char info
  • NodeType link
  • // Variable DECLARATIONS
  • NodeType head
  • NodeType ptr

8
9
Pointer Dereferencing and Member Selection
0
A 6000
ptr
9
10
ptr is a pointer to a node
0
A 6000
ptr
10
11
ptr is the entire node pointed to by ptr
0
A 6000
. info . link
ptr
11
12
ptr-gtinfo is a node member
0
A 6000
ptr-gtinfo (ptr).info //
equivalent
12
13
ptr-gtlink is a node member
0
A 6000
ptr-gtlink (ptr).link // equivalent
13
14
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
15
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
16
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
17
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
18
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
19
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
20
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
21
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
22
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
23
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
24
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
25
Traversing a Dynamic Linked List
0
//PRE head points to a dynamic linked list
ptr head while (ptr ! NULL) cout ltlt
ptr-gtinfo // Or, do something else with
node ptr ptr ptr-gtlink
26
Using Operator new
0
  • If memory is available in an area called the free
    store (or heap), operator new allocates the
    requested object, and returns a pointer to the
    memory allocated.
  • The dynamically allocated object exists until the
    delete operator destroys it.

26
27
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

28
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

location
29
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

location
30
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

B
location
31
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

B
location
32
Inserting a Node at the Front of a List
0
B
item
  • char item B
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • location-gtlink head
  • head location

X C L
B
location
33
Using Operator delete
0
  • The object currently pointed to by the pointer is
    deallocated, and the pointer is considered
    undefined. The objects memory is returned to
    the free store.

33
34
Deleting the First Node from the List
0
item
  • NodeType tempPtr
  • item head-gtinfo
  • tempPtr head
  • head head-gtlink
  • delete tempPtr

B X C L
tempPtr
35
Deleting the First Node from the List
0
B
item
  • NodeType tempPtr
  • item head-gtinfo
  • tempPtr head
  • head head-gtlink
  • delete tempPtr

B X C L
tempPtr
36
Deleting the First Node from the List
0
B
item
  • NodeType tempPtr
  • item head-gtinfo
  • tempPtr head
  • head head-gtlink
  • delete tempPtr

B X C L
tempPtr
37
Deleting the First Node from the List
0
B
item
  • NodeType tempPtr
  • item head-gtinfo
  • tempPtr head
  • head head-gtlink
  • delete tempPtr

B X C L
tempPtr
38
Deleting the First Node from the List
0
B
item
  • NodeType tempPtr
  • item head-gtinfo
  • tempPtr head
  • head head-gtlink
  • delete tempPtr

X C L
tempPtr
39
What is a List?
0
  • a list is a varying-length, linear collection of
    homogeneous elements
  • linear means each list element (except the first)
    has a unique predecessor, and each element
    (except the last) has a unique successor

40
struct NodeType
0
  • // SPECIFICATION FILE DYNAMIC-LINKED SORTED
    LIST( slist2.h )
  • struct NodeType
  • char item // Data (char to keep
    example simple)
  • NodeType link // link to next node in
    list

40
41
0
  • // SPECIFICATION FILE DYNAMIC-LINKED SORTED
    LIST( slist2.h )
  • class SortedList2
  • public
  • bool IsEmpty ( ) const
  • void Print ( ) const
  • void InsertTop ( / in / char item )
  • void Insert ( / in / char item )
  • void DeleteTop ( / out / char
    item )
  • void Delete ( / in / char item )
  • SortedList2 ( ) // Constructor

41
42

0
class SortedList2
SortedList2
Private data head
SortedList2
IsEmpty
Print
InsertTop
Insert
DeleteTop
Delete
43
Insert Algorithm
0
  • what will be the algorithm to Insert an item into
    its proper place in a sorted linked list?
  • that is, for a linked list whose elements are
    maintained in ascending order?

44
Insert algorithm for SortedList2
0
  • find proper position for the new element in the
    sorted list using two pointers prevPtr and
    currPtr, where prevPtr trails behind currPtr
  • obtain a node for insertion and place item in it
  • insert the node by adjusting pointers

45
Implementing SortedList2Member Function Insert
0

// DYNAMIC LINKED LIST IMPLEMENTATION
(slist2.cpp) void SortedList2 Insert ( /
in / char item ) // PRE item is assigned
List components in ascending order // POST
item is in List List components in ascending
order . . .
46

0
Inserting S into a Sorted List
prevPtr currPtr
Private data head
47

0
Finding Proper Position for S
prevPtr currPtr
NULL
Private data head
48

0
Finding Proper Position for S
prevPtr currPtr
Private data head
49

0
Finding Proper Position for S
prevPtr currPtr
Private data head
50

0
Inserting S into Proper Position
prevPtr currPtr
Private data head
C L X
51
0
  • // IMPLEMENTATION DYNAMIC-LINKED SORTED LIST
    (slist2.cpp)
  • SortedList2 SortedList2 ( ) //
    Constructor
  • // Post head NULL
  • head NULL
  • SortedList2 SortedList2 ( ) // Destructor
  • // Post All linked nodes deallocated
  • char temp
  • // keep deleting top node
  • while ( !IsEmpty )
  • DeleteTop ( temp )

51
52
  • void SortedList2 Insert( / in / char item
    )
  • // Pre item is assigned list components in
    ascending order
  • // Post new node containing item is in its
    proper place
  • // list components in ascending order
  • NodeType currPtr
  • NodeType prevPtr
  • NodeType location
  • location new NodeType
  • location-gtinfo item
  • prevPtr NULL
  • currPtr head
  • while ( currPtr ! NULL item gt
    currPtr-gtinfo )
  • prevPtr currPtr // advance both
    pointers
  • currPtr currPtr-gtlink
  • location-gtlink currPtr // insert new node
    here
  • if ( prevPtr NULL )
  • head location
  • else

0
52
53
0
  • void SortedList2 DeleteTop ( / out / char
    item )
  • // Pre list is not empty list elements
    in ascending order
  • // Post item element of first list node _at_
    entry
  • // node containing item is no longer
    in linked list
  • // list elements in ascending order
  • NodeType tempPtr head
  • // obtain item and advance head
  • item head-gtinfo
  • head head-gtlink
  • delete tempPtr

53
54
  • void SortedList2 Delete ( / in / char
    item )
  • // Pre list is not empty list
    elements in ascending order
  • // item component member of some
    list node
  • // Post item element of first list node _at_
    entry
  • // node containing first occurrence
    of item is no longer
  • // in linked list list
    elements in ascending order
  • NodeType delPtr
  • NodeType currPtr // Is item in first
    node?
  • if ( item head-gtinfo )
  • delPtr head // If so, delete first node
  • head head-gtlink
  • else // search for item in rest of list
  • currPtr head
  • while ( currPtr-gtlink-gtinfo ! item )
  • currPtr currPtr-gtlink
  • delPtr currPtr-gtlink
  • currPtr-gtlink currPtr-gtlink-gtlink

54
55

class SortedList2
SortedList2
Private data head
SortedList2
IsEmpty
Print
InsertTop
Insert
DeleteTop
Delete
56
Another Linked List
  • Linked list above has four nodes
  • Address of first node is stored in head
  • Each node has two components
  • Info stores the info
  • Link stores address of the next node

57
(No Transcript)
58
Doubly Linked Lists
  • Doubly linked list every node has next and back
    pointers
  • Can be traversed in either direction

59
Struct for Doubly Linked List
  • struct NodeType
  • int item // Data
  • NodeType next // link to next node in list
  • NodeType back // link to previous node
    in list

60
Delete a Node
  • Consider the list shown in the figure below

61
(No Transcript)
62
Programming Example
  • A new video store in your neighborhood is about
    to open
  • However, it does not have a program to keep track
    of its videos and customers
  • The store managers want someone to write a
    program for their system so that the video store
    can operate

63
Programming Example (continued)
  • The program should be able to perform the
    following operations
  • Rent a video that is, check out a video
  • Return, or check in, a video
  • Create a list of videos owned by the store
  • Show the details of a particular video
  • Print a list of all videos in the store
  • Check whether a particular video is in the store
  • Maintain a customer database
  • Print list of all videos rented by each customer

64
Programming Example (continued)
  • There are two major components of the video
    store
  • A video
  • A customer
  • You need to maintain two lists
  • A list of all videos in the store
  • A list of all customers of the store

65
Part 1 Video Component (continued)
  • The common things associated with a video are
  • Name of the movie
  • Names of the stars
  • Name of the producer
  • Name of the director
  • Name of the production company
  • Number of copies in store

66
(No Transcript)
67
(No Transcript)
68
Video List
  • This program requires us to
  • Maintain a list of all videos in the store
  • Add a new video to our list
  • Use a linked list to create a list of videos

69
(No Transcript)
70
(No Transcript)
71
Part 2 Customer Component
  • Primary characteristics of a customer
  • First name
  • Last name
  • Account number
  • List of rented videos

72
(No Transcript)
73
Basic Operations
  • Basic operations on personType
  • Print the name
  • Set the name
  • Show the first name
  • Show the last name

74
Basic Operations (continued)
  • Basic operations on an object of the type
    customerType are
  • Print name, account , and number of rentals
  • Set name, account , and number of rentals
  • Rent a video, add video to the list
  • Return a video, delete video from the list
  • Show account

75
Main Program
  • The data in the input file is in the form
  • video title
  • movie star1
  • movie star2
  • movie producer
  • movie director
  • movie production co.
  • number of copies
  • .
  • .
  • .

76
Algorithm of the Main Function
  • Open the input file, exit if not found
  • createVideoList create the list of videos
  • displayMenu show the menu
  • While not done, perform various tasks

77
CreateVideoList
  1. Read data and store in a video object
  2. Insert video in list
  3. Repeat steps 1 and 2 for each video in file

78
displayMenu
  • Select one of the following
  • Check whether a particular video is in the store
  • Check out a video
  • Check in a video
  • Check whether a particular video is in the store
  • Print the titles of all videos
  • Print a list of all videos
  • Exit

79
Summary
  • A linked list is a list of items (nodes)
  • Order of the nodes is determined by the address,
    called a link, stored in each node
  • The pointer to a linked list is called head or
    first
  • A linked list is a dynamic data structure
  • The list length is the number of nodes

80
Summary
  • Insertion and deletion does not require data
    movement only the pointers are adjusted
  • A (single) linked list is traversed in only one
    direction
  • Search of a linked list is sequential
  • The head pointer is fixed on first node
  • Traverse use a pointer other than head

81
Summary
  • Doubly linked list
  • Every node has two links next and previous
  • Can be traversed in either direction
  • Item insertion and deletion require the
    adjustment of two pointers in a node
Write a Comment
User Comments (0)
About PowerShow.com