Data Structures - PowerPoint PPT Presentation

1 / 55
About This Presentation
Title:

Data Structures

Description:

Title: Data Structures Author: Andreas Savva Last modified by: easy Created Date: 2/9/2004 9:04:48 PM Document presentation format: On-screen Show Company – PowerPoint PPT presentation

Number of Views:38
Avg rating:3.0/5.0
Slides: 56
Provided by: Andreas184
Category:

less

Transcript and Presenter's Notes

Title: Data Structures


1
Data Structures
Andreas Savva
  • Chapter 6
  • Linked Lists

2
Lists
  • Like a stack or a queue, a list has a sequence of
    entries as its data values.
  • However, unlike a stack or a queue, a list
    permits operations that alter arbitrary entries
    of the sequence.

A linked list
3
List Definition
  • A list of elements of type T is a finite
    sequence of elements of T together with the
    following operations
  • Construct the list, leaving it empty.
  • Determine whether the list is empty or not.
  • Determine whether the list is full or not.
  • Find the size of the list.
  • Clear the list to make it empty.
  • Insert an entry at a specified position of the
    list.
  • Remove an entry at a specified position in the
    list.
  • Retrieve the entry from a specified position in
    the list.
  • Replace the entry at a specified position in the
    list.
  • Traverse the list, performing a given operation
    on each entry.

4
A Simple Linked List implementation
struct List // data members List_entry
head List tail // constructors
List() List(List_entry item, List lst
NULL) ListList() tail
NULL ListList(List_entry item, List lst)
head item tail lst
5
List Functions
List cons(List_entry x, List lst) List m
new List(x, lst) if (m NULL)
cout ltlt Memory full ltlt endl return lst
else return m
List_entry head(List lst) return
lst-gthead
List tail(List lst) return lst-gttail
6
Insert - Sorted
List insert(List_entry x, List lst) if
(lst NULL) return cons(x, NULL)
else if (x lt head(lst)) return cons(x,
lst) else return cons(head(lst),
insert(x, tail(lst)))
insert(5, 1,3,4,7,9,12)
7
Insert
insert(5, 1,3,4,7,9,12)
8
Insert - Garbage
insert(5, 1,3,4,7,9,12) cons(1, insert(5,
3,4,7,9,12)) cons(1, cons(3, insert(5,
4,7,9,12))) cons(1, cons(3, cons(4, insert(5,
7,9,12)))) cons(1, cons(3, cons(4, cons(5,
7,9,12)))) cons(1, cons(3, cons(4,
5,7,9,12))) cons(1, cons(3, 4,5,7,9,12)) cons(
1, 3,4,5,7,9,12) 1,3,4,5,7,9,12
9
Insert - Sorted
List insert(List_entry x, List lst) if
(lst NULL) return cons(x, NULL)
else if (x lt head(lst)) return cons(x,
lst) else List temp lst
return cons(head(lst), insert(x,
tail(lst))) delete temp
insert(5, 1,3,4,7,9,12)
10
Exercises
  • Write the following functions
  • List remove(List_item x, List l)
  • int length(List l)
  • bool isin(List_item x, List l)
  • int nooftimes(List_item x, List l)
  • List append(List l, List t)
  • List reverse(List l)
  • void print(List l)

11
Class Templates
  • A C template construction allows us to write
    code to implement a class, that uses objects of
    an arbitrary, generic type.
  • In template code we utilize a parameter enclosed
    in angles brackets ltgt to denote the generic
    type.

Listltintgt first_list Listltchargt second_list
12
The Node Structure
  • template ltclass Node_entrygt
  • struct Node
  • Node_entry entry
  • NodeltNode_entrygt next
  • Node ()
  • Node(Node_entry item, NodeltNode_entrygt n
    NULL)
  • template ltclass Node_entrygt
  • NodeltNode_entrygtNode()
  • next NULL
  • template ltclass Node_entrygt
  • NodeltNode_entrygtNode(Node_entry item,
    NodeltNode_entrygt n)
  • entry item
  • next n

13
Sorted Linked Lists
  • Lists are sequential (visit one node at a time)
  • Sequential sorting

Examples of sorted linked lists
14
The List Class
  • template ltclass List_entrygt
  • class List
  • public
  • List()
  • void clear()
  • bool empty() const
  • int size() const
  • Error_code remove(const List_entry item)
  • Error_code insert(const List_entry item)
  • void print() const
  • // Safeguards
  • List()
  • List(const ListltList_entrygt copy)
  • void operator (const ListltList_entrygt
    original)
  • protected
  • NodeltList_entrygt head

15
Create List
Constructor
  • template ltclass List_entrygt
  • ListltList_entrygtList( )
  • head NULL

