void ConvertITP(char exp[]){ int i; int len=strlen(exp); Node* stack=(Node*)malloc(sizeof(Node)); stack->top=-1; stack->capacity=len; stack->Array=(int*)malloc(sizeof(int)*len); for(i=0;i<len;i++){ if(IsOperand(exp[i])){ printf("%c",exp[i]); } else if(exp[i]=='('){ Push(stack,exp[i]); } else if(exp[i]==')'){ while(!IsStackEmpty(stack) && Peek(stack)!='('){ printf("%c",Pop(stack)); } if(!IsStackEmpty(stack) && Peek(stack)!='('){ printf("\nError"); return; }else{ Pop(stack); } } else{ while(!IsStackEmpty(stack) && Pref(exp[i])<=Pref(Peek(stack))){ printf("%c",Pop(stack)); } Push(stack,exp[i]); } } while(!IsStackEmpty(stack)){ printf("%c",Pop(stack)); } }
node *Pop(void) { if (!IsStackEmpty()) return Stack[--tree]; else printf("Stack is Empty!\n"); return NULL; }
Item* Pop(Stack* stack) { if (IsStackEmpty(stack)) return NULL; Element* top = stack->top; Item* item = top->item; stack->top = stack->top->next; free(top); return item; }
//O(n^2) time and O(n) space complexity //Reverse stack using only push pop, you are not alloweded to use any other stack int ReverseStack(struct SimpleStack *s){ if(IsStackEmpty(s)){ return ; } int data = Pop(s); ReverseStack(s); InsertAtBottom(s,data); }
struct tree * Top() { if(IsStackEmpty(top)) { return NULL; } return top->next->data; }
int Pop(struct SimpleStack *s){ if(IsStackEmpty(s)){ printf("Stack UnderFlow!!\n"); return INT_MIN; } else { return s->arrayStack[s->top--]; } }
struct tree * Pop() { if(IsStackEmpty(top)) return NULL; struct stack * temp = top; struct tree * data = temp->data; top=top->next; free(temp); return data; }
void InsertAtBottom(struct SimpleStack *s, int d ){ if(IsStackEmpty(s)){ Push(s,d); return; } else{ int temp = Pop(s); InsertAtBottom(s,d); Push(s,temp); } }
int pop(struct ArrayStack *S) { if(IsStackEmpty(S)) { printf("Stack is empty"); return 0; } else { S->top = S->top--; return S->array[S->top]; } }
void Preorder_without_Recursion(struct tree * root1) { CreateStack(); while(1) { while(root1) { printf("%d ",root1->data); Push(root1); root1=root1->left; } if(IsStackEmpty()) break; root1 = Pop(); root1 = root1->right; } DeleteStack(); }
void Stack_Traverse(node *ptr) { for (;;) { while (ptr != nBuf[1]) { // ptr isn't end_node Push(ptr); ptr = ptr->left; } if (!IsStackEmpty()) { ptr = Pop(); Display(ptr); ptr = ptr->right; } else break; } }
void * RemoveFromStack( StackNode **ppStackTop ) { if (NULL == ppStackTop) return NULL; if (IsStackEmpty(ppStackTop)) return NULL; void *data = NULL; StackNode *temp = *ppStackTop; *ppStackTop = (*ppStackTop)->pNext; data = temp->Data; free(temp); temp = NULL; return data; }
/******************************************************************************************** ** Implementation of a Scan Line Seed Fill Algorithm ** Taken from Procedural Elements of Computer Graphics: David Rogers ********************************************************************************************/ void Fill(int _x, int _y, int _FillColour, int _BoundaryColour) { register int x, y ; register int BoundaryColour = _BoundaryColour; register int PixelColour, FillColour = _FillColour ; int XRight, XLeft ; int SaveX, SaveY ; // temp variable XYPixel aPoint, aPoint1 ; // temp var Next = XYStack ; // initialise to start of stack aPoint.x = _x ; aPoint.y = _y ; PushPixel(aPoint) ; // push the seed while(!IsStackEmpty()) // keep going until no more items on the stack { PopPixel(&aPoint) ; // get a point from the stack x = aPoint.x ; y = aPoint.y ; graphics_write_pixel(x, y, FillColour); // fill the point in the fill colour // fill the span to the right of the seed value SaveX = x++ ; // save the x coord of the the point we just filled and move one pixel right while((char)(ReadAPixel(x,y)) != (char)(BoundaryColour)) // if new pixel is not the boundary colour graphics_write_pixel(x++, y, FillColour); // fill it and keep moving right along a horizontal line // must have found the boundary colour when moving right XRight = x - 1 ; // save X coord of the last filled pixel on this line when moving right x = SaveX ; // get the original starting x back // now fill the span to the left of the seed value --x ; while((char)(ReadAPixel(x,y)) != (char)(BoundaryColour)) // if new pixel is not the boundary colour graphics_write_pixel(x--, y, FillColour); // fill it and keep moving left along a horizontal line XLeft = x + 1 ; // save X coord of the last filled pixel on this line when moving left x = SaveX ; // get original x coord for the seed back /////////////////////////////////////////////////////////////////////////////////////////////////// // check that the scan line below is neither a polygon boundary nor // has been previously completely filled ////////////////////////////////////////////////////////////////////////////////////////////////// SaveY = y ; // save the current y coordinate of the line we have just drawn x = XLeft ; // starting at the left x ++y ; // move down one line // starting from the left keep moving right looking at the pixel // until we find something that is neither filled nor boundary colour as it represents something on the line that may be a pixel to fill do { PixelColour = ReadAPixel(x++,y) ; } while(((char)(PixelColour) == (char)(BoundaryColour)) || ((char)(PixelColour) == (char)(FillColour)) ); x-- ; // to get here we must have found something that needs filling i.e. the above loop found that the line below was not a complete boundary edge or filled // if we are still less than the previous right most X coord then it would be a new point that we need to seed while(x < XRight) { // seed the scan line below // if the pixel at x,y is not a boundary colour and less than extreme right // skip over any pixels already filled while(((char)(ReadAPixel(x,y)) != (char)(BoundaryColour)) && (x < XRight)) ++x ; // push the extreme right pixel onto the stack aPoint1.x = x - 1 ; aPoint1.y = y ; PushPixel(aPoint1) ; // continue checking in case the span is interrupted by another shape inside the one we are trying to fill ++x ; // skip over anything that is filled or boundary (i.e. other shape) inside the one we are trying to fill do { PixelColour = ReadAPixel(x++,y) ; } while(((char)(PixelColour) == (char)(BoundaryColour)) || ((char)(PixelColour) == (char)(FillColour)) ); x-- ; } x = SaveX ; y = SaveY ; /////////////////////////////////////////////////////////////////////////////////////////////////// // check that the scan line above is neither a polygon boundary nor // has been previously completely filled y = SaveY; x = XLeft ; --y ; do { PixelColour = ReadAPixel(x++,y) ; } while(((char)(PixelColour) == (char)(BoundaryColour)) || ((char)(PixelColour) == (char)(FillColour)) ); x-- ; while(x < XRight) // if we have not reached the boundary { // seed the scan line below while(((char)(ReadAPixel(x,y)) != (char)(BoundaryColour)) && (x < XRight)) ++x ; // look for right most x inside the boudan // push the extreme right pixel onto the stack aPoint1.x = x - 1 ; aPoint1.y = y ; PushPixel(aPoint1) ; // continue checking in case the span is interrupted ++x ; do { PixelColour = ReadAPixel(x++,y) ; } while(((char)(PixelColour) == (char)(BoundaryColour)) || ((char)(PixelColour) == (char)(FillColour)) ); x-- ; } x = SaveX ; y = SaveY ; } }
void NoRecursionQuickSort(int* pData, int nLength) { int i = 0; int j = 0; int nLeft = 0; int nRight = 0; int nTemp = 0; int nTop = 0; StackNode* pStackTop = NULL; StackData data; data.nLeft = 0; data.nRight = nLength - 1; StackPush(&pStackTop, &data); while (!IsStackEmpty(pStackTop)) { BOOL bPopSuccess = StackPop(&pStackTop, &data); if (!bPopSuccess) { break; } nLeft = data.nLeft; nRight = data.nRight; i = nLeft; j = nRight; if (nLeft < nRight) { // 把最左边的值作为中值。 nTemp = pData[nLeft]; while (i < j) { while (i < j && pData[j] > nTemp) { --j; } if (i < j) { pData[i] = pData[j]; ++i; } while (i < j && pData[i] < nTemp) { ++i; } if (i < j) { pData[j] = pData[i]; --j; } // 中值的下标位置是i; // 当前的结果是: // [nLeft, i - 1]段的数据都小于或等于i位置的数据,i位置的数据小于或等于[i + 1, nRight]段的数据。 pData[i] = nTemp; // 把[nLeft, i - 1]压入栈中,以后会取出来对该段数据进行排序 data.nLeft = nLeft; data.nRight = i - 1; StackPush(&pStackTop, &data); // 把[i + 1, nRight]压入栈中,以后会取出来对该段数据进行排序 data.nLeft = i + 1; data.nRight = nRight; StackPush(&pStackTop, &data); } } } assert(IsStackEmpty(pStackTop)); }