CSCE 210 Data Structures and Algorithms - PowerPoint PPT Presentation

1 / 79
About This Presentation
Title:

CSCE 210 Data Structures and Algorithms

Description:

A Dictionary is a form of container that permits access by content. ... emptyTable (E): Initialize table and fill with empty symbol ... – PowerPoint PPT presentation

Number of Views:27
Avg rating:3.0/5.0
Slides: 80
Provided by: dramrg
Category:

less

Transcript and Presenter's Notes

Title: CSCE 210 Data Structures and Algorithms


1
CSCE 210Data Structures and Algorithms
  • Prof. Amr Goneid
  • AUC
  • Part 9. Dictionaries(1) Key Tables and Lists

2
Dictionaries(1)Key Tables and Lists
  • The Dictionary Data Structure
  • The Key Table
  • ADT Key Table
  • The Key Table Class Definition
  • Key Table Class implementation
  • Example Application
  • The Linked List
  • ADT Linked List
  • The Linked List Class Definition
  • Linked List Class implementation
  • Example Application

3
1. The Dictionary Data Structure
  • Simple containers such as tables, stacks and
    queues permit access of elements by position or
    order of insertion.
  • A Dictionary is a form of container that permits
    access by content.
  • Should support the following main operations
  • Insert (D,x) Insert item x in dictionary D
  • Delete (D,x) Delete item x from D
  • Search (D,k) search for key k in D

4
The Dictionary Data Structure
  • Examples
  • Unsorted arrays and Linked Lists permit linear
    search
  • Sorted arrays permit Binary search
  • Ordered Lists permit linear search
  • Binary Search Trees (BST) fast support of all
    dictionary operations.
  • Hash Tables Fast retrieval by hashing key to a
    position.

5
The Dictionary Data Structure
  • There are 3 types of dictionaries
  • Static Dictionaries These are built once and
    never change. Thus they need to support search,
    but not insertion or deletion. These are better
    implemented using arrays or Hash tables with
    linear probing.
  • Semi-dynamic Dictionaries These structures
    support insertion and search queries, but not
    deletion. These can be implemented as arrays,
    linked lists or Hash tables with linear probing.

6
The Dictionary Data Structure
  • Fully Dynamic Dictionaries These need fast
    support of all dictionary operations. Binary
    Search Trees are best. Hash tables are also great
    for fully dynamic dictionaries as well, provided
    we use chaining as the collision resolution
    mechanism.

7
The Dictionary Data Structure
  • In the following, we will design and implement
    two dictionary data structures that support all
    basic operations. Both will be linear structures
    and so will employ linear search. They are
    suitable for small to medium sized data.
  • The first uses a run-time array to implement an
    ordered list and is suitable if we know the
    maximum data size
  • The second uses a linked list and is suitable if
    we do not know the size of data to insert.

8
2. The Key Table
  • Key Table Abstraction
  • Key Table a container of data in the form of a
    linear configuration of elements in which we can
    insert and delete nodes in any order. It also
    supports search by content (key), and can
    represent a dictionary ADT.
  • Element a container for one key and associated
    data item
  • Current Element special element in the table,
    indicated by a pointer to the current position.
  • EmptyKey a special key value to indicate that
    the table slot is empty. Initially all table
    positions are empty.

9
Ordered Key Table Class
  • We will construct a class Ktable whose objects
    are key tables. They will be implemented as
    dynamic arrays.
  • The data members will be the elements and a
    pointer to these elements.
  • An element contains a key field and a data field.
    Search is by content (key)
  • The table will be ordered on the key field.

10
(a) ADT Key Table
  • Key Table Data Members
  • Elements. Each element has
  • Data or information field of type dataType.
  • A key of type keyType
  • Others
  • T, a pointer to a dynamic array of elements
  • P, a pointer to the current element
  • MaxSize, The maximum size (Capacity) of the
    table
  • csize, The current size of table (no. of filled
    slots)
  • Empty, A special key value to indicate an empty
    slot

11
Element Specification
  • // The element structure can be specified as a
    Class
  • // in the private part of the main Ktable class.
  • class element // Hidden from user
  • public
  • keyType key // key
  • dataType data // Data
  • // end of class element declaration

