Exemplo n.º 1
0
int main()
{
	// Let's make a stack -- Always a good idea initialize variables to some known value
	SStack stack = {0};
	int counter = 0; // A counter for our "for loops"
	
	InitStack(&stack,10);


	// Let's fill the stack with numbers 0 - 9.
	// When this for loop is done the stack will be FULL.
	for(counter = 0; counter < 10; counter++)
		Push(&stack,counter);
	
	if(Push(&stack,22))
		printf("This is we bad! We just pushed an element onto a full stack.\n\n");

	else
		printf("This is good!  We were denied while trying to \"push\" onto a full stack.\n\n");


	// We'll print out the numbers.  You'll see there in LIFO order (The last one we
	// pushed on, is the first one we'll pop off, etc).
	for(counter = 0; counter < 10; counter++)
		printf("We just popped off #%d\n",Pop(&stack));
		
	
	if(FreeStack(&stack))
		printf("\nYeah the memory was freed successfully!\n\n");
	else
		printf("\nOh NO!  An error occurred while trying to free the memory");

	return 0;
}
Exemplo n.º 2
0
// 从尾到头打印单链表
void ListPrintReverseOrder(ListNode * head)
{
	/* 递归解法 
	if(head == NULL) {
		return ;
	} else {
		ListPrintReverseOrder(head->next);
		printf("%d\t", head->value);
	}
	*/

	// 压栈解法开始
	SqStack * s;
	s = InitStack(s);

	ListNode * p = head;
	while(p != NULL) {
		Push(s, *p);
		p = p->next;
	}

	print(s);

	ElemType node;
	while(s->top != s->base) {
		Pop(s, &node);
		printf("%d\t", node);
	}
	printf("\n");

	FreeStack(s);
	// 压栈解法结束
}
Exemplo n.º 3
0
void IteratorStack::Reset()
{
  FreeStack(1); // leave at most the top element on stack
  StackableIterator* top = Top();
  if (top) {
    top->Reset();
    if (traversalOrder == PostOrder) 
      Push(IteratorToPushIfAny(top->CurrentUpCall()));
  }
}
void UpdateConflictingSimplicies(Vertex* point, DelaunayTriangulation* dt)
{
  int i;
  
  Simplex* simplex = FindContainingSimplex(dt, point);
  Simplex* current;
  
  Stack* toCheck = NewStack();
  Push(toCheck, simplex);

  while (!IsEmpty(toCheck))
  {
    current = (Simplex*)Pop(toCheck);
    
    int isDel = IsDelaunay(current,point); 
    
    if (isDel == -1) 
    {     
      int i = 0;
      while( isDel == -1 )
      {
        RandomPerturbation(point,i);
        isDel = IsDelaunay(current,point);
        
		i++;
      }   
      
      FreeStack(toCheck,nullptr);
      EmptyArrayList(dt->m_Conflicts);
      UpdateConflictingSimplicies(point,dt);
      return;
    }
    
    if ((!isDel) && (!ArrayListContains(dt->m_Conflicts, current)))
    {
      AddToArrayList(dt->m_Conflicts, current);
      for (i = 0; i < 4; i++)
        if (current->m_NeighbourSimplices[i])
          Push(toCheck, current->m_NeighbourSimplices[i]);
    }
  }
  FreeStack(toCheck,nullptr);
}
Exemplo n.º 5
0
double Calc_Rev_Polish(const char *expr)
{
    Node *mid = NULL;
    Node *rev = NULL;

    InitStack(&mid);
    InitStack(&rev);

    Parse_Expr(mid,expr);
    Reverse(mid);

    Mid_To_Rev(mid,rev);
    Reverse(rev);

    double res = Calc_By_Rev(rev);
    FreeStack(mid);
    FreeStack(rev);

    return res;
}
Exemplo n.º 6
0
void wxStackWalker::Walk(size_t skip, size_t maxDepth)
{
    // read all frames required
    SaveStack(maxDepth);

    // process them
    ProcessFrames(skip);

    // cleanup
    FreeStack();
}
Exemplo n.º 7
0
double Calc_Mid_Polish(const char *expr)
{
    Node *mid = NULL;
    InitStack(&mid);

    Parse_Expr(mid,expr);
    Reverse(mid);

	double res = Calc_By_Mid(mid);
	FreeStack(mid);

    return res;
}
Exemplo n.º 8
0
int main() {
    int i = 0;
    Stack s;
    NewStack(&s);
    for(i = 0; i < 10; ++i) {
        StackPush(&s, i);
    }

    for(i = 0; i < s.count; ++i) {
        printf("the push num is:%d\n", s.array[i]);
    }
    FreeStack(&s);
    return 0;
}
void FreeDelaunayTriangulation(DelaunayTriangulation* dt)
{
  free(dt->m_AlphaSimplex);
  FreeStack(dt->m_RemovedSimplices, free);
  
  while(!IsEmpty(dt->m_RemovedVoronoiCells))
  {
    VoronoiCell* voronoiCell = (VoronoiCell*)Pop(dt->m_RemovedVoronoiCells);
    int i;
    for (i = 0;i < voronoiCell->m_NumPointsAlloc; i++)
      free(voronoiCell->m_Points[i]);
    free(voronoiCell->m_Points);
    FreeArrayList(voronoiCell->m_Vertices, nullptr);
    free(voronoiCell);
  }
  
  FreeStack(dt->m_RemovedVoronoiCells, nullptr);
  FreeLinkedList(dt->m_Simplices, free);
  FreeArrayList(dt->m_Conflicts, free);
  FreeArrayList(dt->m_Updates, nullptr);
  FreeNeighbourUpdates(dt->m_NeighbourUpdates);
  free(dt); 
}
Exemplo n.º 10
0
int main(void)
{
     InitStack(256);
     Push(7);
     Push(0);
     Push(6);
     Push(2);
     Push(9);
     printf("%d\n",Pop());
     printf("%d\n",Pop());
     printf("%d\n",Pop());
     printf("%d\n",Pop());
     printf("%d\n",Pop());
     FreeStack();
    
    return 0;
}
Exemplo n.º 11
0
int main()
{
	/*Create an empty stack and variable placeholder*/
	int numberRead;
	StackHndl TheStack;
	TheStack = NULL;
	TheStack = NewStack();
	
	printf("Input a non-negative integer to be converted into binary: \n");
	while(scanf("%d", &numberRead) == 1)
	{
		/*Negative numbers are alerted as errors and not converted*/
		if(numberRead < 0) 
		{
			printf("%d is a negative integer! Try again.\n", numberRead);
			continue;
		}
		
		/*Zero cases are handled separately*/
		if(numberRead == 0) Push(TheStack, 0);
		
		/*Loop that iterates down the number*/
		while(numberRead > 0)
		{
			if(numberRead%2 != 0) Push(TheStack, 1);
			else Push(TheStack, 0);
			numberRead = numberRead/2;
		}
		
		/*PRINTING SECTION*/
		printf("The binary representation of that is: ");
		while(!IsEmpty(TheStack))
		{
			printf("%d",Top(TheStack));
			Pop(TheStack);
		}
		printf("\n");
	}

	FreeStack(&TheStack);
	return 0;
}
ArrayList* FindNeighbours(Vertex* point, Simplex* simplex)
{
  ArrayList* l   = NewArrayList(); 
  Stack* toCheck = NewStack();

  Simplex* current;
  Push(toCheck, simplex);

  while (!IsEmpty(toCheck))
  {
    current = (Simplex*)Pop(toCheck);
    
    if ( PointOnSimplex(point, current) && (! ArrayListContains(l, current)) )
    {
      AddToArrayList(l, current);
      for (int i = 0; i < 4; i++)
        if (current->m_NeighbourSimplices[i])
          Push(toCheck, current->m_NeighbourSimplices[i]);
    }   
  }
  FreeStack(toCheck,nullptr);

  return l;   
}
Exemplo n.º 13
0
//=========================================
int FindPath(vec3_t start, vec3_t destination) {
node_t *StartNode;
node_t *BestNode;
node_t *tNode;
int NodeNumD;
int NodeNumS;
int g,c,i;
float h;
vec3_t tstart,tdest;

  VectorCopy(start,tstart);
  VectorCopy(destination,tdest);

  // Get NodeNum of start vector
  NodeNumS=GetNodeNum(tstart);
  if (NodeNumS==-1) {
	  //gi.dprintf("bad nodenum at start\n");
  return 0; // ERROR
  }

  // Get NodeNum of destination vector
  NodeNumD=GetNodeNum(tdest);
  if (NodeNumD==-1) 
  {
	 // gi.dprintf("bad nondenum at end\n");
	  return 0; // ERROR
  }

  // Allocate OPEN/CLOSED list pointers..
  OPEN=(node_t *)V_Malloc(sizeof(node_t), TAG_LEVEL);
 // OPEN=(node_t *)malloc(sizeof(node_t));
  OPEN->NextNode=NULL;

  CLOSED=(node_t *)V_Malloc(sizeof(node_t), TAG_LEVEL);
  //CLOSED=(node_t *)malloc(sizeof(node_t));
  CLOSED->NextNode=NULL;

  //================================================
  // This is our very first NODE!  Our start vector
  //================================================
  StartNode=(node_t *)V_Malloc(sizeof(node_t), TAG_LEVEL);
  //StartNode=(node_t *)malloc(sizeof(node_t));
  StartNode->nodenum=NodeNumS; // starting position nodenum
  StartNode->g=g=0; // we haven't gone anywhere yet
  StartNode->h=h=distance(start, destination);//fabs(vDiff(start,destination)); // calculate remaining distance (heuristic estimate) GHz - changed to fabs()
  StartNode->f=g+h; // total cost from start to finish
  for (c=0;c < NUMCHILDS;c++)
    StartNode->Child[c]=NULL; // no children for search pattern yet
  StartNode->NextNode=NULL;
  StartNode->PrevNode=NULL;
  //================================================

  // next node in open list points to our starting node
  OPEN->NextNode=BestNode=StartNode; // First node on OPEN list..

  //GHz - need to free these nodes too!
  //NodeList[NodeCount++] = OPEN;
//  NodeList[NodeCount++] = CLOSED;
  NodeCount+=2;

  for (;;) {
    tNode=BestNode; // Save last valid node
    BestNode=(node_t *)NextBestNode(NodeNumS, NodeNumD); // Get next node from OPEN list
    if (!BestNode) {
		//gi.dprintf("ran out of nodes to search\n");
		return 0;//GHz
     // BestNode=tNode; // Last valid node..
     // break;
	}

    if (BestNode->nodenum==NodeNumD) break;// we there yet?
    ComputeSuccessors(BestNode,NodeNumD);} // Search from here..

  //================================================

     RemoveDuplicates(BestNode, CLOSED);//FIXME: move this up before the start==end crash check

 // gi.dprintf("%d: processed %d nodes\n", level.framenum,NodeCount);
  if (BestNode==StartNode) {  // Start==End??
    FreeStack(StartNode);//FIXME: may cause crash
	//gi.dprintf("start==end\n");
    return 0; }

    


  //gi.dprintf("Start = %d End = %d\n", NodeNumS, NodeNumD);
 // gi.dprintf("Printing tNode (in reverse):\n");
 // PrintNodes(BestNode, true);
 // gi.dprintf("Printing OPEN list:\n");
  //PrintNodes(OPEN, false);
  //gi.dprintf("Printing CLOSED list:\n");
 // PrintNodes(CLOSED, false);

BestNode->NextNode=NULL; // Must tie this off!


  // How many nodes we got?
   tNode=BestNode;
  i=0;
  while (tNode) {
    i++; // How many nodes?
    tNode=tNode->PrevNode; }

  if (i <= 2) { // Only nodes are Start and End??
    FreeStack(BestNode);//FIXME: may cause crash
	//gi.dprintf("only start and end nodes\n");
    return 0; }

  // Let's allocate our own stuff...

 
  //CLOSED->NextNode = NULL;//GHz - only needs to be null if we are using freestack()
  numpts=i;

  //GHz - free old memory
  //V_Free(Waypoint);

  Waypoint=(int *)V_Malloc(numpts*sizeof(int), TAG_LEVEL);
  //Waypoint=(int *)malloc(numpts*sizeof(int));

  // Now, we have to assign the nodenum's along
  // this path in reverse order because that is
  // the way the A* algorithm finishes its search.
  // The last best node it visited was the END!
  // So, we copy them over in reverse.. No biggy..

  tNode=BestNode;
  while (BestNode) {
    Waypoint[--i]=BestNode->nodenum;//GHz: how/when is this freed?
    BestNode=BestNode->PrevNode; }

// NOTE: At this point, if our numpts returned is not
// zero, then a path has been found!  To follow this
// path we simply follow node[Waypoint[i]].origin
// because Waypoint array is filled with indexes into
// our node[i] array of valid vectors in the map..
// We did it!!  Now free the stack and exit..

  //================================================

  //++++++++++ GHz NOTES +++++++++++++
  // FreeStack() is flawed because the lists have nodes that point to nodes on other lists
  // so if you free one list, then the next list will crash when it encounters a node with
  // an invalid pointer (node was freed in last list)
  //++++++++++++++++++++++++++++++++++

  FreeStack(tNode); // Release ALL resources!!

  //GHz: cleanup test/debugging
  //for (i=0;i<NodeCount;i++)
  //{
//	  V_Free(NodeList[i]);
 // }
 // OPEN = NULL;
  //CLOSED = NULL;
  NodeCount = 0;

  //TODO: performance... cpu usage is still very high
  //TODO: grid editor, save grid to disk
  //TODO: need some way of handling manually edited grid
  // because NextNode() only searches within a specific 32x32 pattern


 // gi.dprintf("%d: found %d\n",level.framenum,numpts);

  return (numpts);
}
Exemplo n.º 14
0
IteratorStack::~IteratorStack()
{
  FreeStack(0);
  delete iteratorStackRepr;
}
Exemplo n.º 15
0
void IteratorStack::ReConstruct(TraversalOrder torder, 
				IterStackEnumType _enumType) 
{
  InitTraversal(torder, _enumType);
  FreeStack(0);
}
void FreeNeighbourUpdates(NeighbourUpdate* neighbourUpdate)
{
  FreeStack(neighbourUpdate->m_Pointers, free);
  FreeStack(neighbourUpdate->m_Old,  free);
  free(neighbourUpdate);
}