Пример #1
0
int main()
	{
		struct Stack *s = createStack();
		int i, ele;
		printf("\ttop : %d \n",s->top);
		printf("\t capacity: %d\n", s->capacity);
		for (i=0; i<6; i++)
			{
			push(s, (i+1)*100);
			}
		printf(" \tStsack Elements\n\n");	
		
		printStack(s);
		reverseStack(s);
		printf(" \tStsack Elements after reverse : \n\n");	
		
		printStack(s);
		
			for (i=0; i<6; i++)
			{
			ele= pop(s);
			printf("\nFD stack:%d\n\n",ele);
			}
		printf(" \tStsack Elements\n\n");	
		printStack(s);
		
		
		return 0;
	}
Пример #2
0
void testing(int testArray[], int arrayLength)
{
    int i;
    Stack * stack;
    // create a stack
    stack = createStack();
    // loop for the length of the test data array
    for (i = 0; i < arrayLength; i++)
    {
        //  add an item to the stack
        push(stack, testArray[i]);
        //  print the stack
        printf("List print loop number %d: ", (i+1));
        printStack(stack);
    }
    //    test pop
    printf("Removing the top value from the stack...\n'%d' was just removed from the stack\n", pop(stack));
    //    test peek
    printf("The top value now is: %d\n", peek(stack));
    //    test print stack
    printf("The stack contains the following: ");
    printStack(stack);
    //    test length
    printf("The length of the stack is: %d\n", stackLength(stack));
    //    test destroy stack
    destroyStack(stack);
}
Пример #3
0
//void move(char fromPeg, char toPeg, int disk)
//{
//    printf("Move the disk %d from \'%c\' to \'%c\'\n",
//           disk, fromPeg, toPeg);
//}
void ToH(int num_of_disks, char S[], char A[], char D[])
{
	int i;
	char s = 'S';
	char a = 'A';
	char d = 'D';
	int no_of_moves = pow(2, num_of_disks) - 1;
	for(i=num_of_disks;i>=1;i--)
	{
		push(S,i);
	}
	printStack(S);
	for(i = 1; i <= no_of_moves; i++)
	{
		if(i%3==1)
		{
			movedisks(S,D,s,d);
			printStack(D);
		}
	
		else if(i%3==2)
		{
			movedisks(S,A,s,a);
		}
		else
		{
			movedisks(A,D,a,d);
		}
		
	}
	
	
}
Пример #4
0
void testLib() {
  printf("Testing stack library functions...\n");
  Stack s = createStack(3);

  printf("\nCase 1: pushing elements greater than capacity...\n");
  push(0,s); push(1,s);
  push(2,s); push(3,s);
  printStack(s);
  int success = s->top == 2;

  printf("\nCase 2: popping elements greater than length...\n");
  pop(s); pop(s);
  pop(s); pop(s);
  pop(s); pop(s);
  pop(s);
  printStack(s);
//  printf("top = %i\n",s->top);
  success = success && s->top == -1;

  printf("\nCase 3: pushing after having already enqueued and dequeued...\n");
  push(0,s); push(1,s);
  push(2,s); push(3,s);
  push(2,s); push(3,s);
  printStack(s);
//  printf("top = %i\n",s->top);
  success = success && s->top == 2;
  printf("\n");

  freeStack(s);
  if(success) printf("  ☺ success.\n");
  else printf("  ☹ failure.\n");
}
Пример #5
0
int main() {
	int i, j;
	char item[26];

	printf("빈 상태의 스택");
	printStack();

	printf("\n\n스택에 A부터 Z까지 삽입");
	for (i = 65; i < 91; i++) {
		push(i);
	}
	printStack();

	printf("\n\npop으로 역순 출력");
	for (j = 0; j < 26; j++) {
		item[j] = pop();
	}
	printf("\n ");
	for (j = 0; j < 26; j++) {
		printf("%c ", item[j]);
	}

	printf("\n\npop 이후, 빈 상태의 스택");
	printStack();

	system("pause");
	return 0;
}
Пример #6
0
int main (int argc, char *argv[]) {

  int i = 0;
  Stack *myStack = createStack();
  Node *n = NULL;

  if (myStack == NULL) {
    printf("main: Cannot create stack. Exiting!\n");
    return 0;
  }

  for (i = 0; i < 10; i++) {
    n = createNode(i);
    pushStack(myStack, n);
  }

  printStack(myStack);

  for (i = 0; i < 11; i++) {
    n = popStack(myStack);
    if (n == NULL) {
      printf("main: popped NULL!\n");
      printStack(myStack);
      break;
    }
    printf("main: popped %d\n", n->data);
    free(n);
    n = NULL;
    printStack(myStack);
  }

  return 0;
}
Пример #7
0
int main (void)
{
	Stack * s = createStack();

	int x;
	for (x = 0; x < 5; x++)
		push(s, x);

	printf ("Stack contents: '");
	printStack (s);
	printf ("'\n");

	for (x = 0; x < 2; x++)
	{
		printf ("popped: %d  stack is now: '", pop(s));
		printStack (s);
		printf ("'\n");
	}

	destroyStack (s);
	printf ("Stack contents: '");
	printStack (s);
	printf ("'\n");

	return 0;
}
Пример #8
0
int main() {
  int lenght, i;
  char str[] = "(((a))+(b))(((()()(((()()()))))";
  lenght = strlen(str);

  printStack();

  for(i = 0; i < lenght; i++){

    if(str[i] == '(') {
      push(str[i]);
      printStack();
    } else if(str[i] == ')') {
      pop();
      printStack();
    }
  }

  printStack();

  if (is_empty_stack()) TM_PRINTF("Well done, it was balanced\n", NULL);
  else TM_PRINTF("ooops, stack contains %d elements\n", stack_size);

  return 0;
}
Пример #9
0
int main() {

    push(&stack,1); 
    push(&stack,2);
    push(&stack,3);
    push(&stack,4);
    push(&stack,5);
    printStack(stack);
    pop(&stack);
    pop(&stack);
    printStack(stack);
}
Пример #10
0
int grammarParse() {
	push('$');
	push('S');
	printf("Rule\t\tStep\tStack\n");
	printf("\t\t0\t$S\n");
	int i = 0;
	char a;
	char x;
	for (i = 0; i < wordlen; ++i){
		a = wordToG(wordIndex[i]);
    FOO:
		x = pop();
		if (isVT(x)) {
			if (x == a){
				printStack();
				printf("\n");
				continue;
			}
			else {
				printf("Error1!The position is %d\n",i);return 0;
			}
		}
		else {
			if (x == '$') {
				if ( x == a) {
					printf("Success\n");break;
				}
				else {
					printf("Error2!The postition is %d\n",i);
					return 0;
				}
			}
			else {
				if (!checkMatrix(x,a,&i)) {
					printf("(x=%c,a=%c)\n", x,a);
					printf("Error3!The postition is %d\n",i);
					return 0;
				}
				else{
					printf("\b");
					printStack();
					printf("\n");
					goto FOO;
				}
			}
		}
		printStack();
		printf("\n");
	}
	return 1;
}
Пример #11
0
/* function main begins program execution */
int main( void )
{ 
   StackNodePtr stackPtr = NULL; /* points to stack top */
   int choice; /* user's menu choice */
   int value;  /* int input by user */
 
   instructions(); /* display the menu */
   printf( "? " );
   scanf( "%d", &choice );

   /* while user does not enter 3 */
   while ( choice != 3 ) { 

      switch ( choice ) { 

         /* push value onto stack */
         case 1:      
            printf( "Enter an integer: " );
            scanf( "%d", &value );
            push( &stackPtr, value );
            printStack( stackPtr );
            break;

         /* pop value off stack */
         case 2:      

            /* if stack is not empty */
            if ( !isEmpty( stackPtr ) ) {
               printf( "The popped value is %d.\n", pop( &stackPtr ) );
            } /* end if */

            printStack( stackPtr );
            break;

         default:
            printf( "Invalid choice.\n\n" );
            instructions();
            break;

      } /* end switch */

      printf( "? " );
      scanf( "%d", &choice );
   } /* end while */

   printf( "End of run.\n" );

   return 0; /* indicates successful termination */

} /* end main */
Пример #12
0
int main()
{
	STACK *S;
	S = createStack(5);
 	push(S,7);
    push(S,5);
    push(S,21);
    push(S,-1);
   
    printStack(S);
    pop(S);
    pop(S);
    printStack(S);
    return 0;
}
void SetOfStacks::printStacks() {
    for (int i = 0; i < stacks.size(); i++) {
        std::cout << "Stack " << i+1 << "\n";
        printStack(stacks.at(i));
    }
    std::cout << "\n";
}
Пример #14
0
static EncodedJSValue JSC_HOST_CALL functionPrintStack(ExecState* exec)
{
    // When the callers call this function, they are expecting to print the
    // stack starting their own frame. So skip 1 for this frame.
    printStack(exec, 1);
    return JSValue::encode(jsUndefined());
}
Пример #15
0
void printStack(struct stack * stack ){
    if ( stack == NULL ){
        return ;
    }
    printf("val: %d\n",stack->val);
    printStack(stack->bottom);
}
Пример #16
0
static bool listDataCommand(CardinalVM* vm, const char* cmd) {
	if (cmd[0] == 'b') {
		listBreakPoints(vm);
	}
	else if (cmd[0] == 'v') {
		listLocalVariables(vm);
	}
	else if (cmd[0] == 'g') {
		listGlobalVariables(vm);
	}
	else if (cmd[0] == 'm') {
		listMemberProperties(vm);
	}
	else if (cmd[0] == 's') {
		listStatistics(vm);
	}
	else if (cmd[0] == 'f') {
		printStack(vm);
	}
	else {
		printf("%s%s%s%s%s%s%s", "Unknown list option, expected one of:\n",
				"b - breakpoints\n",
				"v - local variables\n",
				"m - member properties\n",
				"g - global variables\n",
				"s - statistics\n",
				"f - stack\n");
		return false;
	}
	return true;
}
Пример #17
0
Файл: stack.c Проект: wdli/algo
void main (void)
{


  char str[MAX_SIZE];

  printf ("Please input strings, end with '-' \n");
  while (scanf("%s", str)) {
    
    printf(" Input: %s\n", str);
    if (memcmp(str,"-",1) == 0) { 
      break;
    }

    if (push(str) < 0){
      printf (" push failed\n");
      return;
    }
    printStack();

  }

  printf ("Now pop the stack\n");
  
  char * ret = NULL;
  while ((ret = pop())) {
    printf ( " %s ",ret);
  }
  printf ("\n");
  printf (" All done! \n");
}
Пример #18
0
int main()
{
   stackType stack;
   printf("Initializing stack.\n");
   initializeStack(&stack);

   printf("Pushing: %d\n", 3);
   stackPush(&stack, 3);

   printf("Popping...");
   int x = stackPop(&stack);
   if (x == 3)
     printf("success\n");
   else
     printf("********** ERROR\n");

   printf("Pushing: %d\n", 4);
   stackPush(&stack, 4);
   printf("Pushing: %d\n", 5);
   stackPush(&stack, 5);
   printf("Popping...");
   x = stackPop(&stack);
   if (x == 5)
     printf("success\n");
   else
     printf("********** ERROR\n");

   printf("Pushing: %d\n", 6);
   stackPush(&stack, 6);
   printf("Pushing: %d\n", 7);
   stackPush(&stack, 7);

   printf("Stack contents (should be 7,6,4):\n");
   printStack(&stack);
}
int main(void){

	int i, x;
	char array[] = {'(','(','a','+','b',')','+','(','(',
					'c','+','d',')',')',')'};

	size_t tam = sizeof(array) / sizeof(array[0]);

	tStack stack;

	creatStack(&stack);

	for(i=0; i<tam; i++){
		if(array[i] == '('){
			x = pushStack(&stack, array[i]);
		}
		if((array[i] == ')') && (i>0)){
			x = popStack(&stack);
		}
	}

	if(stack.size == 0){
		printf("\nAccepted\n");
	}
	else{
		printf("\nRejected\n");
	}

	printStack(&stack);


	system("pause");

	return 0;
}
Пример #20
0
int main()
{
	int poppedNumber;
	STACK myStackHead;
	myStackHead=(STACK)malloc(sizeof(struct stack));
	myStackHead->top=getNode();
	myStackHead->top->next=NULL;
	push(myStackHead,1);	
	poppedNumber=pop(myStackHead);
	if(poppedNumber!=-1)
		printf("\nPopped:%d\n",poppedNumber);
	poppedNumber=pop(myStackHead);
	if(poppedNumber!=-1)
		printf("\nPopped:%d\n",poppedNumber);
	push(myStackHead,2);	
	push(myStackHead,3);
	poppedNumber=pop(myStackHead);
	if(poppedNumber!=-1)
		printf("\nPopped:%d\n",poppedNumber);
	push(myStackHead,4);
	poppedNumber=pop(myStackHead);
	if(poppedNumber!=-1)
		printf("\nPopped:%d\n",poppedNumber);
	push(myStackHead,5);	
	push(myStackHead,6);	
	push(myStackHead,7);
	poppedNumber=pop(myStackHead);
	if(poppedNumber!=-1)
		printf("\nPopped:%d\n",poppedNumber);	
	push(myStackHead,8);	

	printStack(myStackHead);	

	return 0;
}
Пример #21
0
void printDebugInfo(void *ret, char **stack) {
    // Print the return address of the function we are in
    printf("Our return address is: %p\n", ret);

    // Print what our stack looks like right now
    printStack(stack, ret);
}
Пример #22
0
void printStack(bool print) {
  if (!isEmpty()) {
    int value = pop();
    printStack(print);
    if (print) printf("%d ", value);
    push(value);
  }
}
Пример #23
0
/* Tato funkce jen vhodně obaluje make2param a komunikuje 
 * s uživatelem*/
