Professional Documents
Culture Documents
Linked SQ
Linked SQ
Linked Structures
ItemType* itemPtr;
...
itemPtr = new ItemType;
*itemPtr = newItem;
Dynamic allocation of each
stack element (cont.)
• How should we preserve the order of the
stack elements?
Chaining the stack elements
together
Chaining the stack elements
together (cont.)
• Each node in the stack should contain two
parts:
– info: the user's data
– next: the address of the next element in the
stack
Node Type
template<class ItemType>
struct NodeType {
ItemType info;
NodeType* next;
};
First and last stack elements
• We need a data member to store the pointer
to the top of the stack
template<class ItemType>
class StackType {
public:
StackType();
~StackType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Push(ItemType);
void Pop(ItemType&);
private:
NodeType<ItemType>* topPtr;
};
Pushing on a
non-empty
stack
Pushing on a non-empty stack
(cont.)
• The order of changing the pointers is very
important !!
Pushing on an empty stack
Function Push
template <class ItemType>
void StackType<ItemType>::Push(ItemType
item)
{
NodeType<ItemType>* location;
Need to use a
temporary
pointer
Function Pop
template <class ItemType>
void StackType<ItemType>::Pop(ItemType& item)
{
NodeType<ItemType>* tempPtr;
item = topPtr->info;
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
Popping the last element on the stack
Other Stack functions
template<class ItemType>
StackType<ItemType>::StackType()
{
topPtr = NULL;
}
template<class ItemType>
void StackType<ItemType>::MakeEmpty()
{
NodeType<ItemType>* tempPtr;
while(topPtr != NULL) {
tempPtr = topPtr;
topPtr = topPtr->next;
delete tempPtr;
}
}
Other Stack functions (cont.)
template<class ItemType>
bool StackType<ItemType>::IsEmpty() const
{
return(topPtr == NULL);
}
template<class ItemType>
bool StackType<ItemType>::IsFull() const
{
NodeType<ItemType>* location;
location = new NodeType<ItemType>;
if(location == NULL)
return true; template<class ItemType>
else { StackType<ItemType>::~StackType()
delete location; {
return false; MakeEmpty();
} }
}
Copy Constructors for stacks
• Suppose we want to make a copy of a stack, will
the following work?
template<class ItemType>
void StackType(StackType<ItemType> oldStack,
StackType<ItemType>& copy)
{
StackType<ItemType> tempStack;
while(!tempStack.IsEmpty()) {
ItemType item;
tempStack.Pop(item);
copy.Push(item);
while(!oldStack.IsEmpty()) {
}
oldStack.Pop(item);
}
tempStack.Push(item);
}
Copy Constructors (cont.)
• Shallow Copy: an object is copied to another
object without copying any pointed-to data
• Deep Copy: makes copies of any pointed-to
data
template<class ItemType>
class QueueType {
public:
QueueType();
~QueueType();
void MakeEmpty();
bool IsEmpty() const;
bool IsFull() const;
void Enqueue(ItemType);
void Dequeue(ItemType&);
private:
NodeType<ItemType>* qFront;
NodeType<ItemType>* qRear;
};
Enqueuing (non-empty queue)
Enqueuing (empty queue)
• We need to make qFront point to the new
node also
qRear = NULL
newNode
Function Enqueue
template <class ItemType>
void QueueType<ItemType>::Enqueue(ItemType
newItem)
{
NodeType<ItemType>* newNode;
tempPtr = qFront;
item = qFront->info;
qFront = qFront->next;
if(qFront == NULL)
qRear = NULL;
delete tempPtr;
}
qRear, qFront revisited
template<class ItemType>
bool QueueType<ItemType>::IsFull() const
{
NodeType<ItemType>* ptr;
ptr = new NodeType<ItemType>;
if(ptr == NULL)
return true;
else {
delete ptr;
return false;
}
}
Other Queue functions (cont.)
template<class ItemType>
QueueType<ItemType>::~QueueType()
{
MakeEmpty();
}
A circular linked queue design
Comparing queue implementations
• Memory requirements
– Array-based implementation
• Assume a queue (size: 100) of strings (80 bytes
each)
• Assume indices take 2 bytes
• Total memory: (80 bytes x 101 slots) + (2 bytes x 2
indexes) = 8084 bytes
– Linked-list-based implementation
• Assume pointers take 4 bytes
• Total memory per node: 80 bytes + 4 bytes = 84
bytes
Comparing
queue
implementations
(cont.)
Comparing queue implementations
(cont.)
• Memory requirements
– Array-based implementation
• Assume a queue (size: 100) of short integers
(2 bytes each)
• Assume indices take 2 bytes
• Total memory: (2 bytes x 101 slots) + (2 bytes x 2
indexes) = 206 bytes
– Linked-list-based implementation
• Assume pointers take 4 bytes
• Total memory per node: 2 bytes + 4 bytes = 6 bytes
Comparing
queue
implementations
(cont.)
Comparing queue implementations
Big-O Comparison of Queue Operations
Operation Array Linked
Implementation Implementation
Class constructor O(1) O(1)
MakeEmpty O(1) O(N)
IsFull O(1) O(1)
IsEmpty O(1) O(1)
Enqueue O(1) O(1)
Dequeue O(1) O(1)
Destructor O(1) O(N)