Chapter 4 Linked Stacks and Queues ?????? - PowerPoint PPT Presentation

1 / 56
About This Presentation
Title:

Chapter 4 Linked Stacks and Queues ??????

Description:

Chapter 4 Linked Stacks and Queues Pointers and linked structures Linked stacks Linked stacks with safeguards – PowerPoint PPT presentation

Number of Views:151
Avg rating:3.0/5.0
Slides: 57
Provided by: Kelly
Category:

less

Transcript and Presenter's Notes

Title: Chapter 4 Linked Stacks and Queues ??????


1
Chapter 4 Linked Stacks and Queues??????
  1. Pointers and linked structures(??????)
  2. Linked stacks (??)
  3. Linked stacks with safeguards
  4. Linked queues (???)
  5. Application polynomial arithmetic
  6. Abstract data types and implementations

2
  • ????(overflow)??
  • When using arrays to implement data structures,
    we must fix the sizes of the arrays.
  • - if the size is not big enough, then overflow
  • - if the size is too big, then much of the
    space is not used
  • Solution pointers, dynamic memory allocation.

3
Linked Structures
  • A linked structure is made up of nodes,

entry next
A pointer pointing to the next node
Store data
4
  • We shall use a struct to implement nodes
  • struct Node
  • // data members
  • Node_entry entry
  • Node next
  • // constructors
  • Node()
  • Node(Node_entry item, Node add_on NULL)

Type of entries
Default value
5
Node Constructors NodeNode() next
NULL NodeNode(Node_entry item, Node add_on)
entry item next add_on
6
(No Transcript)
7
Linked Stacks
  • To implement pop and push, we need to decide
    Where is the top node?
  • Take top_node at top node is much easier.

8
Push New node
new_top-gtnext top_node top_node new_top
// Obs! The order is important!
9
Pop up a node
top_node top_node-gtnext
10
Class declaration for linked stacks
11
New node is added as the first node.
12
(No Transcript)
13
How about the following code of popping if
(top_node NULL) return underflow top_node
top_node-gtnext return success
This node is lost! Garbage is created!
14
(No Transcript)
15
Problem Example
Garbage created when stack small goes out of
scope, the space for some_data becomes garbage.
Solution provide a destructor.
16
  1. A destructor is automatically executed on objects
    of the class when the objects go out of scope.
  2. It is often used to clean up the space that
    otherwise become garbage.

17
Danger in Assignment
Random address after inner_stack goes out of
scope.
18
  • Misbehaviors
  • Lost data space
  • outer_stack.top_node becomes a random address
    when the destructor on inner_stack delete
    outer_stack.
  • The reason is that the assignment here copies
    references (has reference semantics), not value
    (value semantics).
  • The solution is to provide a overloading
    assignment that has value semantics (only copies
    data).

19
  • Prototype and outline
  • void Stackoperator(const Stack original)
  • Make a copy of the data in original a) first
    case original is empty b) copy the first node
    c) run a loop to copy every node in original.
  • Clear out any data in the Stack being assigned
    to
  • Move the newly copied data to the Stack object.
  • Obs! Dont release data before copy finished.
    Otherwise, it will not work for x x.

20
(No Transcript)
21
  • The default copy operation copies every data
    member of a class (has reference semantics here)
  • Copy shares nodes with vital_data
  • When destructor is applied to copy, vital_data is
    also destroyed.

22
Solution Define its own copy constructor. Stack
Stack(const Stack original) //The Stack is
initialized as a copy of original
23
Linked stack copy constructor
24
Modified linked-stack specification
25
Linked queues
26
(No Transcript)
27
Append and Serve
28
(No Transcript)
29
(No Transcript)
30
Extended linked queue
31
(No Transcript)
32
Polish notation for expressions
  • Writing all operators either before their
    operands or after them is called Polish
    notation.
  • prefix form operators are before their operands
  • Postfix form(reverse Polish form) operators are
    after their operands.
  • ab becomes a b (prefix) or a b (postfix)
  • a b c becomes a b c (postfix)
  • a(b-c)d becomes a b c - d (postfix)

