Пример #1
0
int main(int argc, char **argv)
{
	int N = (argc < 2) ? 20 : atoi(argv[1]);
	if (N < 20) N = 20;
	Stack s = newStack();
	int i;
	char x[50];
	for (i = 0; i < N; i++) {
		if (random()%10 > 5) {
			if (!emptyStack(s)) {
				char *str = popFrom(s);
				printf("Remove %s\n",str);
				free(str);
			}
		}
		else {
			randomString(x);
			pushOnto(s,x);
			printf("Insert %s\n",x);
		}
		showStack(s);
	}
	disposeStack(s);
	return 0;
}
Пример #2
0
void test_stringStack(){
	struct stack stringStack;
	int i=0;
	char *friends[] = {"Alis", "Bob", "Michael"};
	char *temp;
	
	newStack(&stringStack, sizeof(char*), stringFree, stringDisplay);
	for(i=0; i<3; i++){
		char *copy = strdup((const char*)friends[i]);
		/*
		Reason why we should pass &copy and not just copy:
		If we pass just copy, then the memcpy in the push function will
			write into s->elems array, the value pointed to by the string.
		x /10wx s->elems
		0x602060:       0x73696c41      0x00000000      0x00000000      0x00000000
		0x602070:       0x00000000      0x00000000      0x00000000      0x00000000
		If you see, the 1st element should ideally contain the pointer to the string
			"Alis". Instead it has stored the value(string) in the stack. Our
			stack now contains Alis in its 1st element. The free function will now
			try to free 0x73696c41 which is an invalid address.

		Thus we should pass &copy and the elems will look like
		(gdb) p item // item is the pointer to char* of Alis.
		$2 = (void *) 0x7fffffffdfe8
		(gdb) x /10wx s->elems

		0x602060:       0x00602440      0x00000000      0x00000000      0x00000000
		0x602070:       0x00000000      0x00000000      0x00000000      0x00000000
		
		i.e 0x7fffffffdfe8 points to 0x00602440 which points to "Alis"
		the memcpy will store the value at 0x7fffffffdfe8 to the 1st element in the stack which
			is 0x00602440
		*/
		Push(&stringStack, &copy);
	}

	stackDisplay(&stringStack);

	disposeStack(&stringStack);

	/*
	Here again we should pass the address of the character pointer temp. If we just pass the value of temp,
	then the memcpy in the POP function, will try to write the value in the stack to the value pointer to 
	by this array. Currently, temp is not pointing to anything and it is not the duty of the stack to fill in 
	the value. It is just going to return the address of the value in the stack in this variable.
	*/
	for(i=0; i<4; i++){
		if(Pop(&stringStack, &temp)){
			printf("\nString is %s\n",temp);
			free(temp);
		}
		else{
			printf("\nStack is Empty\n");
			return;
		}
	}
}
Пример #3
0
int areParanthesisCorrect(char* data){
	ArrayUtil util = initialize();
	char *actual = (char*)util.base;
	char ch;
	int length = strlen(data);
	int i,found,res;
	char* topElement;
	Stack *stack = New(sizeof(char),20);
	for(i=0;i<length;i++){
		ch = data[i];
		found = findIndex(util,&ch);
		if(found>-1 && found<3)
			res = push(stack,&ch);
		if(found>=3 && found<6){
			topElement = (char*)top(stack);
			if(topElement != NULL){
				if(*topElement == actual[found-3]){
					topElement = (char*)pop(stack);
				}
				else{
					dispose(util);
					disposeStack(stack);
					return 0;
				}
			}
			else
				return 0;
		}
	}
	if(isEmpty(stack)){
		dispose(util);
		disposeStack(stack);
		return 1;
	}
	dispose(util);
	disposeStack(stack);
	return 0;
}
Пример #4
0
int main()
{
	Stack s;
	int i = 0;
	int tmp = 0;

	s = initStack(12);
	for(i=0; i<10; i++)
	{
		push(i, s);
	}

	while(!isEmpty(s))
	{
		tmp = pop(s);
		printf("%d    ", tmp);
	}

	disposeStack(s);
	return 0;
}