笔试程序题专项----2012年408

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;

typedef struct node
{
    char data;
    node *next;
} node,*pnode;

pnode head1,head2,p,q;

void print(pnode head)
{
    p=head->next;
    while(p!=NULL)
    {
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}
pnode load1(pnode head,string str)
{
    pnode t=head,tt,res;
    for(int i=0; i<str.size(); i++)
    {
        tt=(pnode)malloc(sizeof(node));
        tt->data=str[i];
        tt->next=NULL;
        if(str[i]=='a') res=tt;
        t->next=tt;
        t=t->next;
    }
    return res;
}
pnode load2(pnode head,string str,pnode last)
{
    pnode t=head,tt,res;
    for(int i=0; i<str.length(); i++)
    {
        tt=(pnode)malloc(sizeof(node));
        tt->data=str[i];
        tt->next=NULL;
        t->next=tt;
        t=t->next;
    }
    tt->next=last;
    return res;
}

int len(pnode head)
{
    pnode t=head->next;
    int sum=0;
    while(t->next!=NULL)
    {
        sum++;
        t=t->next;
    }
    return sum;
}
int main() {

    head1=(pnode)malloc(sizeof(node));
    head1->next=NULL;
    head2=(pnode)malloc(sizeof(node));
    head2->next=NULL;

    pnode temp=load1(head1,"downloading");
    print(head1);
    load2(head2,"re",temp);
    print(head2);

    int l1=len(head1);
    int l2=len(head2);
    pnode p1=head1->next;
    pnode p2=head2->next;

    if(l1>l2)
    {
        for(int i=0; i<l1-l2; i++)
            p1=p1->next;
    }
    else
    {
        for(int i=0; i<l2-l1; i++)
            p2=p2->next;
    }


    while((p1!=NULL)&& p1!=p2)
    {
        p1=p1->next;
        p2=p2->next;
    }
    cout<<p1->data<<endl;
    return 0;

}

猜你喜欢

转载自blog.csdn.net/qiang_____0712/article/details/88086883
今日推荐