12
Key Table Operations
  • construct Create table
  • emptyTable (E) Initialize table and fill with
    empty symbol
  • tableIsEmpty ? bool return True if table is
    empty
  • tableIsFull ? bool return True if table is full
  • occupancy ? int return the current no. of
    elements in the table

13
Key Table Operations
  • updateData (d) to update the data portion of
    the current element to contain d assume the
    current position is nonempty.
  • retrieveData (d) to return the data (d) in the
    current element assume the current position is
    nonempty.
  • deleteCurrent delete the current element.
    Assume the current position is nonempty initially.

14
Key Table Operations
  • search (k) ? bool Search the table for the slot
    with key part that matches (k). If found, set p
    to the slot and return True, else return false
  • orderInsert (k,d) insert an element in a
    position that maintains an ascending order of the
    key portion.
  • traverse traverse table to print key and info
    fields.

15
(b) Key Table Class Definition
  • // File Ktable.h
  • // Definition of Ktable Template Class
  • ifndef KTABLE_H
  • define KTABLE_H
  • // Specification of the class
  • template ltclass keyType, class dataTypegt
  • class Ktable
  • public

16
Key Table Class Definition
  • // Member Functions
  • Ktable(int nelements 128) // Constructor
  • Ktable() // Destructor
  • // Functions Prototype Definitions
  • void emptyTable(const keyType )
  • bool tableIsEmpty() const
  • bool tableIsFull() const

17
Key Table Class Definition
  • int occupancy() const
  • void updateData(const dataType )
  • void retrieveData(dataType ) const
  • void deleteCurrent()
  • bool search(const keyType )
  • bool orderInsert(const keyType ,
  • const dataType )
  • void traverse() const

18
Key Table Class Definition
  • private
  • // Element Class
  • class element
  • public
  • keyType key // key
  • dataType data // Data
  • // end of class element declaration

19
Key Table Class Definition
  • element T // Pointer to Storage Array
  • int p // Pointer to current element
  • // Maximum and Current Sizes
  • int MaxSize, csize
  • keyType Empty // Empty symbol
  • // end of Ktable Class definition
  • endif // KTABLE_H
  • include "Ktable.cpp"

20
(c) Key Table Class Implementation
  • // FileKtable.cpp
  • // Ktable Class implementation file
  • include ltiostreamgt
  • using namespace std
  • // Constructor with argument,
  • // size is nelements, default is 128
  • template ltclass keyType, class dataTypegt
  • KtableltkeyType, dataTypegtKtable(int nelements)
  • MaxSize nelements T new elementMaxSize
  • p -1 csize 0

21
Key Table Class Implementation
  • // Destructor
  • template ltclass keyType, class dataTypegt
  • KtableltkeyType, dataTypegtKtable()
  • delete T

22
Key Table Class Implementation
  • // Empty whole Table
  • template ltclass keyType, class dataTypegt
  • void KtableltkeyType, dataTypegtemptyTable
  • (const keyType k)
  • Empty k
  • for(int i0 i lt MaxSize i) Ti.key Empty
  • p -1 csize 0

23
Key Table Class Implementation
  • // return True if table is empty
  • template ltclass keyType, class dataTypegt
  • bool KtableltkeyType, dataTypegttableIsEmpty()
    const
  • return (csize 0)
  • // return True if table is full
  • template ltclass keyType, class dataTypegt
  • bool KtableltkeyType, dataTypegttableIsFull()
    const
  • return (csize MaxSize)

24
Key Table Class Implementation
  • // to return the current occupancy of the table
  • template ltclass keyType, class dataTypegt
  • int KtableltkeyType, dataTypegtoccupancy() const
  • return csize
  • // to update the data in the current position
  • template ltclass keyType, class dataTypegt
  • void KtableltkeyType, dataTypegtupdateData
  • (const dataType d)
  • if ((p gt 0)(p lt csize))Tp.data d

25
Key Table Class Implementation
  • // Retrieve the data part of the current position
  • template ltclass keyType, class dataTypegt
  • void KtableltkeyType, dataTypegtretrieveData
  • (dataType d) const
  • d Tp.data

26
Key Table Class Implementation
  • // delete the current element
  • template ltclass keyType, class dataTypegt
  • void KtableltkeyType, dataTypegtdeleteCurrent()
  • if(!tableIsEmpty())
  • for(int ip1 iltcsize i)
  • Ti-1 Ti
  • csize-- Tcsize.key Empty

