我遇到了这个问题,我需要为它创建一个代码。所以我们有一个由用户输入的字符串,然后代码需要检查句子是否是回文(句子中间的对称词应该是相同的。但我们应该使用堆栈来实现它。 我熟悉函数pop()和push()(甚至认为我没有在下面使用它们)。到目前为止我所想到的是,我接受字符串并从这个字符串中创建一堆单词并使用该堆栈来检查句子是否是回文。我现在被困住了,我真的想不出别的什么。非常感谢帮助。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
char s[30];
struct stack *next;
};
typedef struct stack STACK;
struct top
{
int num;
struct stack *top;
};
typedef struct top TOP;
void create_stack(TOP *s, char str[1000])
{
char temp1[30];
int i=0, j=0;
STACK *temp;
temp=(STACK*)malloc(1*sizeof(STACK));
while(1)
{
if(str[i]!=' ' && str[i]!='\0')
{
temp1[j]=str[i];
j++;
}
else
{
temp1[j]='\0';
strcpy(temp->s,temp1);
printf("%s\n", temp->s);
if(s->top==NULL)
{
s->top=temp;
s->num=1;
}
else
{
temp->next=s->top;
s->top=temp;
s->num++;
}
j=0;
}
if(str[i]=='\0')
{
break;
}
i++;
}
}
void move_cursor(STACK *cursor, int pos)
{
while (pos!=0)
{
cursor=cursor->next;
pos--;
}
}
void compare(TOP *s)
{
STACK *cursor1, *cursor2;
cursor1=s->top;
cursor2=s->top;
int cursor_move1, cursor_move2, i=0, check=1;
if(s->num%2==0)
{
cursor_move1=s->num/2;
cursor_move2=(s->num/2)+1;
while (i!=cursor_move1)
{
cursor1=s->top;
cursor2=s->top;
move_cursor(cursor1, i);
move_cursor(cursor2, cursor_move2);
if(strcmp(cursor1->s,cursor2->s)!=0)
{
check=0;
break;
}
else
{
i++;
cursor_move2++;
}
}
}
if(check==0)
printf("%d Neg", check);
else
printf("1Pos");
}
void display(TOP *top)
{
STACK *cursor;
cursor=top->top;
while(cursor->next==NULL)
{
printf("%s pos\n ", cursor->s);
cursor=cursor->next;
}
}
int main()
{
char input[1000];
TOP top;
top.num=0;
top.top=NULL;
fgets(input, 100, stdin);
input[strlen(input)-1]='\0';
create_stack(&top, input);
printf("%d \n ", top.num);
display(&top);
printf("---------------------------------------------------------\n");
compare(&top);
return 0;
}
答案 0 :(得分:1)
您的代码中存在不同的问题。最大的一个解释了为什么你不能提供堆栈,如果你只是在开始循环之前在堆栈中创建一个单独的元素,而你显然需要为每个单词分配一个元素。此外,您忘记将顶部元素的next
值初始化为NULL。在C中BTW你永远不应该施放malloc。
create_stack应该成为:
void create_stack(TOP *s, char str[1000])
{
char temp1[30];
int i=0, j=0;
STACK *temp;
temp=malloc(1*sizeof(STACK));
temp->next = NULL; // must be explicitely NULL for further use
while(1)
{
if(str[i]!=' ' && str[i]!='\0')
{
temp1[j]=str[i];
j++;
}
else
{
temp1[j]='\0';
strcpy(temp->s,temp1);
printf("%s\n", temp->s);
if(s->top==NULL)
{
s->top=temp;
s->num=1;
}
else
{
temp->next=s->top;
s->top=temp;
s->num++;
}
j=0;
temp=malloc(1*sizeof(STACK)); // time to allocate a new element
}
if(str[i]=='\0')
{
free(temp); // last allocated has not been used
break;
}
i++;
}
}
在显示中,您的循环测试是完全错误的,它应该是while(cursor!=NULL)
修复此问题后,您应该使用调试器来理解,而compare不会给出预期的结果。无论如何,我的意见是,不应重复移动光标,而是应该为STACK元素分配一个指针数组,用堆栈的内容提供一次,并使用该数组直接在数组元素之间进行比较,即通过索引。
答案 1 :(得分:0)
伙计我能够解决我的问题。我发布下面的代码,所以如果有人需要它知道在哪里找到它。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct stack
{
char s[30];
struct stack *next;
};
typedef struct stack STACK;
struct top
{
int num;
STACK *top;
};
typedef struct top TOP;
void push(TOP *top, char str[30])
{
STACK *temp;
temp=malloc(1*sizeof(STACK));
temp->next = NULL;
strcpy(temp->s, str);
if(top->num==0)
{
top->top=temp;
top->num=1;
}
else
{
temp->next=top->top;
top->top=temp;
top->num++;
}
}
void pop (TOP *top, char s[30])
{
STACK *temp;
temp=top->top;
temp=temp->next;
strcpy(s,top->top->s);
free(top->top);
top->top=temp;
top->num--;
}
void create_stack(TOP *s, char str[1000])
{
char temp1[30];
int i=0, j=0;
while(1)
{
if(str[i]!=' ' && str[i]!='\0')
{
temp1[j]=str[i];
j++;
}
else
{
temp1[j]='\0';
push(s, temp1);
j=0;
}
if(str[i]=='\0')
{
break;
}
i++;
}
}
void display(TOP *top)
{
STACK *cursor;
cursor=top->top;
while(cursor!=NULL)
{
printf("%s\n ", cursor->s);
cursor=cursor->next;
}
}
void compare(TOP *top, char *s)
{
char s2[1000];
s2[0]='\0';
char ret[30];
int len;
pop(top,ret);
strcpy(s2, ret);
while(top->top!=NULL)
{
len=strlen(s2);
s2[len]=' ';
s2[len+1]='\0';
ret[0]='\0';
pop(top,ret);
strcat(s2, ret);
}
if(strcmp(s, s2)==0)
printf("The sentence is palindromic by words!\n");
else
printf("The sentence is not palindromic by words!\n");
}
int main()
{
char input[1000];
TOP top;
top.num=0;
top.top=NULL;
while(1)
{
fgets(input, 100, stdin);
input[strlen(input)-1]='\0';
if(strcmp(input, "exit")==0)
break;
create_stack(&top, input);
compare(&top, input);
}
return 0;
}
尝试输入&#34;像狗一样的猫和像猫一样的狗&#34;