Recitation Week 10 - PowerPoint PPT Presentation

About This Presentation
Title:

Recitation Week 10

Description:

Object Oriented Programming COP3330 / CGS5409 – PowerPoint PPT presentation

Number of Views:53
Avg rating:3.0/5.0
Slides: 29
Provided by: Michael3545
Learn more at: https://ww2.cs.fsu.edu
Category:

less

Transcript and Presenter's Notes

Title: Recitation Week 10


1
Recitation Week 10
  • Object Oriented Programming
  • COP3330 / CGS5409

2
Todays Recitation
  • Intro to Data Structures
  • Vectors
  • Linked Lists
  • Queues
  • Stacks

3
Abstract Data Types
  • C has some built-in methods of storing compound
    data in useful ways, like arrays and structs.
  • Collectively known as Abstract Data Types, as
    they describe the nature of what is stored, along
    with the expected operations for accessing the
    data, without specifying the details of
    implementation

4
Abstract Data Types
  • C classes allow a nice mechanism for
    implementing such data types
  • Provides an interface (the public section) for
    the necessary operations
  • The actual implementation details are internal
    and hidden.

5
Vectors
  • Data structure that stores items of the same
    type, and is based on storage in an array
  • By encapsulating an array into a class (a vector
    class), we can
  • Use dynamic allocation to allow the internal
    array to be flexible in size
  • Handle boundary issues of the array (error
    checking for out-of-bounds indices).

6
Vectors
  • Advantages Random access - i.e. quick locating
    of data if the index is known.
  • Disadvantages Inserts and Deletes are typically
    slow, since they may require shifting many
    elements to consecutive array slots

7
Linked Lists
  • Collections of data items linked together with
    pointers, lined up "in a row".
  • Typically a list of data of the same type, like
    an array, but storage is arranged differently.
  • Made up of a collection of "nodes", which are
    created from a self-referential class (or struct).

