LINKED LISTS - PowerPoint PPT Presentation

1 / 65
About This Presentation
Title:

LINKED LISTS

Description:

Discover how to build and manipulate a linked list. Learn how to construct a doubly linked list ... The list is printed only if the pointer to the list is not NULL ... – PowerPoint PPT presentation

Number of Views:92
Avg rating:3.0/5.0
Slides: 66
Provided by: charlyn6
Category:
Tags: linked | lists | build | construct | how | to

less

Transcript and Presenter's Notes

Title: LINKED LISTS


1
Chapter 17
  • LINKED LISTS

2
Objectives
  • 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
Limitations
  • Storing and processing data in an array has
    limitations
  • Array size is fixed
  • 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

4
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

5
Linked Lists
  • 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

6
Linked Lists
  • 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

7
Linked List 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

8
(No Transcript)
9
Linked List Navigation
  • current head

10
Linked List Navigation
  • current current-link

11
(No Transcript)
12
Insertion
  • Consider the following linked list
  • A new node with info 50 is to be created and
    inserted after p

13
First Part of Insertion
  • newNode new nodeType //create newNode
  • newNode-info 50 //store 50 in the new node

14
Tying New Node to List
  • newNode-link p-link
  • p-link newNode
  • newNode-link p-link

15
Completing the List
  • p-link newNode

16
Dangling List
  • 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

17
Simplifying Insertion
  • The following two statements insert newNode
    between p and q
  • newNode-link q
  • p-link newNode
  • Execution order does not matter

18
Reversed Order
  • If the statements are executed in the following
    order
  • p-link newNode
  • newNode-link q
  • After the statement p-link newNode

19
Completing an Insert
  • newNode-link q

20
Dangling Node Deletion
  • Suppose the node with info 34 is to be deleted
    from the list

21
Properly Deleting a Node
  • After the statement p-link p-link-link
  • The resulting figure is
  • The node with info 34 is removed from the list
  • The memory is still occupied by this node, that
    is, this node is dangling

22
Properly Deleting a Node
  • The following statements delete the node and
    deallocate the memory
  • q p-link
  • p-link q-link
  • delete q
  • After the statement q p-link

23
Properly Deleting a Node
  • After the statement p-link q-link the
    resulting figure is

24
Properly Deleting a Node
  • After the statement delete q the resulting
    figure is

25
Deletion in the Link List ADT
  • The basic operations on linked lists are
  • 1. Initialize the list
  • 2. Check if the list is empty
  • 3. Check if the list is full
  • 4. Search the list for a given item
  • 5. Insert an item in the list
  • 6. Delete an item from the list
  • 7. Destroy the list
  • 8. Print the list

26
Deletion in the Link List ADT
  • We also include the following operation on linked
    lists, which is helpful if we want to check the
    info of the first node
  • 9. Retrieve the info contained in the first node

27
Deletion in the Link List ADT
  • Case 1 List is empty
  • If the list is empty, output an error message
  • Case 2 List is not empty
  • The node to be deleted is the first node
  • After deletion, the list becomes empty.
    Therefore, after deletion, both first and last
    are set to NULL.

28
Deletion in the Link List ADT
  • Case 2 List is not empty
  • The node to be deleted is the first node
  • Consider the list of more than one node, as shown
    in Figure 17-28

29
Deletion in the Link List ADT
30
Deletion in the Link List ADT
  • Case 3 Node to be deleted is not the first node,
    but is somewhere in this list
  • Case 3a Node to be deleted is not the last node

31
Deletion in the Link List ADT
32
Deletion in the Link List ADT
  • Case 3b Node to be deleted is the last node

33
Insertion
  • Case 1 The list is empty

34
Insertion
  • Case 2 The list is not empty, and the item to be
    inserted is smaller than the smallest item in the
    list

35
Insertion
  • Case 3 The list is not empty, and the item to be
    inserted is larger than the first item in the
    list
  • Case 3a The item to be inserted is larger than
    the largest item in the list that is, it goes at
    the end of the list

36
Insertion
37
Insertion
  • Case 3b The item to be inserted goes somewhere
    in the middle of the list

38
Print List in Reverse Order
  • Suppose that current is a pointer to a linked
    list
  • if(current ! NULL)
  • reversePrint(current-link) //print the
    tail
  • coutinfonode
  • Here the base case is hidden. The list is printed
    only if the pointer to the list is not NULL
  • Inside the if statement the recursive call is on
    the tail of the list
  • There are statements after the recursive call, so
    when the transfer comes back to the calling
    function, we must execute the remaining statements

39
(No Transcript)
40
Doubly Linked Lists
  • A doubly linked list is a linked list in which
    every node has a next and a back pointer
  • A doubly linked list can be traversed in either
    direction. We can traverse the list starting at
    the first node or if a pointer to the last node
    is given, we can traverse the list starting at
    the last node

