Presentation is loading. Please wait.

Presentation is loading. Please wait.

Implementing a Stack as a Linked Structure CS 308 – Data Structures.

Similar presentations


Presentation on theme: "Implementing a Stack as a Linked Structure CS 308 – Data Structures."— Presentation transcript:

1 Implementing a Stack as a Linked Structure CS 308 – Data Structures

2 Implementing stacks using arrays Simple implementation The size of the stack must be determined when a stack object is declared Space is wasted if we use less elements We cannot "push" more elements than the array can hold

3 Dynamic allocation of each stack element Allocate memory for each new element dynamically ItemType* itemPtr;... itemPtr = new ItemType; *itemPtr = newItem;

4 Dynamic allocation of each stack element (cont.) How should we preserve the order of the stack elements?

5 Chaining the stack elements together

6 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

7 Node Type template struct NodeType { ItemType info; NodeType* next; };

8 First and last stack elements We need a data member to store the pointer to the top of the stack The next element of the last node should contain the value NULL

9 Stack class specification // forward declaration of NodeType (like function prototype) template struct NodeType; template class StackType { public: StackType(); ~StackType(); void MakeEmpty(); bool IsEmpty() const; bool IsFull() const; void Push(ItemType); void Pop(ItemType&); private: NodeType * topPtr; };

10 Pushing on a non-empty stack

11 Pushing on a non-empty stack (cont.) The order of changing the pointers is very important !!

12 Pushing on an empty stack

13 Function Push template void StackType ::Push(ItemType item) { NodeType * location; location = new NodeType ; location->info = newItem; location->next = topPtr; topPtr = location; }

14 Popping the top element

15 Popping the top element (cont.) Need to use a temporary pointer

16 Function Pop template void StackType ::Pop(ItemType& item) { NodeType * tempPtr; item = topPtr->info; tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }

17 Popping the last element on the stack

18 Other Stack functions template StackType() StackType ::StackType() { topPtr = NULL; } template MakeEmpty() void StackType ::MakeEmpty() { NodeType * tempPtr; while(topPtr != NULL) { tempPtr = topPtr; topPtr = topPtr->next; delete tempPtr; }

19 Other Stack functions (cont.) template IsEmpty() bool StackType ::IsEmpty() const { return(topPtr == NULL); } template IsFull() bool StackType ::IsFull() const { NodeType * location; location = new NodeType ; if(location == NULL) return true; else { delete location; return false; } template StackType() StackType ::~StackType() { MakeEmpty(); }

20 Copy Constructors: an example using stacks Suppose we want to make a copy of a stack, will the following work? template void StackType(StackType oldStack, StackType & copy) { StackType tempStack; ItemType item; while(!oldStack.IsEmpty()) { oldStack.Pop(item); tempStack.Push(item); } while(!tempStack.IsEmpty()) { tempStack.Pop(item); copy.Push(item); }

21 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 When do you need a copy constructor? (1) When parameters are passed by value (2) Return the value of a function (return thisStack;) (3) Initializing a variable in a declaration (StackType myStack=yourStack;)

22

23 Copy constructor for stacks template Stack Type ::StackType(const StackType & anotherStack) { NodeType * ptr1; NodeType * ptr2; if(anotherStack.topPtr == NULL) topPtr = NULL; else { topPtr = new NodeType ; topPtr->info = anotherStack.topPtr->info; ptr1 = anotherStack.topPtr->next; ptr2 = topPtr; while(ptr1 !=NULL) { ptr2->next = new NodeType ; ptr2 = ptr2->next; ptr2->info = ptr1->info; ptr1 = ptr1->next; } ptr2->next = NULL; } Alternatively, copy one stack to another using the assignment operator (you need to overload it though!!)

24 Comparing stack implementations Big-O Comparison of Stack Operations OperationArray Implementation Linked Implementation Class constructorO(1) MakeEmptyO(1)O(N) IsFullO(1) IsEmptyO(1) PushO(1) PopO(1) DestructorO(1)O(N)

25 Exercises 2, 3


Download ppt "Implementing a Stack as a Linked Structure CS 308 – Data Structures."

Similar presentations


Ads by Google