stacks fundamentals

Download Stacks fundamentals

If you can't read please download the document

Upload: greatqadirgee4u

Post on 16-Apr-2017

5.348 views

Category:

Education


0 download

TRANSCRIPT

Data Communication

CIS-122 : Data Structures Lecture- #1On Stacks

Conducted bySyed Muhammad Haroon , SE

Pakistan Institute of Engineering & Applied SciencesDepartment of Computer & Information Sciences

1

OutlineRevision of Data Structures Lecture # 01DefinitionRepresentation of StackOperations on StackLecture # 02Applications of StackArithmetic ExpressionRecursionFactorialQuick SortAssignmentStack MachinesTower of HanoiRecord Management

2

IntroductionDynamic data structuresData structures that grow and shrink during executionLinked listsAllow insertions and removals anywhere StacksAllow insertions and removals only at top of stackQueuesAllow insertions at the back and removals from the front Binary treesHigh-speed searching and sorting of data and efficient elimination of duplicate data items

3

Self-Referential StructuresSelf-referential structuresStructure that contains a pointer to a structure of the same typeCan be linked together to form useful data structures such as lists, queues, stacks and treesTerminated with a NULL pointer (0)

10

15

NULL pointer (points to nothing)

Data member and pointer

4

Dynamic Memory AllocationDynamic memory allocationObtain and release memory during execution

5

Linked ListsLinked list Linear collection of self-referential class objects, called nodesConnected by pointer linksAccessed via a pointer to the first node of the listSubsequent nodes are accessed via the link-pointer member of the current nodeLink pointer in the last node is set to null to mark the lists endUse a linked list instead of an array whenYou have an unpredictable number of data elementsYour list needs to be sorted quickly

6

Types of Linked ListsSingly linked listBegins with a pointer to the first nodeTerminates with a null pointerOnly traversed in one directionCircular, singly linkedPointer in the last node points back to the first nodeDoubly linked listTwo start pointers first element and last elementEach node has a forward pointer and a backward pointerAllows traversals both forwards and backwardsCircular, doubly linked listForward pointer of the last node points to the first node and backward pointer of the first node points to the last node

7

What is a stack?It is an ordered group of homogeneous items of elements.Elements are added to and removed from the top of the stack (the most recently added items are at the top of the stack).The last element to be added is the first to be removed (LIFO: Last In, First Out).

8

BASIC STACK OPERATIONS Initialize the Stack. Pop (delete an item) Push (insert an item) Status(Empty, Full, No of Item, Item at Top)Clear the Stack Determine Stack Size

9

Stacks

10

Push Operation

Push an item onto the top of the stack (insert an item)

11

Pop operationPop an item off the top of the stack (delete an item)

12

Representation of StacksArraysFixed Size StackItem, top

Link ListDynamic StackNode: data, linkStack Header

13

14Array Representation of stacksTo implement a stack, items are inserted and removed at the same end (called the top)

To use an array to implement a stack, you need both the array itself and an integerThe integer tells you either:Which location is currently the top of the stack, orHow many elements are in the stack

14

15Pushing and poppingIf the bottom of the stack is at location 0(l), then an empty stack is represented by top = -1(l-1) or count = 0(l)

If the top of the stack is at location 9(u), then an full stack is represented by top = 9(u-1) or count = 10(u)

That isEmpty : Top < lFull : Top >= u+l-1

top = 3

or count = 40 1 2 3 4 5 6 7 8 917239744

stk:

15

Stack Definitionstruct STACK{

int count; /* keeps the number of elements in the stack */ int top; /* indicates the location of the top of the stack*/

int items[STACKSIZE]; /*array to store the stack elements*/

}

16

Pushing and poppingTo add (push) an element, either:Increment top and store the element in stk[top], orStore the element in stk[count] and increment count

To remove (pop) an element, either:Get the element from stk[top] and decrement top, orDecrement count and get the element in stk[count]

17

18After poppingWhen you pop an element, do you just leave the deleted element sitting in the array?The surprising answer is, it dependsIf this is an array of primitives, or if you are programming in C or C++, then doing anything more is just a waste of timeIf you are programming in Java, and the array contains objects, you should set the deleted array element to nullWhy? To allow it to be garbage collected!top = 2

or count = 30 1 2 3 4 5 6 7 8 917239744

stk:

18

19Linked-list implementation of stacksSince all the action happens at the top of a stack, a singly-linked list (SLL) is a fine way to implement itThe header of the list points to the top of the stack

44972317

myStack:Pushing is inserting an element at the front of the listPopping is removing an element from the front of the list

19

20Linked-list implementation detailsWith a linked-list representation, overflow will not happen (unless you exhaust memory, which is another kind of problem)

Underflow can happen, and should be handled the same way as for an array implementation

When a node is popped from a list, and the node references an object, the reference (the pointer in the node) does not need to be set to nullUnlike an array implementation, it really is removed--you can no longer get to it from the linked listHence, garbage collection can occur as appropriate

20

Pushing and poppingTo add (push) an element, :Link.new=topTop=newHeader=top

To remove (pop) an element, either:Ptr=top.linkItem=top.dataHeader=ptrTop=ptr

21