27
Key Table Class Implementation
  • // search the table for the slot with key part
    that matches (k).
  • // If found, set p to the slot and return True,
    else return false
  • template ltclass keyType, class dataTypegt
  • bool KtableltkeyType, dataTypegtsearch(const
    keyType k)
  • bool found false
  • if(!tableIsEmpty())
  • p 0
  • while ((! found) (p lt csize))
  • if (k Tp.key) found true
  • else p
  • return found

28
Key Table Class Implementation
  • // insert element in a position that maintains an
  • // ascending order of the key
  • template ltclass keyType, class dataTypegt
  • bool KtableltkeyType, dataTypegtorderInsert
  • (const keyType k, const dataType d)
  • if (!tableIsFull())
  • p csize
  • while((p gt 0) (Tp-1.key gt k)) Tp
    Tp-1 p--
  • Tp.key k Tp.data d csize
  • return true
  • else return false

29
Key Table Class Implementation
  • // traverse table to print key and data fields
  • template ltclass keyType, class dataTypegt
  • void KtableltkeyType, dataTypegttraverse() const
  • for(int i 0 i lt csize i)
  • cout ltlt Ti.key ltlt " " ltlt Ti.data ltlt endl

30
Key Table Class Implementation
  • Note
  • Since the keys are kept in ascending order, it is
    possible to use the binary search algorithm in
    the search function.
  • The implementation of this option is left as an
    exercise.

31
(d) Example Application
  • An ordered list of characters and their
    frequencies in a string
  • Given a string, build a List of characters and
    their count in the string. The list is ordered
    alphabetically on the characters.

32
Ordered List
  • // File KTabletest.cpp
  • // Applies Ktable Class
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • include "Ktable.h
  • int main()
  • Ktableltchar , intgt ctable
  • string s
  • char c
  • int i, count
  • bool keyfound

33
Ordered List
  • // Read a string
  • cout ltlt "Enter a string" ltlt endl
  • getline(cin,s)
  • cout ltlt s ltlt endl // display it
  • ctable.emptyTable(char(126))
  • for (i 0 i lt s.length() i) // for every
    character
  • c toupper(s.at(i))
  • // Search for character in the table
  • keyfound ctable.search (c)

34
Ordered List
  • if (keyfound) // if found
  • ctable.retrieveData(count) // get data
  • count // increment count
  • ctable.updateData(count) // store back
  • // Not found, a new node is inserted
  • else ctable.orderInsert(c,1)

35
Ordered List
  • // print characters and their frequencies
  • ctable.traverse()
  • cout ltlt ctable,occupancy() ltlt endl // current
    table size
  • // empty table
  • ctable.emptyTable(char(126))
  • // the size now should be zero
  • cout ltlt ctable.occupancy() ltlt endl
  • return 0

36
Sample Output
  • Enter a string
  • The Rain in Spain
  • The Rain in Spain
  • 3
  • A 2
  • E 1
  • H 1
  • I 3
  • N 3
  • P 1
  • R 1
  • S 1
  • T 1
  • 10
  • 0
  • Press any key to continue

37
3. The Linked List
  • Linked List Abstraction
  • Linked List a container of data in the form of a
    linear configuration of nodes in which we can
    insert and delete nodes in any order. Each Node
    is linked to its successor in the list. If it
    also supports search by contents, it can
    represent a dictionary ADT.
  • Node a container for one data item and has a
    direct relationship with at most two other nodes,
    its predecessor (if any) and its successor (if
    any).
  • Head node or first node is the only node without
    a predecessor.
  • Current node special node in the list, indicated
    by the current position.
  • Previous Node the predecessor of the current node

38
Ordered Linked List Class
  • We will construct a class List whose objects
    are linked lists. They will be implemented as
    dynamic lists.
  • The data members will be the nodes and the
    pointers to these nodes.
  • A node contains a key field and a data field.
    Search is by content (key)
  • The list will be ordered on the key field.

39
(a) ADT Linked List
  • Linked List Data Members
  • Nodes. Each node has
  • Data or information field of type dataType.
  • A key of type keyType
  • Link field (next) , a pointer to next node
  • Pointers
  • head, a pointer to the first node
  • cursor, a pointer to the current node
  • prev, a pointer to the previous node.