8
Linked Lists
  • Self-referential class a class whose member data
    contains at least one pointer that points to an
    object of the same class type.
  • Each node contains a piece of data, and a pointer
    to the next node.
  • Nodes can be anywhere in memory (not restricted
    to consecutive slots, like in an array).
  • Nodes generally allocated dynamically, so a
    linked list can grow to any size, theoretically
    (within the boundaries of the program's memory).

9
Linked Lists
  • An alternative to array-based storage.
  • Advantages Inserts and Deletes are typically
    fast. Require only creation of a new node, and
    changing of a few pointers.
  • Disadvantage No random access. Possible to
    build indexing into a linked list class, but
    locating an element requires walking through the
    list.
  • Notice that the advantages of the array (vector)
    are generally the disadvantages of the linked
    list, and vice versa

10
Stacks
  • First In Last Out (FILO)
  • Insertions and removals can occur from "top"
    position only
  • Analogy - a stack of cafeteria trays. New trays
    are always placed on top. Trays also picked up
    from the top.

11
Stacks
  • A stack class will have two primary operations
  • push -- adds an item onto the top of the stack
  • pop -- removes the top item from the stack
  • Typical application areas include compilers,
    operating systems, handling of program memory
    (nested function calls)

12
Queues
  • First In First Out (FIFO)
  • Insertions at the "end" of the queue, and
    removals from the "front" of the queue.
  • Analogy - waiting in line for a ride at an
    amusement park. Get in line at the end. First
    come, first serve.

13
Queues
  • A queue class will have two primary operations
  • enqueue -- adds an item into the queue (i.e. at
    the back of the line)
  • dequeue -- removes an item from the queue (i.e.
    from the front of the line).
  • Typical application areas include print job
    scheduling, operating systems (process
    scheduling).

14
Implementing Abstract Types
  • Some abstract types, like Stacks and Queues, can
    be implemented with a vector or with a linked
    list. 
  • A stack can use a linked list as its underlying
    storage mechanism, for instance, but would limit
    the access to the list to just the "push" and
    "pop" concepts (insert and remove from one end).

15
Trees
  • A non-linear collection of data items, also
    linked together with pointers (like a linked
    list).
  • Made up of self-referential nodes. In this case,
    each node may contain 2 or more pointers to other
    nodes.

16
Trees
  • Typical example a binary tree
  • Each node contains a data element, and two
    pointers, each of which points to another node.
  • Very useful for fast searching and sorting of
    data, assuming the data is to be kept in some
    kind of order.
  • Binary search - finds a path through the tree,
    starting at the "root", and each chosen path
    (left or right node) eliminates half of the
    stored values.

17
Code Example Linked Lists
  • Creates a template doubly linked list of List()
    type, composed of Listnode() type nodes.
  • Listnode.h -- Template ListNode class definition.
  • List.h -- Template List class definition.

18
Listnode.h
  • ifndef LISTNODE_H
  • define LISTNODE_H
  • templatelt typename T gt class List
  • templatelt typename T gt
  • class ListNode
  • friend class Listlt T gt // make List a friend
  • public
  • ListNode( const T ) // constructor
  • T getData() const // return data in node
  • private
  • T data // data
  • ListNodelt T gt nextPtr // next node in list

19
Listnode.h -- continued
  • // constructor
  • templatelt typename T gt
  • ListNodelt T gtListNode( const T info ) data(
    info ), nextPtr( 0 )
  • // end ListNode constructor
  • // return copy of data in node
  • templatelt typename T gt
  • T ListNodelt T gtgetData() const
  • return data
  • // end function getData
  • endif

20
List.h
  • ifndef LIST_H
  • define LIST_H
  • include ltiostreamgt
  • include "Listnode.h" // ListNode class
    definition
  • templatelt typename T gt
  • class List
  • public
  • List() // constructor
  • List() // destructor
  • void insertAtFront( const T )
  • void insertAtBack( const T )
  • bool removeFromFront( T )
  • bool removeFromBack( T )
  • bool isEmpty() const
  • void print() const
  • private

21
List.h -- continued
  • // default constructor
  • templatelt typename T gt
  • Listlt T gtList() firstPtr( 0 ), lastPtr( 0
    )
  • // end List constructor
  • // destructor
  • templatelt typename T gt
  • Listlt T gtList()
  • if ( !isEmpty() ) // List is not empty
  • cout ltlt "Destroying nodes ...\n"
  • ListNodelt T gt currentPtr firstPtr
  • ListNodelt T gt tempPtr
  • while ( currentPtr ! 0 ) // delete
    remaining nodes
  • tempPtr currentPtr
  • cout ltlt tempPtr-gtdata ltlt '\n'

22
List.h -- continued
  • / insert node at front of list
  • templatelt typename T gt
  • void Listlt T gtinsertAtFront( const T value )
  • ListNodelt T gt newPtr getNewNode( value )
    // new node
  • if ( isEmpty() ) // List is empty
  • firstPtr lastPtr newPtr // new list
    has only one node
  • else // List is not empty
  • newPtr-gtnextPtr firstPtr // point new
    node to previous 1st node
  • firstPtr newPtr // aim firstPtr at new
    node
  • // end else
  • // end function insertAtFront

23
List.h -- continued
  • // insert node at back of list
  • templatelt typename T gt
  • void Listlt T gtinsertAtBack( const T value )
  • ListNodelt T gt newPtr getNewNode( value )
    // new node
  • if ( isEmpty() ) // List is empty
  • firstPtr lastPtr newPtr // new list
    has only one node
  • else // List is not empty
  • lastPtr-gtnextPtr newPtr // update
    previous last node
  • lastPtr newPtr // new last node
  • // end else
  • // end function insertAtBack

24
List.h -- continued
  • // delete node from front of list
  • templatelt typename T gt
  • bool Listlt T gtremoveFromFront( T value )
  • if ( isEmpty() ) // List is empty
  • return false // delete unsuccessful
  • else
  • ListNodelt T gt tempPtr firstPtr // hold
    tempPtr to delete
  • if ( firstPtr lastPtr )
  • firstPtr lastPtr 0 // no nodes
    remain after removal
  • else
  • firstPtr firstPtr-gtnextPtr // point
    to previous 2nd node
  • value tempPtr-gtdata // return data being
    removed
  • delete tempPtr // reclaim previous front
    node
  • return true // delete successful
  • // end else

25
List.h -- continued
  • // delete node from back of list
  • templatelt typename T gt
  • bool Listlt T gtremoveFromBack( T value )
  • if ( isEmpty() ) // List is empty
  • return false // delete unsuccessful
  • else
  • ListNodelt T gt tempPtr lastPtr // hold
    tempPtr to delete
  • if ( firstPtr lastPtr ) // List has one
    element
  • firstPtr lastPtr 0 // no nodes
    remain after removal
  • else
  • ListNodelt T gt currentPtr firstPtr
  • while ( currentPtr-gtnextPtr ! lastPtr )
    // locate second-to-last element
  • currentPtr currentPtr-gtnextPtr //
    move to next node
  • lastPtr currentPtr // remove last
    node
  • currentPtr-gtnextPtr 0 // this is now
    the last node
  • // end else
  • value tempPtr-gtdata // return value from
    old last node
  • delete tempPtr // reclaim former last node
  • return true // delete successful

26
List.h -- continued
  • // is List empty?
  • templatelt typename T gt
  • bool Listlt T gtisEmpty() const
  • return firstPtr 0
  • // end function isEmpty
  • // return pointer to newly allocated node
  • templatelt typename T gt
  • ListNodelt T gt Listlt T gtgetNewNode(
  • const T value )
  • return new ListNodelt T gt( value )
  • // end function getNewNode

27
List.h -- continued
  • // display contents of List
  • templatelt typename T gt
  • void Listlt T gtprint() const
  • if ( isEmpty() ) // List is empty
  • cout ltlt "The list is empty\n\n"
  • return
  • // end if
  • ListNodelt T gt currentPtr firstPtr
  • cout ltlt "The list is "
  • while ( currentPtr ! 0 ) // get element
    dat
  • cout ltlt currentPtr-gtdata ltlt ' '
  • currentPtr currentPtr-gtnextPtr
  • // end while
  • cout ltlt "\n\n"
  • // end function print
  • endif

28
Questions?
Write a Comment
User Comments (0)
About PowerShow.com