User submissions are the sole responsibility of contributors, with TuteeHUB disclaiming liability for accuracy, copyrights, or consequences of use; content is for informational purposes only and not professional advice.
I am new to teaching the particular syllabus I have been asked to teach and I am confused by the approach given for several of the algorithms. For example the stack implementation based on the course materials given below seems very convoluted. I'm curious to hear if others would share my concerns if they had to teach about stacks in this way, or if they can offer any insight into the advantages of this approach.
The concerns I have which I'm curious to hear your opinion about are:
Using the call by reference approach makes the push and pop calls very unwieldy
It looks like new items are added to the end of the list, making the algorithm O(n) rather than O(1)
It is possible to implement a stack in a much simpler fashion, which would allow student to focus more on conceptual understanding of the behavior and use of stacks, and to see the link between the concept and the implementation more clearly.
# NullPointer should be set to -1 if using array element with index 0
NULLPOINTER =-1#Declare record type to store data and pointerclassNode:def __init__(self):
self.Data=""
self.Pointer= NULLPOINTER
defInitialiseStack():Stack=[Node()for i in range(8)]TopOfStack= NULLPOINTER # set start pointerFreeListPtr=0# set starting position of free istforIndexin range(7):Stack[Index].Pointer=Index+1Stack[7].Pointer= NULLPOINTER # last node of free listreturn(Stack,TopOfStack,FreeListPtr)defPush(Stack,TopOfStack,FreeListPtr,NewItem):ifFreeListPtr!= NULLPOINTER:# there is space in the array# take node from free list and store data itemNewNodePtr=FreeListPtrStack[NewNodePtr].Data=NewItemFreeListPtr=Stack[FreeListPtr].Pointer# insert new node at top of stackStack[NewNodePtr].Pointer=TopOfStackTopOfStack=NewNodePtrelse:print("no space for more data")return(Stack,TopOfStack,FreeListPtr)defPop(Stack,TopOfStack,FreeListPtr):ifTopOfStack== NULLPOINTER:print("no data on stack")Value=""else:Value=Stack[TopOfStack].DataThisNodePtr=TopOfStackTopOfStack=Stack[TopOfStack].PointerStack[ThisNodePtr].Pointer=FreeListPtrFreeListPtr=ThisNodePtrreturn(Stack,TopOfStack,FreeListPtr,Value)defOutputAllNodes(Stack,TopOfStack):CurrentNodePtr=TopOfStack# start at beginning of listifTopOfStack==
No matter what stage you're at in your education or career, TuteeHUB will help you reach the next level that
you're aiming for. Simply,Choose a subject/topic and get started in self-paced practice
sessions to improve your knowledge and scores.
manpreet
Best Answer
3 years ago
I am new to teaching the particular syllabus I have been asked to teach and I am confused by the approach given for several of the algorithms. For example the stack implementation based on the course materials given below seems very convoluted. I'm curious to hear if others would share my concerns if they had to teach about stacks in this way, or if they can offer any insight into the advantages of this approach.
The concerns I have which I'm curious to hear your opinion about are:
Using the call by reference approach makes the
pushandpopcalls very unwieldyIt looks like new items are added to the end of the list, making the algorithm O(n) rather than O(1)
The algorithm is hard to follow (IMO)
It is possible to implement a stack in a much simpler fashion, which would allow student to focus more on conceptual understanding of the behavior and use of stacks, and to see the link between the concept and the implementation more clearly.