原创 字符串中心对称判断(二)

2010-1-18 18:17 3080 7 7 分类: 软件与OS

#define NULL 0                         


#include<stdio.h>


#include<string.h>


#define maxsize 20


 


typedef struct node


{


  char strdata;


  struct node *next;


}Llist;


 


typedef struct nodestack


{


  char stackdata[maxsize];


  int top;


}stack;


 


Llist *creat(int n)  //创建链表


{


 int j;


 char ch;


 Llist *head,*p,*q;


 p=(Llist *)malloc(sizeof(Llist));


 p->strdata=0;


 p->next=NULL;


 head=p;


 for(j=1;j<=n;j++)


 {


  getchar();


 q=(Llist *)malloc(sizeof(Llist));


 q->strdata=getchar();


 q->next=NULL;


 p->next=q;


 p=p->next;


 }


 return(head);


}


 


 stack *push(stack *s,char x)         //进栈


 {


 if(s->top==(maxsize-1))


 printf("overflow");


 else


 {s->top=s->top+1;


 s->stackdata[s->top]=x;


 }return(s);


 }


 char popup(stack *s)                //出栈


 {


 char p;


 if(s->top==-1)


 printf("empty stack");


 else


 {


 p=s->stackdata[s->top];


 s->top=s->top-1;


 }


 return(p);


 }


void main()


 {


 stack *s;


 Llist *p,*head;


 int flag="0",n,i;


 s=(stack *)malloc(sizeof(stack));


 s->top=-1;


 printf("please Input interger n(1--maxsize)and n wei string\n");//输入位数和字符串


 scanf("%d",&n);


 head=creat(n);


p=head->next;        //对称判断


 for(i=0;i<n/2;i++)


 {


 push(s,p->strdata);   //取一半元素进栈


 p=p->next;


 }


 if(n%2==1 && n>1)


 do


 {


 p=p->next;


 flag=(popup(s)==p->strdata);  //逐个弹出比较


 }


 while(s->top!=-1 && flag==1);


 if(n==0)


 printf("error opration");


 else if(flag==1 || n==1)


 printf("zi fu chaun shi zhong xin dui chen de!\n");   //对称


 else


 printf("zi fu chuan bu dui chen");     //不对称


 }


 8d630fd9-2bf6-4cf8-a1a1-22f0cdd1282a.jpg

文章评论0条评论)

登录后参与讨论
我要评论
0
7
关闭 站长推荐上一条 /2 下一条