40
Data Members
Current

NULL
Last
head
First
prev
cursor
key
data
next
41
Node Specification
  • // The linked structure for a node can be
  • // specified as a Class in the private part of
  • // the main linked list class.
  • class node // Hidden from user
  • public
  • keyType key // key
  • dataType data // Data
  • node next // pointer to next node
  • // end of class node declaration
  • typedef node NodePointer
  • // Pointers
  • NodePointer head, cursor, prev

42
Linked List Operations
  • Notation Meaning
  • head the head pointer
  • cursor pointer to current node
  • prev pointer to predecessor node
  • pnew pointer to a new node
  • d item with the same type as the
  • data portion of a node
  • k item with type as the key portion
  • of the node
  • b boolean value
  • L Length of list

43
Linked List Class Operations
  • construct initialize list to empty
  • listIsEmpty ? b return True if list is empty
  • curIsEmpty ? b return True if current position
    is empty
  • toFirst to make the current node the first
    node if list is empty, the current position is
    still empty
  • atFirst ? b to return True if the current node
    is the first node or if the list and the current
    position are both empty.

44
Linked List Class Operations
  • advance to advance to next node. Assume the
    current position is nonempty initially.
  • toEnd to make the current node the tail node
    if list is empty, the current position is still
    empty
  • atEnd ? b to return True if the current node is
    the tail node or if the list and the current
    position are both empty.
  • listSize ? L to return the size of the list
  • updateData (d) to update the data portion of
    the current node to contain d assume the current
    position is nonempty.

45
Linked List Class Operations
  • retrieveData ? d to return the data in the
    current node assume the current position is
    nonempty.
  • insertFirst (k,d) insert a node with key (k)
    and data (d) at the head of the list the new
    node becomes the current node.
  • insertAfter (k,d) insert a node after the
    current node without changing the current
    position assume the current position is nonempty
    in a non-empty list.
  • insertBefore (k,d) insert a node before the
    current node current position becomes the new
    node

46
Linked List Class Operations
  • insertEnd(k,d) insert a node at the end of the
    list, current position becomes the new node.
  • deleteNode delete the current node and set the
    current position to the next node if the current
    node is the last node initially, the current
    position becomes empty assume the current
    position is nonempty initially.
  • deleteFirst delete the first node and set the
    current position to the next node if it was
    initially the only node, the current position
    becomes empty

47
Linked List Class Operations
  • deleteEnd delete the last node and set the
    current position to empty.
  • makeListEmpty delete whole list
  • search (k) ? b search the list for the node
    with key part that matches (k). If found, set
    cursor to the node and return True, else return
    false and the current position becomes empty.

48
Linked List Class Operations
  • orderInsert (k,d) insert a node in a position
    that maintains an ascending order of the key
    portion of the nodes.
  • traverse traverse list to print key and info
    fields.
  • The Linked List will be implemented as a
  • template class to allow different types for
  • the key and data fields.

49
(b) Linked List Class Definition
  • // File List.h
  • // Definition of Simple Linked List Template
    Class
  • ifndef LIST_H
  • define LIST_H
  • // Specification of the class
  • template ltclass keyType, class dataTypegt
  • class List
  • public

50
List Class Header File
  • // Member Functions
  • // Create an empty List
  • List()
  • // Class Destructor
  • List()
  • // Functions Prototype Definitions
  • bool listIsEmpty() const
  • bool curIsEmpty() const
  • void toFirst()
  • bool atFirst() const
  • void advance()

51
List Class Header File
  • void toEnd()
  • bool atEnd() const
  • int listSize() const
  • void updateData (const dataType )
  • void retrieveData (dataType ) const
  • void insertFirst (const keyType , const
    dataType )
  • void insertAfter (const keyType , const
    dataType )
  • void insertBefore (const keyType , const
    dataType )
  • void insertEnd (const keyType , const dataType
    )
  • void deleteNode()
  • void deleteFirst()

52
List Class Header File
  • void deleteEnd()
  • void makeListEmpty()
  • bool search (const keyType )
  • void orderInsert(const keyType , const dataType
    )
  • void traverse()

53
List Class Header File
  • private
  • // Node Class
  • class node
  • public
  • keyType key // key
  • dataType data // Data
  • node next // pointer to next node
  • // end of class node declaration