void make2wrap(Oper2par function, char operationName[]) //%.h
{
    if (!make2param(function)) {
        fprintf(stderr, " error > %s vyžaduje dva parametry\n",
                operationName);
    }
    printStack();
}
Пример #24
0
int main(){
	Stack *topo;
	topo=NULL;
	system("clear");	
	_push(&topo,1);
	_push(&topo,2);
	_push(&topo,3);
	printf("\n\n %i \n\n",sizeofStack(topo));
	printStack(topo);
	__pause();
	_pop(&topo);
	printStack(topo);
	printf("\n\n %i \n\n",sizeofStack(topo));
	__pause();

	return 1;
}
Пример #25
0
str
MDBvar(Client cntxt, MalBlkPtr mb, MalStkPtr stk, InstrPtr p)
{
	(void) p;
	(void) stk;
	printStack(cntxt->fdout, mb, stk);
	return MAL_SUCCEED;
}
Пример #26
0
//void runPM0(FILE* fn1, FILE* fo2) {
void runPM0(FILE* fn1) {
    // When running the VM, make a stack and an instruction array
    int stack[MAX_STACK_HEIGHT];
    instruction code[MAX_CODE_LENGTH];
    regFile registers;
    int instructionCount = 0, i, j;
    
    // Initialize registers
    registers.SP = 0;
    registers.BP = 1;
    registers.PC = 0;
    registers.IR.OP = 0;
    registers.IR.L = 0;
    registers.IR.M = 0;
    
    // Initialize stack
    for(i = 0; i < MAX_STACK_HEIGHT; i++) {
        stack[i] = 0;
    }
    
    // Read in the instructions into the instruction array
    while (fscanf(fn1, "%d%d%d ", &code[instructionCount].OP, &code[instructionCount].L, &code[instructionCount].M) != EOF) {
        // Increment the instructionIterator
        instructionCount++;
    }
    
    // Now that we have the instructions, we can now start the execution of the program.
    printf("Line  OP   L   M\n");
    
    // Print out the instructions in assembly language format
    for(i = 0; i < instructionCount; i++) {
        printf(" %3d %3s %3d %3d\n", i, OPCODES[code[i].OP], code[i].L, code[i].M);
    }
    
    printf("\n");
    printf("Line  OP   L   M  pc  bp  sp  stack\n");
    printf("  Initial Values   0   1   0  -EMPTY-\n");
    
    // Now we need to do the fetch and execute cycles for all the instructions
    //for(i = 0; i < instructionCount; i++) {
    while(registers.BP > 0) { //CJA - TBD - Changed this because we don't go for "instruction count" times. We go until the base pointer is dead. ( THIS NEEDS TO BE CONFIRMED! )
        // Fetch: (Since it is one line of code, I decided not to write a function for it)
        //  IR <- code[PC]
        //  PC <- PC + 1
        registers.IR = code[registers.PC++];
        printf(" %3d %3s %3d %3d", registers.PC-1, OPCODES[code[registers.PC-1].OP], code[registers.PC-1].L, code[registers.PC-1].M);
        // Execute:
        //execute(fo2, code, stack, &registers);
        execute(code, stack, &registers);
        printf(" %3d %3d %3d ", registers.PC, registers.BP, registers.SP);
        //for(j = 0; j <= registers.SP; j++) {
//            printf(" %d", stack[j]);
//        }
//        printf("\n");
        printStack(stack, registers);
        printf("\n");
    }
}
Пример #27
0
// main
int main(){
   // top is a pointer to a node struct
   // holds null
   struct node* top = NULL;
   // size is 0
   int size=0;
   
   printf("in main");
   // prints "size = 0, top = [something that indicates it's null]"
   printf("\nsize = %d, *top = %p",size,top);
   
   // sends top (currently null) to printStack()
   printStack(top);
   
   printf("\nThe size of the stack is %d.\n", stackSize(top));
   pop(top);
   
   // sends the location of top (bc of the &)
   // to push() w the value 4
   push(4,&top);
   pop(top);
   
   printf("\nThe size of the stack is %d.\n", stackSize(top));
   
   // size is the size of the stack, top is the address of the node
   // top -> data is the value for data held in the node's address
   // top -> is the value for the next node held in the current node's address
   printf("\nsize = %d, *top = %p, top data = %d, top next = %p",size,top,top->data,top->next);
   printStack(top);
   //printf("\nsize = %d, *top = %p, top data = %d, top next = %p",size,top,top->data,top->next);
   push(5,&top);
   
   printf("\nThe size of the stack is %d.\n", stackSize(top));
   

   //printf("\nsize = %d, *top = %p, top data = %d, top next = %p",size,top,top->data,top->next);
   printStack(top);
   push(6,&top);
   
   printf("\nThe size of the stack is %d.\n", stackSize(top));
   

   printStack(top);
   return (0);
}
Пример #28
0
/* Tato funkce jen vhodně obaluje make1param a komunikuje 
 * s uživatelem*/
