Linked Lists - PowerPoint PPT Presentation

1 / 16
About This Presentation
Title:

Linked Lists

Description:

In so doing, we are implementing the linked list as a concrete data type. ... You will then find the implementation a lot easier! ... – PowerPoint PPT presentation

Number of Views:59
Avg rating:3.0/5.0
Slides: 17
Provided by: ugrad2
Category:
Tags: domain | find | linked | list | lists | name | the

less

Transcript and Presenter's Notes

Title: Linked Lists


1
  • Linked Lists
  • Using dynamic memory allocation we can specify
    the capacity of an IntVector at run-time
  • It is expensive to increase the capacity of the
    IntVector
  • allocate memory to a larger array
  • copy the data from the original array to the new
    array
  • release the memory allocated to the original
    array
  • It is not possible to increase the capacity of
    the original array
  • A linked list will allow us to store data in a
    structure that can grow or shrink dynamically as
    needed

2
Singly linked lists A singly linked list is a
linear, sequential access, homogeneous data
structure Domain a collection of nodes each of
which contains a data element a pointer to a
node at the head of the list Structure there is
a pointer to the first node in the list and each
node contains a pointer that points to the next
node in the list, with the exception of the last
3
Operations insertFirst, insertLast,
insertAfter find deleteItem printNode
. . . not an exhaustive list We will now
implement a linked list toolkit a module that
contains the implementation of the operations
that we want to support. In so doing, we are
implementing the linked list as a concrete data
type. First we must determine how to represent a
node. This is a simple data structure so we will
implement it as a CDT using a struct. We will
assume that a typedef statement is used to define
the data type of the elements to be stored in the
list typedef int Item_type
4
typedef int Item_type struct Node
Item_type item Node next Now lets
suppose that we declare a pointer to a Node
object and dynamically allocate memory to a new
node Node nodePtr nodePtr new Node Recall
that we can now access the individual elements
within the node in two ways (nodePtr).item
2 nodePtr-gtnext NULL
5
When we insert a new element into a linked list,
we need to create a new node. We will start by
writing a helper function to do this Node
makeNode( const Item_type theItem, Node
nextNode 0 )//Post a new node is created
containing theItem the//new node is linked to
nextNode (or is null) a//pointer to the new
node is returnedNode newNode new Node
newNode-gtitem theItem newNode-gtnext
nextNode return newNode Note that in our
documentation, we do not specify what will happen
if our request for a new node fails due to
insufficient memory. We will address this
problem later in the course.
6
  • A note on formal parameter names
  • The following variant also works
  • Node makeNode( const Item_type item ,
    Node nextNode 0 )//Post a new node is
    created containing theItem the//new node is
    linked to nextNode (or is null) a//pointer to
    the new node is returnedNode newNode new
    Node
  • newNode-gt item item
  • newNode-gtnext nextNode
  • return newNode
  • In this case the compiler knows which is which
  • the first item is a field name in a Node struct
  • the second item is the formal parameter

7
We will now write functions to implement each of
the other operations. When working with linked
lists, draw a picture of what it is you are
trying to do. You will then find the
implementation a lot easier! insertFirst insert
an item into the front of the list Before
insertion Now insert newItem into the list
8
Implementing the insertFirst() function void
insertFirst( Node head, const Item_type
item )//Pre head points to the head of a list
or is null//Post item is inserted at the front
of the list Node newNode makeNode( item,
head ) head newNode Node for the 1st
parameter because we modify it! A more efficient
implementation is to simply write the body of the
function like this head makeNode( item,
head )
9
insertLast insert an item at the end of the
list Before insertion Now insert newItem at
end of list
10
Implementing the insertLast() function void
insertLast( Node head, const Item_type
item )//Pre head points to the head of a list
or is null//Post item is inserted at the end of
the list Node newNode makeNode( item )
if( head NULL ) head newNode
else Node cursor head
while( cursor-gtnext ! NULL ) cursor
cursor-gtnext cursor-gtnext newNode

11
Implementing the find() function Node find(
Node head, const Item_type item )//Pre
head points to the head of a list or is
null//Post if item is in the list, a pointer to
the node//containing it is returned otherwise a
null pointer//is returned Node cursor
head while( cursor ! NULL cursor-gtitem
! item ) cursor cursor-gtnext
return cursor While-loop relies on shortstop
evaluation of Boolean
12
deleteItem deletes an item from the list Before
deletion of item2 After deleting item2
delNode
head
item1
item2
item3
13
Special case deletes an item from the front of
the list Before deletion of item1 After
deleting item1
head
item1
item2
item3
head
item1
item2
item3
14
Special case delete an item from the end of the
list Before deletion of item3 After deleting
item3
head
item1
item2
item3
head
item1
item2
item3
15
Special case delete only item from the
list Before deletion of item1 After deleting
item1
head
item1
head
item1
16
Implementing the deleteItem() function bool
deleteItem( Node head, const Item_type
item )//Pre head points to the head of a list
or is null//Post if item is in the list it has
been deleted//and true returned otherwise false
returned Node cursor head Node
previousNode NULL while( cursor ! NULL
cursor-gtitem ! item ) previousNode
cursor cursor cursor-gtnext
if( cursor NULL ) // item not found
return false else if(
previousNode NULL ) // item is at front of
list head cursor-gtnext
else previousNode-gtnext
cursor-gtnext delete cursor
return true
Write a Comment
User Comments (0)
About PowerShow.com