33
Converting infix form to postfix form
  • Operands are output directly
  • Operators are suspended until their operands are
    found
  • When do you know the operands are read? Another
    operator is read.
  • Use a stack to remember the operators.
  • When another operator is read, compare the
    priorities of the operator and the operator on
    the top of the stack. Pop if the top operator has
    higher priority.

34
  • Exercise. Design an algorithm that converting
    infix expressions (including parentheses) to
    postfix expressions.

35
Advantages of postfix form
  • No parentheses, no priorities
  • Evaluation is done from left to right
  • Evaluation is easy and efficient
  • Used in compilers, infix expressions are
    transformed into postfix form

36
Evaluating postfix expressions
  • Read the expression from left to right, remember
    the operands until their operator is found later
  • Natural way to remember operands use stack, and
    push operands into the stack
  • When an operator is read, pop the operands and
    push back the result
  • Read on until the end of the expression.

37
Application Polynomial arithmetic
  • Operations Polynomial addition, subtraction,
    multiplication and division (, -, , /)
  • Use reverse Polish notation operands are
    entered before the operations
  • As for the calculator for numbers, operands are
    pushed into a stack, and when an operation is
    performed, operands are popped up and the result
    is pushed back.

38
Data Structures for Polynomial
A polynomial is a list of terms, which consists
of a coefficient and an exponent. The Term is
implemented as struct Term int degree
double coefficient Term (int exponent 0
double scalar 0)
39
  • Then we need to perform operations on lists of
    Terms. The operations have the properties, for
    example, when forming a new list for the sum of
    two lists
  • Removing the first entry from a list
  • Inserting new entries at the end of a list.
  • So, polynomials are treated as queues.

40
  • Contiguous or linked queues?
  • No clear bound for contiguous queues.
  • We represent a polynomial as an extended linked
    queue of terms.

41
polynomials as linked queue of terms.
zero polynomial
42
  • However, we dont want to provide queue methods
    for client programs, for example, not serve(),
    although we would like to use queue methods to
    implement Polynomials.
  • Use private inheritance define the class
    Polynomial to be privately inherited from the
    class Extended_queue and queue methods are not
    available to client programs.

43
Polynomials as a class
class Polynomialprivate Extended_queue
public void read() void print() const
void equals_sum(Polynomial p, Polynomial q)
void equals_diff(Polynomial p, Polynomial q)
void equals_prod(Polynomial p, Polynomial q)
Error_code equals_quot(Polynomial p, Polynomial
q) int degree() const private void
mult_term(Polynomial p, Term t)
44
  • To make things easier, we assume that
  • Polynomials are stored in the order of
    decreasing exponent within the linked queue
  • No two terms have the same exponent
  • No term has a zero coefficient.

45
(No Transcript)
46
(No Transcript)
47
(No Transcript)
48
The main program
49
(No Transcript)
50
(No Transcript)
51
ADT and their implementations
52
  • ADT includes two parts
  • How the components are related each other
  • and
  • what operations can be performed on the
    elements of the ADT.
  • No mention how it is implemented.
  • Contiguous queues and linked queues are all its
    implementation.

53
The process of implementing an ADT
  • Abstract level of a type definition
  • Data Structure level
  • Implementation level

Definition of ADT
Decide on a structure to model our data type
Decide on the details of how our data structure
will be stored
54
  • For queues, we decide to use array or linked
    structures for the implementation at the data
    structure level, so the methods can be analyzed.
  • In the implementation level, we decide more
    details that lead to a class definition.

55
(No Transcript)
56
??????
  • ?????????????????????
  • ??ADT???????????
  • ??????????????????????,???????????,????ADT???????
  • ??????????ADT.
Write a Comment
User Comments (0)
About PowerShow.com