1 / 34

Linked Stacks and Queues

Linked Stacks and Queues. Pointers and Linked Structures Linked Stacks Linked Stacks with Safeguards Linked Queues Application: Polynomials Arithmatic Abstract Data Types and Implementations. Pointers. Pointers. Links or Address or Pointer Sometimes called a reference (handle)

caron
Télécharger la présentation

Linked Stacks and Queues

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Linked Stacks and Queues Pointers and Linked StructuresLinked StacksLinked Stacks with SafeguardsLinked Queues Application: Polynomials Arithmatic Abstract Data Types and Implementations Kruse/Ryba ch04

  2. Pointers Kruse/Ryba ch04

  3. Pointers • Links or Address or Pointer • Sometimes called a reference (handle) • Machine address Kruse/Ryba ch04

  4. Diagram Conventions a "Sue" "Les" b "Bill" c "Jill" d e Kruse/Ryba ch04

  5. Singly Linked Structures Fred Harry 225-3465 225-8122 $1,254.65 $2,436.98 Sally Joe Larry 335-2145 225-2311 336-8865 $2,332.45 $1,496.00 $2,221.66 Kruse/Ryba ch04

  6. Item* itemPtr; itemPtr itemPtr = NULL; itemPtr = new Item; . . . *itemPtr = someItem; Dynamic Objects MEMORY Run-time stack Heap Kruse/Ryba ch04

  7. Item* itemPtr; itemPtr Dynamic Objects MEMORY Run-time stack itemPtr = NULL; itemPtr = new Item; Heap . . . *itemPtr = someItem; delete itemPtr; Kruse/Ryba ch04

  8. 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 <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 = newint[size];for (i = 0; i<size; i++)dynamicArray[i] = i; Kruse/Ryba ch04

  10. a a "Sue" "Bill" b b "Bill" "Bill" a "Sue" b "Bill" a = b; *a = *b; Pointer Assignment Kruse/Ryba ch04

  11. Addresses of Automatic Objects x Item x; Item* y; y = &x; y y Kruse/Ryba ch04

  12. Dynamically Allocated Singly Linked List Fred Harry 225-3465 225-8122 $1,254.65 $2,436.98 Sally Joe Larry 335-2145 225-2311 336-8865 $2,332.45 $1,496.00 $2,221.66 Kruse/Ryba ch04

  13. Node structNode //class Node { public: // data members NodeEntryentry; Node *next; // constructors Node(); Node(NodeEntryitem, Node* addOn = NULL);}; Kruse/Ryba ch04

  14. Constructors Node::Node(){ next = NULL;} Node::Node(NodeEntry item, Node* addOn){ entry = item; next = addOn;} Kruse/Ryba ch04

  15. Sample Code Node 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 Stack • class Stack {public: Stack();void empty() const;voidpush(constStack_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 encapsulation • Maintain logical distinction between stack and top of stack • Maintain consistency with other data structures • Helps 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(constStack_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.voidStack::pop(){ Node *old_top = top_node;if (top_node == NULL) throw underflow_error(“A bummer”);top_node = old_top->next;deleteold_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. x 10 y 20 x y 20 20 Kruse/Ryba ch04

  22. continued • Now, assume x and y are pointers. 10 20 x y If we have x = y; 10 x This is called Reference Semantic 20 y y 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 nodesnew_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 elsewhile (!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 15 x x 5 15 20 10 y y 20 10 20 10 Kruse/Ryba ch04

  26. Linked Stack- Copy Constructor • /*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} Kruse/Ryba ch04

  27. Improved Linked Stack • class Stack • {public:// Standard Stack methods Stack();void empty() const;voidpush(constStack_entry &item);voidpop();voidtop(Stack_entry&item) const;//Safety features for linked structures ~Stack(); Stack(const Stack &original);voidoperator =(const Stack &original);protected: Node *top_node;};//end class Stack Kruse/Ryba ch04

  28. 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 Kruse/Ryba ch04

  29. Linked Queues • class Queue • {public:// standard Queue methods Queue();void empty() const;voidappend(constQueue_entry &item);voidserve();voidretrieve(Queue_entry&item) const;// safety features for linked structures ~Queue(); Queue(constQueue &original);void operator =(const Queue &original);protected: Node *front, *rear;};//end class Queue Kruse/Ryba ch04

  30. Extended Queue(A little inheritance) • classExtended_queue: public Queue • { • public: • boolfull() const; • intsize() const; • voidclear(); • voidserve_and_retrieve(Queue_entry item); • }; Kruse/Ryba ch04

  31. size() • intExtended_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 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 Kruse/Ryba ch04

  33. 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 Kruse/Ryba ch04

  34. Catch Everything From Chapter 4 Kruse/Ryba ch04

More Related