key
data
next
54
List Class Header File
  • typedef node NodePointer
  • // Pointers
  • NodePointer head, cursor, prev
  • // End of class List declaration
  • endif // LIST_H
  • include "List.cpp"

55
(c) Linked List Class Implementation
  • // FileList.cpp
  • // Simple Linked List Class implementation file
  • include ltiostreamgt
  • using namespace std
  • // Member Functions
  • // Class Constructor
  • template ltclass keyType, class dataTypegt
  • List ltkeyType, dataTypegt List()
  • head NULL cursor NULL prev NULL
  • // Class Destructor
  • template ltclass keyType, class dataTypegt
  • List ltkeyType, dataTypegt List()
  • makeListEmpty()

56
List Class Implementation File
  • // return True if list is empty
  • template ltclass keyType, class dataTypegt
  • bool ListltkeyType, dataTypegtlistIsEmpty() const
  • return (head NULL)
  • // return True if current position is empty
  • template ltclass keyType, class dataTypegt
  • bool ListltkeyType, dataTypegtcurIsEmpty() const
  • return (cursor NULL)

57
List Class Implementation File
  • // to make the current node the first node if
    list is empty,
  • // the current position is still empty
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegttoFirst()
  • cursor head prev NULL
  • // to return True if the current node is the
    first node or
  • // if the list and the current position are both
    empty.
  • template ltclass keyType, class dataTypegt
  • bool ListltkeyType, dataTypegtatFirst() const
  • return (cursor head)

58
List Class Implementation File
  • // to advance to next node. Assume the current
    position
  • // is nonempty initially.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtadvance()
  • prev cursor cursor cursor-gtnext
  • // to make the current node the tail node
  • // if list is empty, the current position is
    still empty
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegttoEnd()
  • toFirst()
  • if (! listIsEmpty())
  • while ( cursor-gtnext ! NULL) advance()

59
List Class Implementation File
  • // return True if the current node is the tail
    node or
  • // if the list and the current position are both
    empty.
  • template ltclass keyType, class dataTypegt
  • bool ListltkeyType, dataTypegtatEnd() const
  • if ( listIsEmpty()) return true
  • else if (curIsEmpty()) return false
  • else return (cursor-gtnext NULL)

60
List Class Implementation File
  • // to return the size of the list
  • template ltclass keyType, class dataTypegt
  • int ListltkeyType, dataTypegtlistSize() const
  • NodePointer q int count
  • q head count 0
  • while (q ! NULL)
  • count q q-gtnext
  • return count

61
List Class Implementation File
  • // to update the data portion of the current node
    to contain el
  • // assume the current position is nonempty.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtupdateData(const
    dataType d)
  • cursor -gt data d
  • // to return the data in the current node assume
    the current
  • // position is nonempty.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtretrieveData(dataTyp
    e d) const
  • d cursor-gtdata

62
List Class Implementation File
  • // insert a node with data (el) at the head of
    the list the new
  • // node becomes the current node.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtinsertFirst(const
    keyType k, const dataType d )
  • NodePointer pnew
  • pnew new node
  • pnew-gtkey k pnew-gtdata d
  • pnew-gtnext head
  • head pnew
  • cursor head
  • prev NULL

63
List Class Implementation File
  • // insert a node with data (el) after the current
    node without
  • // changing the current position
  • // assume the current position is nonempty in a
    non-empty list.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtinsertAfter(const
    keyType k, const dataType d )
  • NodePointer pnew
  • pnew new node
  • pnew-gtkey k pnew-gtdata d
  • pnew-gtnext cursor-gtnext
  • cursor-gtnext pnew

64
List Class Implementation File
  • // insert a node with data (el) before the
    current node,
  • // current position becomes the new node.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtinsertBefore(const
    keyType k, const dataType d )
  • NodePointer pnew
  • pnew new node
  • pnew-gtkey k pnew-gtdata d
  • pnew-gtnext cursor
  • prev-gtnext pnew
  • cursor pnew

65
List Class Implementation File
  • // insert a node with data (el) at the end of the
    list,
  • // current position becomes the new node.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtinsertEnd(const
    keyType k, const dataType d )
  • if (listIsEmpty()) insertFirst(k,d)
  • else toEnd() insertAfter(k,d)

