C语言如何使用堆栈判断回文?

Python013

C语言如何使用堆栈判断回文?,第1张

#include <iosteam>

#include <string>

using namespace std

#define EMPTY 0

#define FULL 10000

#define MAX 10000

typedef char data

typedef struct elem {

data d

struct elem *next

}elem

typedef struct stack {

int cnt

elem *top

}stack

void initialize(stack *stk)

void push(data d, stack *stk)

data pop(stack *stk)

bool empty(const stack *stk)

bool full(const stack *stk)//栈操作函数

void initialize(stack *stk)

{

stk->cnt = 0

stk->top = NULL

}

bool empty(const stack *stk)

{

return stk->cnt == EMPTY

}

bool full(const stack *stk)

{

return stk->cnt == FULL

}

void push(data d, stack *stk)

{

elem *p

if (!full(stk))

{

p = (elem *)malloc(sizeof(elem))

p->d = d

p->next = stk->top

stk->top = p

stk->cnt++

}

}

data pop(stack *stk)

{

data d

elem *p

if(!empty(stk))

{

d = stk->top->d

p = stk->top

stk->top = stk->top->next

stk->cnt--

delete p

}

return d

}

int main(void)

{

data input[MAX]

stack temp

int i = 0

int flag = 0

initialize(&temp)//初始化临时栈

cin>>&input//输入字符

while (input[i] != '@')

{//字符串入栈

push(input[i], &temp)

i++

}

while (!empty(&temp))

{//字符依次出栈和字符数组比较,判断是否回文

if (temp.top->d == input[flag])

{

pop(&temp)

flag++

}

else

{

cout<<"此字符序列不是回文数!\n"

break

}

}

if (empty(&temp))

cout<<"此字符序列是回文数!\n"

return 1

}

#include <stdio.h>

void main( )

{

char str[100], queue[100], stack[100], top = -1, front = 0, rear = 0, i

int IsHuiwen = 1

gets(str)

for(i = 0str[i] != '\0'i++)

{

queue[rear++] = str[i]

stack[++top] = str[i]

}

while(top !== -1 &&IsHuiwen)

{

if(stack[top--] != queue[front++])

IsHuiwen = 0

}

if(IsHuiwen) printf("yes!\n)

else printf("no!\n)

}