Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Consider a stack implemented using a doubly linked list and pointing to trees (s

ID: 3677844 • Letter: C

Question

Consider a stack implemented using a doubly linked list and pointing to trees (similar to what we needed for project 2. Give pseudo code for push () and pop () (20 points, 10 points each function) Be sure to state what you expect each function to take as arguments and what each function will return. You should use the following structure for your stack, typedef struct stacknode *stacknodeptr; struct stacknode { struct treenode *ptr; stacknodeptr next; stacknodeptr prev; } And let's assume that our stack is declared in main as: stacknodeptr Stack;

Explanation / Answer

/* Insert a node at the stack top */ void push( StackNodePtr *topPtr, int info ) { StackNodePtr newPtr; /* pointer to new node */ newPtr = malloc( sizeof( StackNode ) ); /* insert the node at stack top */ if ( newPtr != NULL ) { newPtr->data = info; newPtr->nextPtr = *topPtr; *topPtr = newPtr; } /* end if */ else { /* no space available */ printf( "%d not inserted. No memory available. ", info ); } /* end else */ } /* end function push */ /* Remove a node from the stack top */ int pop( StackNodePtr *topPtr ) { StackNodePtr tempPtr; /* temporary node pointer */ int popValue; /* node value */ tempPtr = *topPtr; popValue = ( *topPtr )->data; *topPtr = ( *topPtr )->nextPtr; free( tempPtr ); return popValue; } /* end function pop */