41
Doubly Linked List Operations
  • 1. Initialize the list
  • 2. Destroy the list
  • 3. Check whether the list is empty
  • 4. Check whether the list is full
  • 5. Search the list for a given item
  • 6. Insert an item in the list
  • 7. Delete an item from the list
  • 8. Find the length of the list
  • 9. Print the list

42
insertNode
  • Four cases
  • 1. Insertion in an empty list
  • 2. Insertion at the beginning of a nonempty list
  • 3. Insertion at the end of a nonempty list
  • 4. Insertion somewhere in a nonempty list

43
Delete Node
  • The delete operation has several cases
  • 1. The list is empty
  • 2. The item to be deleted is in the first node of
    the list, which would require us to change the
    value of the pointer first
  • 3. The item to be deleted is somewhere in the
    list
  • 4. The item to be deleted is not in the list

44
Deletion Somewhere In List
  • Case 3. Consider the list shown in Figure 17-47

45
Deletion Somewhere In List
List after deleting 17 is shown in Figure 17-49
46
Programming Example
  • For a family or an individual, a favorite place
    to go on weekends or holidays is to a video store
    to rent movies
  • 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 function

47
Programming Example
  • The program should be able to perform the
    following operations
  • 1. Rent a video that is, check out a video
  • 2. Return, or check in, a video
  • 3. Create a list of videos owned by the store
  • 4. Show the details of a particular video
  • 5. Print a list of all videos in the store
  • 6. Check whether a particular video is in the
    store
  • 7. Maintain a customer database
  • 8. Print a list of all the videos rented by each
    customer

48
Programming Example
  • From the programming requirement, we see that
    there are two major components of the video
    store
  • a video
  • a customer
  • Also, we need to maintain three lists
  • 1. A list of all videos in the store
  • 2. A list of all customers of the store
  • 3. A list of rented videos

49
Part 1 The Video Component
  • The common things associated with a video are
  • 1. Name of the movie
  • 2. Names of the stars
  • 3. Name of the producer
  • 4. Name of the director
  • 5. Name of the production company
  • 6. Number of copies in store

50
(No Transcript)
51
(No Transcript)
52
Video List
  • This program requires us to maintain a list of
    all videos in the store, and we should be able to
    add a new video to our list
  • We use a linked list to create a list of videos

53
Video List
The node of a video list has the form shown in
Figure 17-54
54
(No Transcript)
55
(No Transcript)
56
Part 2 Customer Component
  • The customer object stores information about a
    customer, such as first name, last name, account
    number, and a list of videos rented by the
    customer
  • Thus, our video store program has two
    components
  • 1. A video object that contains the necessary
    information about a video as described
    previously
  • 2. A customer object that contains the necessary
    information about a customer, which will be
    described soon

57
Part 2 Customer Component
  • We will create two lists that correspond to these
    two objects
  • a. A list of all videos in the store (as
    described earlier)
  • b. A list of all customers

58
Part 2 Customer Component
  • The primary characteristics of a customer are
  • 1. The customers first name
  • 2. The customers last name
  • 3. The customers account number
  • 4. The list of rented videos

59
(No Transcript)
60
Basic Operations
  • The basic operations on an object of the type
    personType are
  • 1. Print the name
  • 2. Set the name
  • 3. Show the first name
  • 4. Show the last name

61
Basic Operations
  • The basic operations on an object of the type
    customerType are
  • 1. Print the name, account number, and number of
    rentals
  • 2. Set the name, account number, and number of
    rentals
  • 3. Rent a video, add video to the list of rented
    video list
  • 4. Return a video, delete video from the list of
    rented video list
  • 5. Show account number

62
Main Program
  • The data in the input file is in the form
  • video title (that is, name of the movie)
  • movie star1
  • movie star2
  • movie producer
  • movie director
  • movie production co.
  • number of copies
  • .
  • .
  • .

63
Algorithm of the Main Function
  • 1. Open the input file, if input file does not
    exist, exit the program
  • 2. Create the list of videoscreateVideoList
  • 3. Show the menudisplayMenu
  • 4. While not done, perform various operations

64
CreateVideoList
  • a. Read the data and store it in a video object
  • b. Insert the video in the list
  • c. Repeat steps a and b for each videos data in
    the file

65
displayMenu
  • a. Select one of the following
  • b. 1 To check whether a particular video is in
    the store
  • c. 2 To check out a video
  • d. 3 To check in a video
  • e. 4 To check whether a particular video is in
    the store
  • f. 5 To print the titles of all videos
  • g. 6 To print a list of all videos
  • h. 9 To exit
Write a Comment
User Comments (0)
About PowerShow.com