Linked List Implementation - PowerPoint PPT Presentation

About This Presentation
Title:

Linked List Implementation

Description:

... class ... number of entries in the List, the function succeeds: Any entry ... Insertion Method. 3. Insertion into a Linked List. 1. previous ... – PowerPoint PPT presentation

Number of Views:17
Avg rating:3.0/5.0
Slides: 15
Provided by: liw87
Category:

less

Transcript and Presenter's Notes

Title: Linked List Implementation


1
Linked List Implementation
  1. template ltclass Node_entrygt
  2. deftype char Node_entry
  3. mutable
  4. void set_position(int position) const

2
set_position method
template ltclass List_entrygt NodeltList_entrygt
ListltList_entrygtset_position(int position)
const / Pre position is a valid position in the
List 0 lt position lt count . Post Returns a
pointer to the Node in position . /
NodeltList_entrygt q head for ( int i 0 i
lt position i ) q q-gtnext return q
q-gtnext
head
i0
iposition-1
count-1
Why cannot we point this position directly?
3
Insertion Method
template ltclass List_entrygt Error_code
ListltList_entrygtinsert(int position, const
List_entry x) / Post If the List is not full
and 0 position n, where n is the number of
entries in the List, the function succeeds Any
entry formerly at position and all later entries
have their position numbers increased by 1, and x
is inserted at position of the List. Else The
function fails with a diagnostic error code.
/ if (position lt 0 position gt count)
return range_error NodeltList_entrygt
new_node, previous, following if (position
gt 0) previous set_position(position -
1) following previous-gtnext
else following head new_node new
NodeltList_entrygt(x, following) if (new_node
NULL) return overflow if (position
0) head new_node else
previous-gtnext new_node count return
success
4
Insertion into a Linked List
Localization previous and following pointers
position lt 0 or position gtcount wrong range
position gt0
1. previous set_position(position - 1)
2. following previous-gtnext
position 0
following head
head
5
Insertion into a Linked List
Insertion new_node pointer
position gt0
x
2. previous-gtnext new_node
x
1. new_node new NodeltList_entrygt(x, following)
position 0
x
head
x
1. new_node new NodeltList_entrygt(x, following)
2. head new_node
6
new_node, previous, following disappear! Why?
Increase counter by 1
position gt0
x
head
position 0
x
7
set_position method
template ltclass List_entrygt class List
public // Add specifications for the methods
of the list ADT. // Add methods to replace the
compiler-generated defaults. protected // Data
members for the linked-list implementation with
current position follow int count mutable
int current_position NodeltList_entrygt head
mutable NodeltList_entrygt current // Auxiliary
function to locate list positions follows
void set_position(int position) const
template ltclass List_entrygt void ListltList_entrygt
set_position(int position) const / Pre
position is a valid position in the List 0 ?
position lt count . Post The current Node pointer
references the Node at position . / if
(position lt current_position) // must start
over at head of list current_position 0
current head for ( current_position !
position current_position ) current
current-gtnext
8
Why do we declare current, and current_position?
Why can we change set_function type from int to
void?
position lt current_position
head
current_position
position
position gt current_position
head
position
current_position
9
include ltiostream.hgt int get_result(int a, int
b, int (compare)(int, int))
return(compare(a, b)) // Invoke the function
passed int max(int a, int b) cout ltlt"
In max"ltltendl return((a gt b) ? a b) int
min(int a, int b) cout ltlt" In min"ltltendl
return((a lt b) ? a b) void main(void)
int result result get_result(1, 2, max)
cout ltlt "Max of 1 and 2 is\t"ltlt resultltltendl
result get_result(1, 2, min) cout ltlt "Min
of 1 and 2 is\t"ltlt resultltltendl
10
int (compare)(int, int))
compare
600000
500000
int min(int a, int b)
int max(int a, int b)
11
int max(int a, int b)
compare
memory
600000
result get_result(1, 2, max)
int get_result(int a, int b, int (compare)(int,
int)) return(compare(a, b)) // Invoke the
function passed
max(1, 2)
int max(int a, int b) cout ltlt" In
max"ltltendl return((a gt b) ? a b)
12
int min(int a, int b)
compare
memory
500000
result get_result(1, 2, min)
int get_result(int a, int b, int (compare)(int,
int)) return(compare(a, b)) // Invoke the
function passed
min(1, 2)
int min(int a, int b) cout ltlt" In
min"ltltendl return((a gt b) ? a b)
13
void write_ent(char x)
template ltclass List_entrygt void
ListltList_entrygttraverse(void
(visit)(List_entry )) / Post The action
specified by function f has been performed on
every entry of the List, beginning at position 0
and doing each in turn. / NodeltList_entrygt
to_visit for (to_visit head
to_visit!NULL to_visit to_visit-gtnext)
(visit)(to_visit-gtentry)
write_ent(to_visit-gtentry)
test_list.traverse(write_ent)
head
b
a
14
// auxiliary input/output functions void
write_ent(char x) cout ltlt x char
get_char() char c cin gtgtc return c
Write a Comment
User Comments (0)
About PowerShow.com