(链式存储)比较两个字符串A和B大小

(链式存储)比较两个字符串A和B大小,若A>B,返回1,若A<B,返回-1,否则返回0。

#include 
   
   
    
    
#include 
    
    
     
     
#define MAXSIZE 100

typedef char datatype;

typedef struct linklist
{
    datatype data;
    struct linklist *next;
} node;

node *init()
{
    node *head;
    head=(node*)malloc(sizeof(node));
    head->next=NULL;
    return head;
}

void input(node *head,int sign)
{
    node *p,*tail;
    tail=head;
    char a[MAXSIZE];
    int i=0;
    switch(sign)
    {
    case 100:
        printf("请输入A:");
        break;
    case 101:
        printf("请输入B:");
        break;
    }
    gets(a);
    while(a[i]!='\0')
    {
        p=(node*)malloc(sizeof(node));
        p->data=a[i];
        i++;
        tail->next=p;
        tail=p;
    }
    tail->next=NULL;
}


void print(node *headA,node *headB,int sign)
{
    node *pA,*pB;
    switch(sign)
    {
    case 101:
        /*打印A*/
        pA=headA->next;
        if(pA==NULL)printf("\nA是空的!");
        else
        {
            while(pA!=NULL)
            {
                printf("%c",pA->data);
                pA=pA->next;
            }
            printf("\n");
        }
        /*打印B*/
        pB=headB->next;
        if(pB==NULL)printf("\nB是空的!");
        else
        {
            while(pB!=NULL)
            {
                printf("%c",pB->data);
                pB=pB->next;
            }
            printf("\n");
        }
        break;
    default:
        printf("比较结果为:%d\n",sign);
        break;
    }
}

int compare(node *headA,node *headB)
{
    node *pA,*pB;
    for(pA=headA->next,pB=headB->next; pA!=NULL&&pB!=NULL; pA=pA->next,pB=pB->next)
    {
        if(pA->data>pB->data)return 1;
        if(pA->data
     
     
      
      data)return -1;
    }
    if(pA==NULL&&pB!=NULL)return -1;
    if(pB==NULL&&pA!=NULL)return 1;
    if(pA==NULL&&pB==NULL)return 0;
    return 0;
}


int main()
{
    node *headA,*headB;
    int sign=100;
    headA=init();
    headB=init();
    input(headA,sign);
    sign=101;
    input(headB,sign);
    print(headA,headB,sign);
    sign=compare(headA,headB);
    print(headA,headB,sign);
    return 0;
}

     
     
    
    
   
   

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值