1 / 14

Stacks using Linked Lists

Stacks using Linked Lists. Stack Data Structure.

nike
Télécharger la présentation

Stacks using Linked Lists

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. Stacks using Linked Lists

  2. Stack Data Structure As we already know, stacks are linear data structures. This means that their contexts are stored in what looks like a line. An array, too, is a sort of linear data structure in which you can access any element directly. However, in a stack, you can only access the element at its top.

  3. Stack Implementation with Linked Lists One disadvantage of using an array to implement a stack is the wasted space---most of the time most of the array is unused. A more elegant and economical implementation of a stack uses a linked list, which is a data structure that links together individual data objects as if they were ``links'' in a ``chain'' of data. A B C pStack

  4. Stack Implementation with Linked Lists The question here is, where should we consider the top of the stack to be, the beginning or end of the list, and why? A B C pStack

  5. Where should the top of the stack be? Since the Stack ADT uses a LIFO manner to retrieve data, the top of the stack is where all objects are added, and also retrieved from. Therefore, it is necessary that the programmer evaluates the pros and cons on figuring out if the top of the stack should be at the head of the linked list or at the tail.

  6. Top of the stack at the tail Imagine the scenario where the top of the stack is at the end of the linked list. Since the Stack ADT uses a LIFO manner to retrieve data, in this case, new objects would have to be added to the end of the linked list, and retrieved from the end of the linked list too.

  7. When the Top is at the Tail – push() In order to add a new object, it would be necessary to traverse the linked list and find the end. However, this could be taken care of with the help of a tail pointer (pTail), along with the head pointer (pStack). A B C pStack D pTail

  8. When the Top is at the Tail – push() algorithm pushStack ( object ) { allocate ( pNew ) assign object data to pNew if ( Stack is empty ) { pStack = pNew pTail = pNew } else { pTail->next = pNew pTail = pNew } increment size of stack }

  9. When the Top is at the Tail – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. A B C pStack D pTail delete node pPre

  10. When the Top is at the Tail – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. Can this be taken care of with the help of a tail pointer (pTail) and another pointer that always points to the previous pointer from the tail (pPre), along with the head pointer (pStack)? Since there is no way to go backwards with this type of linked list, you will see that you would have to traverse the whole list to get to the previous pointer of pPre. This would result in a algorithm with a big O of n O(n)!

  11. When the Top is at the Head – push() In this case, in order to add a new object, it would just add it to the head in this manner. A B C pStack D pNew

  12. When the Top is at the Head – push() algorithm pushStack ( object ) { allocate ( pNew ) assign object data to pNew pNew->next = pStack pStack = pNew; increment size of stack }

  13. When the Top is at the Head – pop() In order to delete a new object, it would be necessary to traverse the linked list and find the end. Can this be taken care of with the help of a tail pointer (pTail) and and another point that always points to the previous pointer from the tail (pPre), along with the head pointer (pStack)? A B C delete node pStack D

  14. When the Top is at the Head – pop() algorithm popStack ( object ) { if ( stack is empty ) print stack exception message else pKill = pHead pStack = pKill->next assign data from pKill to object delete pKill decrement size of stack }

More Related