用栈判断输入字符串是否为回文字符串

//栈数据结构定义
#include
#include
#include
#define FULL 10000
#define MAX 10000
struct elem {						
	char d;
	struct elem *next;
};

struct stack {							//定义栈
	int cnt;
	struct elem *top;
};		
void push(char d,struct stack *stk){
	struct elem *p;
	if (stk->cnt != FULL){
		p = (struct elem *)malloc(sizeof(struct elem));
		p->d = d;
		p->next = stk->top;
		stk->top = p;
		stk->cnt++;
	}
}

char pop(struct stack *stk){
	char d;
	struct elem *p;
	if(stk->cnt != FULL){
		d = stk->top->d;
		p = stk->top;
		stk->top = stk->top->next;
		stk->cnt--;
		free(p);
	}	
	return d;
}

void initialize(struct stack *stk){
	stk->cnt = 0;
	stk->top = NULL;
}
main(){
	char input[MAX];
	struct stack temp;
	int i = 0;
	int flag = 0;
	initialize(&temp);
	scanf("%s", &input);					//输入字符串

	while (input[i] != '@'){				//字符串入栈
		push(input[i],&temp);
		i++;
	}

	while (temp.cnt != 0){					//字符依次出栈和字符数组比较,判断是否回文数
		if (temp.top->d == input[flag]){
			pop(&temp);						//删除字符
			flag++;
		}
		else{
			printf("此字符序列不是回文序列!\n");
			break;
		}
	}
	if (temp.cnt == 0)
		printf("此字符序列是回文序列!\n");
	return 1;
}

你可能感兴趣的:(数据结构与算法)