16
Empty
template ltclass List_entrygt bool
ListltList_entrygtempty() const return
head NULL
17
Size
template ltclass List_entrygt int
ListltList_entrygtsize() const int count
0 NodeltList_entrygt temp head
while (temp!NULL) count
temp temp-gtnext return count
count
1
0
2
3
4
5
18
Insert
Numbers.insert(4)
If (empty())
19
Insert (continue)
else
20
Insert
template ltclass List_entrygt Error_code
ListltList_entrygtinsert(const List_entry item)
NodeltList_entrygt new_entry new
NodeltList_entrygt(item) if (new_entry
NULL) return overflow // IF FULL else if
(empty()) head new_entry else if (item lt
head-gtentry) new_entry-gtnext
head head new_entry
else NodeltList_entrygt previous
head, current head-gtnext while
(current ! NULL) if (item lt
current-gtentry)
new_entry-gtnext current
break // Exit the loop
previous current
current current-gtnext
previous-gtnext new_entry return
success
21
Remove
Numbers.remove(4)
If (itemhead-gtentry)
22
Remove
template ltclass List_entrygt Error_code
ListltList_entrygtremove(const List_entry item)
NodeltList_entrygt current head if
(empty()) return underflow if (item lt
head-gtentry) return not_found if (item
head-gtentry) head head-gtnext
delete current return success
NodeltList_entrygt previous current
current current-gtnext while (current !
NULL) if (item lt current-gtentry)
break // Exit the loop if (item
current-gtentry) previous-gtnext
current-gtnext delete current
return success
else previous current
current current-gtnext
return not_found
23
Print
template ltclass List_entrygt void
ListltList_entrygtprint() const
NodeltList_entrygt temp head if
(empty()) cout ltlt "Empty List" ltlt
endl else while (temp ! NULL)
cout ltlt temp-gtentry ltlt endl
temp temp-gtnext
3 4 7 9 12
24
Clear List
template ltclass List_entrygt void
ListltList_entrygtclear() NodeltList_entrygt
temp while (head ! NULL)
temp head head head-gtnext
delete temp
NULL
25
Size - Recursive
template ltclass List_entrygt int
ListltList_entrygtsize_recursive(NodeltList_entrygt
lst) const if (lst NULL) return 0
else return 1 size_recursive(lst-gtnext)
template ltclass List_entrygt int
ListltList_entrygtsize() const return
size_recursive(head)
26
Print - Recursive
template ltclass List_entrygt void
ListltList_entrygtprint_recursive(NodeltList_entrygt
lst) const if (lst ! NULL)
cout ltlt lst-gtentry ltlt endl
print_recursive(lst-gtnext) template
ltclass List_entrygt void ListltList_entrygtprint()
const if (empty()) cout ltlt
"Empty List" ltlt endl else
print_recursive(head)
27
Unsorted Lists
  • A text editor example
  • Using position (cursor) for insert, remove,
    replace.

28
The List Class
  • template ltclass Entrygt
  • class List
  • public
  • . . . . .
  • void traverse(void(visit)(Entry ))
  • private
  • int count // Number of nodes in the list
  • NodeltEntrygt head
  • // Auxiliary function used to locate list
    position
  • NodeltEntrygt set_position(int position)
    const

29
Empty
template ltclass Entrygt bool ListltEntrygtempty()
const return count 0
30
Size
template ltclass Entrygt int ListltEntrygtsize()
const return count
31
Set Position
Position starts at 0
templateltclass Entrygt NodeltEntrygt
ListltEntrygtset_position(int position) const
NodeltEntrygt q head for (int i0
iltposition i) q q-gtnext
return q
Example current set_position(2)
0
1
2
3
4
i
1
0
2
32
Traverse
template ltclass Entrygt void ListltEntrygttraverse(
void (visit)(Entry )) NodeltEntrygt
current head for (int i0 iltcount
i) (visit)(current-gtentry)
current current-gtnext
33
Calling Traverse for Printing
void AddOne(int x) x void Print(int
x) cout ltlt x ltlt endl
void main( ) Listltintgt numbers . . .
. . numbers.traverse(AddOne)
numbers.traverse(Print)
34
Retrieve
template ltclass Entrygt Error_code
ListltEntrygtretrieve(int position, Entry x)
if (empty()) return underflow if
(position lt 0 position gt count) return
range_error NodeltEntrygt current
current set_position(position) x
current-gtentry return success
35
Insert
template ltclass Entrygt Error_code
ListltEntrygtinsert(int position, const Entry
x) if (position lt 0 position gt count)
return range_error NodeltEntrygt new_node,
previous, current if (position 0)
current head else previous
set_position(position - 1) current
previous-gtnext new_node new
NodeltEntrygt(x,current) if (new_node
NULL) return overflow if (position 0)
head new_node else previous-gtnext
new_node count return success
36
Insert
if (position0)
37
Remove
template ltclass Entrygt Error_code
ListltEntrygtremove(int position) if
(position lt 0 position gt count) return
range_error NodeltEntrygt previous,
current if (position 0) current
head else previous
set_position(position - 1) current
previous-gtnext if (position 0)
head head-gtnext else previous-gtnext
current-gtnext delete current
count-- return success
38
Remove
if (position0)
39
Replace
template ltclass Entrygt Error_code
ListltEntrygtreplace(int position, const Entry
x) if (position lt 0 position gt
count) return range_error NodeltEntrygt
current current set_position(position)
current-gtentry x return success
40
Keeping the Current Position
  • template ltclass Entrygt
  • class List
  • public
  • . . . . .
  • protected
  • int count
  • mutable int current_position
  • NodeltEntrygt head
  • mutable NodeltEntrygt current
  • void set_position(int position) const

