c语言用栈判断回文数,请问高手我这个程序应该得如何修改
#include"stdio.h"#include"malloc.h"typedefstructsnode{chardata;structsnode*next;}LSNo...
#include"stdio.h"
#include"malloc.h"
typedef struct snode
{
char data;
struct snode *next;
}LSNode;
void StackInitiate(LSNode **head)
{
*head=(LSNode *)malloc(sizeof(LSNode));
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next=NULL)return 0;
else return 1;
}
void StackPush(LSNode *head,char x)//入栈
{
LSNode *p;
p=(LSNode *)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;
head->next=p;
}
char StackPop(LSNode *head)//出栈
{
LSNode *p=head->next;
char x;
if(p==NULL)
{
printf("堆栈已空出错!");
return 0;
}
head->next=p->next;
x=p->data;
free(p);
return x;
}
void Detroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
int main()
{
int i,n;char str[100],ch;
LSNode *head;
StackInitiate(&head);
scanf("%d",&n);
printf("输入一个字符序列(n=%d):",n);
for(i=0;i<n;i++)
{
scanf("%c",&str[i]);
StackPush(head, str[i]);
}
for(i=n-1;i>0;i--)
{
ch=StackPop(head); //printf("%c",ch);
if(ch!=str[i])
{
printf("这个字符序列不是回文数!\n");
}
if(i=0)
printf("这个字符序列是回文数!\n");
}
return 1;
} 展开
#include"malloc.h"
typedef struct snode
{
char data;
struct snode *next;
}LSNode;
void StackInitiate(LSNode **head)
{
*head=(LSNode *)malloc(sizeof(LSNode));
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next=NULL)return 0;
else return 1;
}
void StackPush(LSNode *head,char x)//入栈
{
LSNode *p;
p=(LSNode *)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;
head->next=p;
}
char StackPop(LSNode *head)//出栈
{
LSNode *p=head->next;
char x;
if(p==NULL)
{
printf("堆栈已空出错!");
return 0;
}
head->next=p->next;
x=p->data;
free(p);
return x;
}
void Detroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
int main()
{
int i,n;char str[100],ch;
LSNode *head;
StackInitiate(&head);
scanf("%d",&n);
printf("输入一个字符序列(n=%d):",n);
for(i=0;i<n;i++)
{
scanf("%c",&str[i]);
StackPush(head, str[i]);
}
for(i=n-1;i>0;i--)
{
ch=StackPop(head); //printf("%c",ch);
if(ch!=str[i])
{
printf("这个字符序列不是回文数!\n");
}
if(i=0)
printf("这个字符序列是回文数!\n");
}
return 1;
} 展开
2个回答
展开全部
#include"stdio.h"
#include"malloc.h"
typedef struct snode{ char data; struct snode *next;
}
LSNode;
void StackInitiate(LSNode **head)
{
*head=(LSNode *)malloc(sizeof(LSNode));
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next=NULL)return 0;
else return 1;
}
void StackPush(LSNode *head,char x)
//入栈
{
LSNode *p;
p=(LSNode *)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;
head->next=p;
}char StackPop(LSNode *head)//出栈
{
LSNode *p=head->next;
char x;
if(p==NULL)
{
printf("堆栈已空出错!");
return 0;
}
head->next=p->next;
x=p->data;
free(p);
return x;
}
void Detroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
int main()
{
int i,n;
char str[100],ch;
LSNode *head;
StackInitiate(&head);
scanf("%d",&n);
printf("输入一个字符序列(n=%d):",n);
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&str[i]);
StackPush(head, str[i]);
}
for(i=0;i<=(n-1)/2;i++)
{
ch=StackPop(head);
//printf("%c",ch);
if(ch != str[i])
{
printf("这个字符序列不是回文数!\n");
return 0;
}
}
printf("这个字符序列是回文数!\n");
return 0;
}
给你改了
#include"malloc.h"
typedef struct snode{ char data; struct snode *next;
}
LSNode;
void StackInitiate(LSNode **head)
{
*head=(LSNode *)malloc(sizeof(LSNode));
(*head)->next=NULL;
}
int StackNotEmpty(LSNode *head)
{
if(head->next=NULL)return 0;
else return 1;
}
void StackPush(LSNode *head,char x)
//入栈
{
LSNode *p;
p=(LSNode *)malloc(sizeof(LSNode));
p->data=x;
p->next=head->next;
head->next=p;
}char StackPop(LSNode *head)//出栈
{
LSNode *p=head->next;
char x;
if(p==NULL)
{
printf("堆栈已空出错!");
return 0;
}
head->next=p->next;
x=p->data;
free(p);
return x;
}
void Detroy(LSNode *head)
{
LSNode *p,*p1;
p=head;
while(p!=NULL)
{
p1=p;
p=p->next;
free(p1);
}
}
int main()
{
int i,n;
char str[100],ch;
LSNode *head;
StackInitiate(&head);
scanf("%d",&n);
printf("输入一个字符序列(n=%d):",n);
for(i=0;i<n;i++)
{
fflush(stdin);
scanf("%c",&str[i]);
StackPush(head, str[i]);
}
for(i=0;i<=(n-1)/2;i++)
{
ch=StackPop(head);
//printf("%c",ch);
if(ch != str[i])
{
printf("这个字符序列不是回文数!\n");
return 0;
}
}
printf("这个字符序列是回文数!\n");
return 0;
}
给你改了
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询