66
List Class Implementation File
  • // delete the current node and set the current
  • // position to the next node
  • // if the current node is the last node
    initially, the
  • //current position becomes
  • // empty. Assume the current position is nonempty
  • // initially.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtdeleteNode()
  • NodePointer q
  • if(! curIsEmpty())
  • // current node is not empty

67
List Class Implementation File
  • if (atFirst()) // delete head node
  • q cursor cursor cursor-gtnext
  • head cursor delete q
  • else // delete non-head node
  • q cursor cursor cursor-gtnext
  • prev-gtnext cursor delete q

68
List Class Implementation File
  • // delete the first node and set the current
    position to the next node
  • // if it was initially the only node, the current
    position becomes empty
  • // assume the current position is nonempty
    initially.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtdeleteFirst()
  • if(! listIsEmpty()) toFirst() deleteNode()
  • // delete the last node and set the current
    position to empty
  • // assume the current position is nonempty
    initially.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtdeleteEnd()
  • if(! listIsEmpty()) toEnd() deleteNode()

69
List Class Implementation File
  • // delete whole list
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtmakeListEmpty()
  • toFirst()
  • while (! listIsEmpty())
  • deleteNode()

70
List Class Implementation File
  • / search the list for the node with key part
    that matches (k). If found, set cursor to the
    node and return True,else return false and the
    current position becomes empty. /
  • template ltclass keyType, class dataTypegt
  • bool ListltkeyType, dataTypegtsearch (const
    keyType k)
  • bool found false
  • toFirst()
  • while ((! found) (cursor ! NULL))
  • if (k cursor-gtkey) found true
  • else advance()
  • return found

71
List Class Implementation File
  • // insert a node with data (el) in a position
    that maintains an
  • // ascending order of the key portion of the
    nodes.
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegtorderInsert(const
    keyType k, const dataType d)
  • toFirst()
  • while ((cursor ! NULL) (k gt cursor-gtkey))
  • advance()
  • if (prev NULL) insertFirst(k,d)
  • else insertBefore(k,d)

72
List Class Implementation File
  • // traverse list to print key and data fields
  • template ltclass keyType, class dataTypegt
  • void ListltkeyType, dataTypegttraverse()
  • toFirst()
  • while (! curIsEmpty())
  • cout ltlt cursor-gtkey ltlt " " ltlt cursor-gtdata ltlt
    endl
  • advance()

73
(d) Example Application
  • An ordered list of characters and their
    frequencies in a string
  • Given a string, build a List of characters and
    their count in the string. The list is ordered
    alphabetically on the characters.

74
Ordered List
  • // File ListAppl.cpp
  • // Applies List Class Ordered linked list
  • include ltiostreamgt
  • include ltstringgt
  • using namespace std
  • include "List.h"
  • int main()
  • Listltchar, intgt clist
  • string s
  • char c
  • int i, count
  • bool keyfound

75
Ordered List
  • // Read a string
  • cout ltlt "Enter a string" ltlt endl
  • getline(cin,s)
  • cout ltlt s ltlt endl // display it
  • for (i 0 i lt s.length() i) // for every
    character
  • c toupper(s.at(i)) // convert to upper case
  • // Search for character in the list
  • keyfound clist.searchForKey(c)

76
Ordered List
  • if (keyfound) // if found
  • clist.retrieveData(count) // get data in
    node
  • count // increment count
  • clist.storeData(count) // store back
  • // Not found, a new node is inserted
  • else clist.orderInsert(c,1)

77
Ordered List
  • // print characters and their frequencies
  • clist.traverse()
  • cout ltlt clist.listSize() ltlt endl // current
    list size
  • //clist.makeListEmpty() // empty list, or
  • clist.List() // free memory
  • // the size now should be zero
  • cout ltlt clist.listSize() ltlt endl
  • return 0

78
Sample Output
  • Enter a string
  • The Rain in Spain
  • The Rain in Spain
  • 3
  • A 2
  • E 1
  • H 1
  • I 3
  • N 3
  • P 1
  • R 1
  • S 1
  • T 1
  • 10
  • 0
  • Press any key to continue

79
Learn on your own about
  • Array-based implementation of Linked Lists
Write a Comment
User Comments (0)
About PowerShow.com