Title: Chapter 4 Linked Stacks and Queues ??????
1Chapter 4 Linked Stacks and Queues??????
- Pointers and linked structures(??????)
- Linked stacks (??)
- Linked stacks with safeguards
- Linked queues (???)
- Application polynomial arithmetic
- 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.
3Linked 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
5Node Constructors NodeNode() next
NULL NodeNode(Node_entry item, Node add_on)
entry item next add_on
6(No Transcript)
7Linked Stacks
- To implement pop and push, we need to decide
Where is the top node? - Take top_node at top node is much easier.
8Push New node
new_top-gtnext top_node top_node new_top
// Obs! The order is important!
9Pop up a node
top_node top_node-gtnext
10Class declaration for linked stacks
11New node is added as the first node.
12(No Transcript)
13How 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)
15Problem Example
Garbage created when stack small goes out of
scope, the space for some_data becomes garbage.
Solution provide a destructor.
16- A destructor is automatically executed on objects
of the class when the objects go out of scope. - It is often used to clean up the space that
otherwise become garbage.
17Danger 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.
22Solution Define its own copy constructor. Stack
Stack(const Stack original) //The Stack is
initialized as a copy of original
23Linked stack copy constructor
24Modified linked-stack specification
25Linked queues
26(No Transcript)
27Append and Serve
28(No Transcript)
29(No Transcript)
30Extended linked queue
31(No Transcript)
32Polish 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)
33Converting 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. -
35Advantages 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
36Evaluating 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.
37Application 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.
38Data 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.
43Polynomials 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)
48The main program
49(No Transcript)
50(No Transcript)
51ADT 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.
53The 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.