Mutable data members of a class can be changed,
even by constant methods
41
Set Position
templateltclass Entrygt void ListltEntrygtset_positi
on(int position) const if (position lt
current_position) // start over at head of
list current_position 0
current head for (
current_position lt position current_position)
current current-gtnext
42
Doubly Linked Lists
  • In the last section the problem of moving
    backwards was solved by traversing the list from
    its beginning until the desired node was found,
    but this solution is generally unsatisfactory
    since
  • its programming is difficult, and
  • the running-time of the program will depend on
    the length of the list, which may be quite long.
  • Some applications of linked lists require that we
    move both forward and backward through the list.

43
Doubly Linked Lists
current_position
3
Class data members
Set Position for ( current_position lt
position current_position) current
current-gtnext for ( current_position gt
position current_position--) current
current-gtback
44
The Node Structure
template ltclass Entrygt struct Node Entry
entry NodeltEntrygt back NodeltEntrygt
next Node() Node(Entry item,
NodeltEntrygt b NULL,
NodeltEntrygt n NULL) template ltclass
Entrygt NodeltEntrygtNode() next NULL back
NULL template ltclass Entrygt NodeltEntrygtNod
e(Entry item, NodeltEntrygt b, NodeltEntrygt
n) entry item back b next n
45
The Class List
template ltclass Entrygt class List public
. . . . . protected int count mutable
int current_position mutable NodeltEntrygt
current void set_position(int position)
const
46
Set Position
templateltclass Entrygt void ListltEntrygtset_positi
on(int position) const if
(current_position lt position) for (
current_position lt position current_position)
current current-gtnext else
for ( current_position gt position
current_position--) current
current-gtback
47
Insert
templateltclass Entrygt Error_code
ListltEntrygtinsert(int position, const Entry x)
NodeltEntrygt new_node, following,
preceding if (position lt 0 position gt
count) return range_error if (position
0) if (empty()) following NULL
else set_position(0)
following current
preceding NULL else
set_position(position 1) preceding
current following preceding-gtnext
new_node new NodeltEntrygt(x,preceding,f
ollowing) if (new_node NULL) return
overflow if (preceding ! NULL)
preceding-gtnext new_node if (following !
NULL) following-gtback new_node current
new_node current_position position
count return success
48
Insert
thelist.insert(3,h)
0
1
2
3
4
49
Remove
templateltclass Entrygt void ListltEntrygtremove(int
position) NodeltEntrygt new_node,
following, preceding if (position lt 0
position gt count) return range_error
set_position(position) if (position 0)
preceding NULL else preceding
current-gtback if (position count-1)
following NULL else following
current-gtnext if (preceding ! NULL)
preceding-gtnext following if (following
! NULL) following-gtback preceding
delete current count-- if
(!empty()) if (following NULL)
current_position position 1
current preceding
else current following return
success
50
Remove
thelist.remove(2)
51
Circular Doubly Linked Lists
0
1
2
3
4
current_position
3
52
Linked Lists in Arrays
  • The important feature of dynamic allocation of
    memory does not exist.
  • Applications were linked lists in arrays may
    prove preferable are
  • the number of entries in a list are known in
    advance,
  • the links are frequently rearranged, but
    relatively few additions or deletions are made,
    and
  • the same data are sometimes best treated as a
    linked lists and other times as a contiguous list.

53
Linked Lists in Arrays
54
The Array and Stack of Available Space
0 Clark F 5
1 Smith A -1
2 -1
3 Garcia T 4
4 Hall W 1
5 Evans B 3
6 9
7 6
8 Arthur E 0
9 10
10 2
11
12
available
head
last_used
55
Contiguous Vs Linked Storage
  • Contiguous storage is generally preferable
  • when the entries are individually small
  • when the size of the list is known when the
    program is written
  • when few insertions or deletions need to be made
    except at the end of the list and
  • when random access is important.
  • Linked storage proves superior
  • when the entries are large
  • when the size of the list is not known in
    advance and
  • when flexibility is needed in inserting, deleting
    and rearranging the entries.
Write a Comment
User Comments (0)
About PowerShow.com