1 / 13

Chapter 3

Chapter 3. Lists, Stacks, Queues. Abstract Data Types. A set of items Just items, not data types, nothing related to programming code A set of operations that can be performed on the items Nothing about HOW to do the operations For example add to the end Length of list.

zach
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Lists, Stacks, Queues

  2. Abstract Data Types • A set of items • Just items, not data types, nothing related to programming code • A set of operations that can be performed on the items • Nothing about HOW to do the operations • For example add to the end • Length of list

  3. C++ Classes and ADT • The C++ class facility makes it easy to implement ADTs. • Hides the details from the user • Easy to change the details (would not have to change programs that use the class) • The interface (argument sequence) should never have to change (if it does, then it was a poor design).

  4. List (not linked list) • A basic ADT is a List. • A list holds a collection of items • Class roll • Shopping list • Each item has a position • Various operations • Print, makeEmpty, insert, remove, find • Must determine error conditions • Remove from empty list, find value not there…

  5. Implementations – Array • Very easy to use an array to implement the List ADT. • Fixed size • Easy to add and remove at the end • Easy to find • Very easy to find if list is ordered • Easy to go to a specified position (findKth) • “Hard” to add and remove from anywhere else • Must move items up or down (think of big lists) • NOTE: Easy and hard are computer time, not programmer effort.

  6. Implementation – Linked Lists • Easy to add to beginning • Can be easy to add to end (if keep an end pointer) • Easy to add after/before any position (just change pointers after you get to that position) • findKth not as easy as array based • Can be done with dynamic memory (no fixed size).

  7. Doubly Linked Lists • If we ever need to “back up” in a linked list, it will be very difficult. • Have to start at beginning, keep a previous pointer and go until get to “current” node. • Instead, have a node keep a previous (as well as next) link. • The first node’s previous will be NULL. • More pointers to change when inserting or deleting, but still just a fixed amount of code to execute – O(1). • Also makes insertInOrder easier (no need to keep a previous pointer.

  8. Stack • Last-in-First-out model. • The only real rule is that when we remove from a stack, we get the last thing added (that has not already been removed) • How that is implemented is up to the designer • Operations • Push, pop (mandatory) • Nice: top, size, isempty….

  9. Stack Implementation – Array • Keep an array and a variable to tell where the top of the stack is. • Push will put something into the array and change the top pointer • Pop will return a value and change the top pointer • Top will return a value and NOT change the top pointer

  10. Stack Implementation – Linked List • Use a linked list with just a few methods: • Push == insert at beginning • Pop == remove from beginning • Top == return value of first node

  11. Stack Uses • Find parentheses pairs (or any pairs {} []…) • Evaluating reverse polish (postfix) expressions • Infix to postfix conversion • Function call returns (allows recursion)

  12. Queues • Easy with Linked List (add to end, remove from the beginning). • Need to use a circular array when implementing with an array

  13. Header Node • Have a header node at the beginning of a linked list (node created in the constructor). • This way the Linked List always has a “NODE” (but it is not counted in the size). • Always skipped; has no data • Now, never have to check for empty list. • Never have to change the head pointer. • Most other methods have to be changed to account for the header node.

More Related