Download presentation
Presentation is loading. Please wait.
Published byOsborne Webster Modified over 9 years ago
1
Kruse/Ryba ch041 Linked Stacks and Queues Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials Arithmatic Abstract Data Types and Implementations
2
Kruse/Ryba ch042 Pointers
3
Kruse/Ryba ch043 Links or Address or Pointer Sometimes called a reference (handle) Machine address Pointers
4
Kruse/Ryba ch044 Diagram Conventions "Sue" "Bill" "Les" "Jill" a b c d e
5
Kruse/Ryba ch045 Singly Linked Structures Fred 225-3465 $1,254.65 Harry 225-8122 $2,436.98 Sally 335-2145 $2,332.45 Joe 225-2311 $1,496.00 Larry 336-8865 $2,221.66
6
Kruse/Ryba ch046 Dynamic Objects MEMORY Run-time stack Heap Item* itemPtr; itemPtr itemPtr = NULL; itemPtr = new Item;. *itemPtr = someItem;
7
Kruse/Ryba ch047 Dynamic Objects MEMORY Run-time stack Heap Item* itemPtr; itemPtr itemPtr = NULL; itemPtr = new Item;. *itemPtr = someItem; delete itemPtr;
8
Kruse/Ryba ch048 Dynamic Objects itemPtr = new Item; In C, returns 0 when fails In ANSI C++ throws exception itemPtr = new(nothrow) Item; Same effect as in C Must have #include using namespace std;
9
Kruse/Ryba ch049 Dynamically Allocated Arrays int size, i; int* dynamicArray; cout > size; dynamicArray = new int[size]; for (i = 0; i<size; i++) dynamicArray[i] = i;
10
Kruse/Ryba ch0410 Pointer Assignment "Sue" "Bill" a b a = b; "Sue" "Bill" a b a b *a = *b;
11
Kruse/Ryba ch0411 Addresses of Automatic Objects Item x; Item* y; y = &x; x y y
12
Kruse/Ryba ch0412 Dynamically Allocated Singly Linked List Fred 225-3465 $1,254.65 Harry 225-8122 $2,436.98 Sally 335-2145 $2,332.45 Joe 225-2311 $1,496.00 Larry 336-8865 $2,221.66
13
Kruse/Ryba ch0413 struct Node //class Node { public: // data members NodeEntry entry; Node *next; // constructors Node(); Node(NodeEntry item, Node* addOn = NULL); }; Node
14
Kruse/Ryba ch0414 Constructors Node::Node() { next = NULL; } Node::Node(NodeEntry item, Node* addOn) { entry = item; next = addOn; }
15
Kruse/Ryba ch0415 Sample Code Node firstNode('a'); // Node firstNode stores data 'a'. Node *p0 = &firstNode; // p0 points to firstNode.Node Node *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.
16
Kruse/Ryba ch0416 Linked Stack class Stack { public: Stack(); void empty() const; void push(const Stack_entry &item); void top(Stack_entry &item) const; protected: Node *top_node; };
17
Kruse/Ryba ch0417 Why a data type with only one element? Maintain encapsulation Maintain logical distinction between stack and top of stack Maintain consistency with other data structures Helps with debugging.
18
Kruse/Ryba ch0418 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
19
Kruse/Ryba ch0419 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; }
20
Kruse/Ryba ch0420 Linked Stack- destructor /* Post: The Stack is cleared. */ Stack::~Stack() // Destructor { while (!empty()) pop(); }
21
Kruse/Ryba ch0421 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 xy
22
Kruse/Ryba ch0422 continued Now, assume x and y are pointers. 1020 xy If we have x = y; 10 20 x yy This is called Reference Semantic
23
Kruse/Ryba ch0423 Now, if we have delete x; what happens to y? y y is deleted too. Even if we did not intended to do so.
24
Kruse/Ryba ch0424 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. }
25
Kruse/Ryba ch0425 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 2010 x 2010 y 5 15
26
Kruse/Ryba ch0426 /*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
27
Kruse/Ryba ch0427 Improved Linked Stack class 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
28
Kruse/Ryba ch0428 ADT stack Create the stack, leaving it empty Test whether the stack is empty Push a new entry onto the top of the stack, provided it is not full Pop the entry off the top of the stack, provided the stack is not empty Retrieve the top entry off the stack, provided the stack is not empty
29
Kruse/Ryba ch0429 Linked Queues class 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
30
Kruse/Ryba ch0430 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); };
31
Kruse/Ryba ch0431 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; }
32
Kruse/Ryba ch0432 ADT queue Create the queue, leaving it empty Test whether the queue is empty Append a new entry onto the rear of the queue, provided it is not full Serve (and remove) the entry off the front of the queue, provided the queue is not empty Retrieve the front entry off the queue, provided the queue is not empty
33
Kruse/Ryba ch0433 ADT extended queue Everything that a queue does Determine whether the queue is full or not Find the size of the queue Serve and retrieve the front entry in the queue, provided the queue is not empty Clear the queue to make it empty
34
Kruse/Ryba ch0434 Catch Everything From Chapter 4
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.