Upload
colin-bodman
View
213
Download
0
Embed Size (px)
Citation preview
Our Focus
Only link-list based implementation of Stack class
Won’t talk about different implementations of Queue class, see Textbook for details!
How does a Stack Work?
Last-in-First-out (LIFO) data structure Adding an item
Push operation Removing an item
Pop operation Properties
Ordered collection of items Be accessed at only the top
Building a Stack Class
Design a stack class stack.h (header file)
Implement the stack class stack.cpp (implementation file)
Building a Stack Class
Need to determine the data structure to store data items (array or linked list?)
Need to determine the algorithms to perform operations on the data items
Building a Stack Class
What data items do we need? An array/linked-list to hold stack elements An integer/pointer to indicate the top of stack
What operations do we need? Constructor: build an empty stack Empty: check if a stack is empty Push: add an element on the top Top: return the top element Pop: remove the top element Display: show all the stack elements
A Static Array-Based Stack (p327)
A static array to hold the stack elements, the position 0 as the bottom of the stack
Problem? the stack capacity cannot be changed during run time (waste of space or insufficient room for more data items)
Dynamic Array-Based Stack
Advantage: allow users to specify the stack capacity in its declaration
But, we need to do more ! (Due to the dynamically-allocated memory) Constructor: memory allocation and data
member initialization Destructor: reclaim the dynamically-
allocated memory, avoid “memory leak” Copy constructor: NO “shallow copy” (p339) Assignment operator: assign one object to
another, NO “shallow copy”
Example Codes Explanation (p336)
Figure7.6: const keyword Destructor (p338-9, Figure 7.7)
Deallocate array allocated in constructor Avoid memory leak problem
Example Codes Explanation
Copy constructor (p339) Initialization Passing value parameter Return a function value Creating a temporary storage value
Default copy constructor: member-by-member copy
Ensure deep copy
Example Codes Explanation
Assignment operator (=) The default assignment operator only
member-by-member copy, causing “shallow copy” and “memory leak” (by old array)
Ensure deep copy Check if it is self-assignment (Fig 7.9, p343)
If NO, then destroy the old array, allocate a new one
Never forget: return *this;
Let’s do it
typedef int DataType; class Stack { private: int myCapacity; int myTop; int* myArray; public: ……. };
Linked List-Based Stack
Advantage: grow and shrink as needed Need only one data member:
Pointer myTop Nodes allocated (but not
part of stack class) Node declaration in
Fig 7.11 (p. 353)
Question: Is head or tail of a linked list used for stack top myTop ?
Implementing Linked Stack Operations
Constructor: simply assign null pointer to myTop
Empty: check myTop == NULL (0) Push: insertion at the
head of the list Top: return the data
to which myTop points
View definitions in
Fig. 7.12
View definitions in
Fig. 7.12
Implementing Linked Stack Operations
Pop Delete first node in the
linked listptr = myTop;myTop = myTop->next;delete ptr;
Output Traverse the list
for (ptr = myTop; ptr != 0; ptr = ptr->next) out << ptr->data << endl;
View definitions in
Fig. 7.12
View definitions in
Fig. 7.12
Stack.h
typedef int DataType;class Stack {
public: Stack(); Stack(const Stack& org); void push(const DataType& v); void pop(); DataType top() const; ~Stack();
private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { }
}; typedef Node* NodePtr;
NodePtr myTop;
};
Exercises: implementation
Close Textbook and Notes Copy constructor? Assignment Operator? Destructor?
Implementing Linked Stack Operations
Destructor (p. 361) Traverse list to deallocate nodes “Never burn bridges before crossing them”
Copy constructor (p. 360) Traverse linked list, copying each into new
node Deep copy Watch for the empty object to be copied
Implementing Linked Stack Operations
Assignment operator (=), p.361 Rule out self-assignment Destroy the old list, this->~Stack(); code
reuse Similar to copy constructor
Introduction to Queue
A sequence of data items (FIFO) Items can be removed only at the front Items can be added only at the back
Introduction to Queue
Basic operations Construct a queue Check if empty Enqueue: add an element to back Dequeue: remove an element from front Front: return the first element
Skip array-based queues Linked-list based queues
Data members? Operations: above
Linked List-Based Queue (Chp. 8.3)
Advantage: grow and shrink as needed Two data members: myFont, myBack
Why need myBack? Avoid list traversal when enqueue()
Queue.h
typedef int DataType;
class Queue {public: //constructor
//… member functions private: class Node { public: DataType data; Node* next; Node(DataType v, Node* p) : data(v), next(0) { }
};
typedef Node* NodePtr; NodePtr myFront, myback;
};
Linked Queue, p.418
Constructor: initializesmyFront and myBack
Front, p420 return myFront->data
Dequeue, p421 Delete the first node (watch for empty
queue) Equeue, p420
Insert the node at the back
Linked Queue
Copy constructor: deep copy, p418 Assignment operator, p419
Watch for self-assignment Deep copy Avoid memory leak
Destructor: reclaim memory, p418
Circular Linked Queue, p423
Treat the linked list as circular
Last node points to the first node Alternatively keep pointer to last node
rather than first node, so only needs one data member!
Application of Queues
Disk Scheduling in OS Disk requests from OS Disk has a queue Disk serves requests in queue by FIFO In reality, it may be not always FIFO,
priority?