void make1wrap(Oper1par function, char operationName[]) //%.h
{
    if (!make1param(function)) {
        fprintf(stderr, " error > %s vyžaduje jeden parametr\n",
                operationName);
    }
    printStack();

}
Пример #29
0
void printStack(int index , struct stack * stak){
    if ( index == MIN ){
        return ;
    }
    else{
        printf("%d\n ",stak->stk[index]);
        printStack(index-1,stak);
    }
}
Пример #30
0
void Foam::IOerror::abort()
{
    if (!throwExceptions_ && JobInfo::constructed)
    {
        jobInfo.add("FatalIOError", operator dictionary());
        jobInfo.abort();
    }

    if (abort_)
    {
        Perr<< endl << *this << endl
            << "\nFOAM aborting (FOAM_ABORT set)\n" << endl;
        printStack(Perr);
        ::abort();
    }

    if (Pstream::parRun())
    {
        Perr<< endl << *this << endl
            << "\nFOAM parallel run aborting\n" << endl;
        printStack(Perr);
        Pstream::abort();
    }
    else
    {
        if (throwExceptions_)
        {
            // Make a copy of the error to throw
            IOerror errorException(*this);

            // Rewind the message buffer for the next error message
            messageStreamPtr_->rewind();

            throw errorException;
        }
        else
        {
            Perr<< endl << *this << endl
                << "\nFOAM aborting\n" << endl;
            printStack(Perr);
            ::abort();
        }
    }
}