kruse/ryba ch041 linked stacks and queues pointers and linked structures linked stacks linked stacks...
TRANSCRIPT
Kruse/Ryba ch04 1
Linked Stacks and Queues
Pointers and Linked StructuresLinked StacksLinked Stacks with SafeguardsLinked Queues
Application: Polynomials ArithmaticAbstract Data Types and Implementations
Kruse/Ryba ch04 2
Pointers
Kruse/Ryba ch04 3
Links or Address or PointerSometimes called a reference (handle)Machine address
Pointers
Kruse/Ryba ch04 4
Diagram Conventions
"Sue"
"Bill"
"Les"
"Jill"
a
b
c
d
e
Kruse/Ryba ch04 5
Singly Linked Structures
Fred225-3465$1,254.65
Harry225-8122$2,436.98
Sally335-2145$2,332.45
Joe225-2311$1,496.00
Larry336-8865$2,221.66
Kruse/Ryba ch04 6
Dynamic ObjectsMEMORY
Run-time stack
Heap
Item* itemPtr; itemPtr
itemPtr = NULL;
itemPtr = new Item;
.
.
.
*itemPtr = someItem;
Kruse/Ryba ch04 7
Dynamic ObjectsMEMORY
Run-time stack
Heap
Item* itemPtr; itemPtr
itemPtr = NULL;
itemPtr = new Item;
.
.
.
*itemPtr = someItem;
delete itemPtr;
Kruse/Ryba ch04 8
Dynamic Objects
itemPtr = new Item;In C, returns 0 when failsIn ANSI C++ throws exception
itemPtr = new(nothrow) Item;Same effect as in CMust have #include <new>
using namespace std;
Kruse/Ryba ch04 9
Dynamically Allocated Arrays
int size, i;
int* dynamicArray; cout << "Enter an array size: " << flush;cin >> size;dynamicArray = new int[size];for (i = 0; i<size; i++) dynamicArray[i] = i;
Kruse/Ryba ch04 10
Pointer Assignment
"Sue"
"Bill"
a
b
a = b;
"Sue"
"Bill"
a
b
"Bill"
"Bill"
a
b
*a = *b;
Kruse/Ryba ch04 11
Addresses of Automatic Objects
Item x;Item* y;y = &x;
x
y
y
Kruse/Ryba ch04 12
Dynamically Allocated Singly Linked List
Fred225-3465$1,254.65
Harry225-8122$2,436.98
Sally335-2145$2,332.45
Joe225-2311$1,496.00
Larry336-8865$2,221.66
Kruse/Ryba ch04 13
struct Node //class Node {
public: // data members NodeEntry entry; Node *next; // constructors Node(); Node(NodeEntry item, Node* addOn = NULL);};
Node
Kruse/Ryba ch04 14
Constructors
Node::Node(){ next = NULL;}
Node::Node(NodeEntry item, Node* addOn){ entry = item; next = addOn;}
Kruse/Ryba ch04 15
Sample CodeNode firstNode('a'); // Node firstNode stores data 'a'.Node *p0 = &firstNode; // p0 points to firstNode.NodeNode *p1 = new Node('b'); // A second node is created.p0->next = p1; // The second Node is linked after
firstNode.Node *p2 = new Node('c', p0); // A third Node storing 'c' is
created. // The third Node links back to
the first node, p1->next = p2; // The third Node is linked after
the second Node.
Kruse/Ryba ch04 16
Linked Stackclass Stack { public: Stack(); void empty() const; void push(const Stack_entry &item);
void top(Stack_entry &item) const; protected: Node *top_node;};
Kruse/Ryba ch04 17
Why a data type with only one element?
Maintain encapsulationMaintain logical distinction between stack and top of stackMaintain consistency with other data structuresHelps with debugging.
Kruse/Ryba ch04 18
Linked Stack- push//Post: Stack_entry item is added to top// of the Stack; returns success or// returns a code of overflow // if dynamic memory is exhausted.void Stack::push(const Stack_entry &item){ Node *new_top = new Node(item, top_node); if (new_top == NULL) throw overflow_error(“You goofed”); top_node = new_top; return;}
//#include <stdexcept>
Kruse/Ryba ch04 19
Linked Stack- pop//Post: The top of the Stack is removed.// If the Stack is empty the method// returns underflow; // otherwise it returns success.void Stack::pop(){ Node *old_top = top_node; if (top_node == NULL) throw underflow_error(“A bummer”); top_node = old_top->next; delete old_top; return;}
Kruse/Ryba ch04 20
Linked Stack- destructor
/*Post: The Stack is cleared.*/
Stack::~Stack() // Destructor{ while (!empty()) pop();}
Kruse/Ryba ch04 21
Dangers in Assignments
Suppose we have:
– int x = 10;
int y = 20;
– Now, if we have x = y;
– This is called Value Semantic.
10
20
x
y
20 20
x y
Kruse/Ryba ch04 22
continued
Now, assume x and y are pointers.
10 20x y
If we have x = y;
10
20
x
yy
This is called Reference Semantic
Kruse/Ryba ch04 23
Now, if we have delete x; what happens to y?
y
y is deleted too. Even if we did not intended to do so.
Kruse/Ryba ch04 24
Linked Stack- overloaded =/* Post: The Stack is reset as a copy of Stack original.*/void Stack::operator = (const Stack &original) { Node *new_top, *new_copy, *original_node = original.top_node; if (original_node == NULL) new_top = NULL; else
{ // Duplicate the linked nodes new_copy = new_top = new Node(original_node->entry); while (original_node->next != NULL)
{ original_node = original_node->next; new_copy->next = new Node(original_node->entry); new_copy = new_copy->next; } //end while } //end else while (!empty()) // Clean out old Stack entries pop(); top_node = new_top; // and replace them with new entries.}
Kruse/Ryba ch04 25
Assume x and y are objects.To execute this function We can have:
x = y; Or x.operator = (y);x ill execute the function and y will be its parameter.
and we get the following:5
20
x
10
15
y
20 10
x
20 10
y
5 15
Kruse/Ryba ch04 26
/*Post: Stack initialized as a copy of Stack original.*/Stack::Stack(const Stack &original) // copy constructor{ Node *new_copy, *original_node = original.top_node; if (original_node == NULL) top_node = NULL; else
{ // Duplicate the linked nodes. top_node = new_copy = new Node(original_node->entry); while (original_node->next != NULL)
{ original_node = original_node->next; new_copy->next = new Node(original_node->entry); new_copy = new_copy->next; }//end while }//end else}
Linked Stack- Copy Constructor
Kruse/Ryba ch04 27
Improved Linked Stackclass Stack { public: // Standard Stack methods Stack(); void empty() const; void push(const Stack_entry &item); void pop(); void top(Stack_entry &item) const; //Safety features for linked structures ~Stack(); Stack(const Stack &original); void operator =(const Stack &original); protected: Node *top_node;};//end class Stack
Kruse/Ryba ch04 28
ADT stack
Create the stack, leaving it emptyTest whether the stack is emptyPush a new entry onto the top of the stack, provided it is not fullPop the entry off the top of the stack, provided the stack is not emptyRetrieve the top entry off the stack, provided the stack is not empty
Kruse/Ryba ch04 29
Linked Queuesclass Queue { public: // standard Queue methods Queue(); void empty() const; void append(const Queue_entry &item); void serve(); void retrieve(Queue_entry &item) const; // safety features for linked structures ~Queue(); Queue(const Queue &original); void operator =(const Queue &original); protected: Node *front, *rear;};//end class Queue
Kruse/Ryba ch04 30
Extended Queue(A little inheritance)
class Extended_queue: public Queue
{
public:
bool full() const;
int size() const;
void clear();
void serve_and_retrieve(Queue_entry item);
};
Kruse/Ryba ch04 31
size()int Extended_queue::size() const/*Post: Return the number of entries in the Extended_queue.*/{ Node *window = front; int count = 0; while (window != NULL) { window = window->next; count++; } return count;}
Kruse/Ryba ch04 32
ADT queue
Create the queue, leaving it emptyTest whether the queue is emptyAppend a new entry onto the rear of the queue, provided it is not fullServe (and remove) the entry off the front of the queue, provided the queue is not emptyRetrieve the front entry off the queue, provided the queue is not empty
Kruse/Ryba ch04 33
ADT extended queue
Everything that a queue doesDetermine whether the queue is full or notFind the size of the queueServe and retrieve the front entry in the queue, provided the queue is not emptyClear the queue to make it empty
Kruse/Ryba ch04 34
Catch Everything From Chapter 4