Error on page 482
This
c=s.pop();is wrong and should be
c=s.top();
s.pop();(Once we normally defined "pop()" to return a copy of the top of the stack, but no longer... and Scansholm forgot to update his code on this page...
class ForExample{
ForExample * i_am_a_link;
ForExample i_am_a_mistake;
};Linked data structures are constructed out of Elements or Nodes. Typically Nodes/Elements they contain some data plus some links. Elements and Nodes often have a constructor.... and are hidden inside another class that defines how to place data in the structure and how to extract it afterward. Because the Nodes are kept private we can allow their contents to be public -- and so available to the containing structure.
Here
[ 15demo.cpp ]
is a
demonstration of how links work
that shows you the numerical values of each link and
the addresses of the nodes in a list. This follows the
example in the book.
When should I use linked data
Here [ Set.png ] is a UML diagram of the books Set and Element classes ( download model from [ Set.dia ] ).
. . . . . . . . . ( end of section Book Topics) <<Contents | End>>
pointer->next->previous == pointerwhenever pointer->next is not NULL.
See pages 475...
Can you go over recursive data types?
Data types are not exactly recursive. They just can store
the addresses of objects of the same type. This lets them
reflect a recursive structure in the real world. For example
in a family tree each person has two parents, so a realistic
class would be:
class Person{
...
Person * mother;
Person * father;
...
friend void print_ancestors(Person * p, int gen);
...
};The rule is that once the compiler sees "class NNN" then you can declare pointers "NNN * nnn;". The NNN class is incomplete. You can not declare objects of type NNN: "NNN object" until the complete class is done.
We use can use recursion to move around and explore these structures:
void Person::print_ancestors(Person *p, int g=0)
{
if(p)
{
list_ancestors(p->mother, g+1);
list_ancestors(p->father, g+1);
cout << substr(".........", 0, g)<<*p<<endl;
}
}(unchecked code).
| Address | Contents | Symbol in C++ |
|---|---|---|
| ... | ||
| 1200 | 'b' | first->next |
| 1202 | 0 | first->next->next |
| 1202 | 2400 | first->next->prev |
| ... | ||
| 2400 | 'a' | first |
| 2401 | 1200 | first->next |
| 2402 | 0 | first->prev |
| ... |
| Address | Contents | |
|---|---|---|
| ... | ||
| 1200 | 'b' | |
| 1202 | 0 | |
| 1202 | 3400 | |
| ... | ||
| 2400 | 'a' | first |
| 2401 | 3400 | |
| 2402 | 0 | |
| ... | ||
| 3400 | 'c' | |
| 3401 | 1200 | |
| 3402 | 2400 | |
| ... |
Because: a typical rcord for a person might have several 100 bits of data and the point no more than 32bits.
Consider: which takes up more space:
QA76.76 C 1978or the book found at that place on the shelves in the library.
Which takes up more space: you or your phone number?
Why combine linked data structures and recursion to reverse a string?
Why not?
It is easy! You can also use a stack..... see the text and laboratory 8
below.
15demo.cpp didnt work on MicroSoft.NET
I'm not surprised.... I would guess you will have to do something
with <iostream> and cerr. Let me know more detials...
TBD
What is a set?
In mathematics it is an unordered collection of elements with no repeats.
In Computer Science it is an ordered set of items. We have
at least the following operations
| Name | Math | Meaning |
|---|---|---|
| member | x ∈ A | True if x is in A else false. |
| union | A ∪ B | Elements in either A or B |
| intersection | A ∩ B | Elements in both A and B |
| insert | - | Afterward inserted element is in set |
| remove | - | Afterward inserted element is not in set |
You can always type in code like this and add a debugging output:
cerr<< .....<< endl;at the start to see what the code does.
Doubly linked lists seem better than singly linked lists should we also use them?
Adding links to a data structure often makes it more powerful. However
we have to store the extra pointer, and we have to manipulate/copy/change
the extra pointer. As a result doubly linked lists take up more space
and can be slower than singly linked lists.
Use a simply linked list when you need a simple structure and program performance is more important than your time. Otherwise use the Standard Library!
Use a doubly linked list when you want reliable coded and bi-directional access.
In a complex data structure with many different links use the UML to work out whther you need a single or double linked connection. An arrow head [ Linked Data Structures in uml1b ] is the best notation for singly linked data.
What is the special Element at the start of the Doubly Linked list in the book?
This is a called a tombstone. It stays there when the rest of the list has
gone. It simplifies coding insertion and deletion because we know
that before EVERY Element there is a previous one, and after each one
there is a next Element. No NULL pointers to test for and program around.
What is the difference between a link and a pointer?
Not much. A link is often used to indicate a special pointer used
to connect items in a container. So a link typically is a pointer from
a class, back into the same class.
.
Why use a dynamically linked list instead of an array/vector/deque/...
To allow you to insert and delete elements in the middle, without copying the
data.
. . . . . . . . . ( end of section CSci202 Computer Science II, Session 15 Linked Data Structures) <<Contents | End>>
Laboratory 8 Implementing and using a Stack to Evaluate Expressions
We will now use C++ to turn yur computer into a $15 calculator!
| Input | 1 + 2 + 3 = |
| Output | 6 |
| Input | ( 1 + 2 ) + 3 = |
| Output | 9 |