寒假集训作业(2)——链表

这真是令人头疼!
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2116
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *creat2(int n)
{
    struct node *head,*tail,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    for(i=1;i<=n;i++)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    return(head);
}
int main()
{
  int n;
  cin>>n;
  struct node *s;
  s=creat2(n);
  s=s->next;
  while(s!=NULL)
   {
     if(s->next==NULL)
      cout<<s->data<<endl;
     else
       cout<<s->data<<" ";
     s=s->next;
   }
}

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2117

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *creat1(int n)
{
    struct node *head,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return(head);
}
int main()
{
  int n;
  cin>>n;
  struct node *s;
  s=creat1(n);
  s=s->next;
  while(s!=NULL)
   {
     if(s->next==NULL)
      cout<<s->data<<endl;
     else
       cout<<s->data<<" ";
     s=s->next;
   }
}

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2118
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct node
{
    int data;
    struct node *next;
};
/*struct node *creat1(int n)
{
    struct node *head,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return(head);
}*/
void reverse(struct node * head)
{

    struct node *p,*q;
    p=head->next;
    head->next=NULL;
    q=p->next;
    while(p!=NULL)
    {
        p->next=head->next;
        head->next=p;
        p=q;
        if(q!=NULL)
        {
            q=q->next;
        }
    }
}
int main()
{
    struct node *head,*tail,*p,*s;
    head=(struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
    while(1)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        if((p->data)==-1)
        {
            break;
        }
        p->next=NULL;
        tail->next=p;
        tail=p;
    }
    reverse(head);
    s=head;
    s=s->next;
    while(s!=NULL)
    {
        if(s->next==NULL)
        cout<<s->data<<endl;
        else
        cout<<s->data<<" ";
        s=s->next;
    }
}

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2119
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct node
{
    int data;
    struct node *next;
};
/*struct node *creat1(int n)
{
    struct node *head,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return(head);
}*/
struct node *creat2(int n)
{
    struct node *head,*tail,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        tail->next=p;
        tail=p;
    }
    return(head);
}
/*void reverse(struct node * head)
{

    struct node *p,*q;
    p=head->next;
    head->next=NULL;
    q=p->next;
    while(p!=NULL)
    {
        p->next=head->next;
        head->next=p;
        p=q;
        if(q!=NULL)
        {
            q=q->next;
        }
    }
}*/
struct node * merge(struct node *head1,struct node *head2)
{
    struct node *p1,*p2,*tail;
    p1=head1->next;
    p2=head2->next;
    tail=head1;
    free(head2);
    while(p1&&p2)
    {
        if(p1->data<p2->data)
        {
            tail->next=p1;
            tail=p1;
            p1=p1->next;
        }
        else
        {
            tail->next=p2;
            tail=p2;
            p2=p2->next;
        }
    }
    if(p1)
        tail->next=p1;
    else
        tail->next=p2;
    return(head1);
}
void display(struct node *head)
{
    struct node *s;
    s=head;s=s->next;
    while(s!=NULL)
    {
        if(s->next==NULL)
        printf("%d\n",s->data);
        else
        printf("%d ",s->data);
        s=s->next;
    }
}
int main()
{
    int n,m;
    struct node *head1,*head2,*head,*s;
    cin>>n>>m;
    head1=creat2(n);
    head2=creat2(m);
    head=merge(head1,head2);
    display(head);
}

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2120
#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;
struct node
{
    int data;
    struct node *next;
};
struct node *creat1(int n)//逆序建链表
{
    struct node *head,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    return(head);
}
struct node *creat2(int n)//顺序建链表
{
    struct node *head,*tail,*p;
    int i;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        tail->next=p;
        tail=p;
    }
    return(head);
}
void reverse(struct node * head)//逆置
{

    struct node *p,*q;
    p=head->next;
    head->next=NULL;
    q=p->next;
    while(p!=NULL)
    {
        p->next=head->next;
        head->next=p;
        p=q;
        if(q!=NULL)
        {
            q=q->next;
        }
    }
}
struct node * merge(struct node *head1,struct node *head2)//归并
{
    struct node *p1,*p2,*tail;
    p1=head1->next;
    p2=head2->next;
    tail=head1;
    free(head2);
    while(p1&&p2)
    {
        if(p1->data<p2->data)
        {
            tail->next=p1;
            tail=p1;
            p1=p1->next;
        }
        else
        {
            tail->next=p2;
            tail=p2;
            p2=p2->next;
        }
    }
    if(p1)
        tail->next=p1;
    else
        tail->next=p2;
    return(head1);
}
struct node * split(struct node * head1)//拆分
{
    struct node *head2,*p,*q;
    head2=(struct node *)malloc(sizeof(struct node));
    head2->next=NULL;
    p=head1->next;
    head1->next=NULL;
    q=p->next;
    while(p!=NULL)
    {
        if(p->data%2)
        {
            p->next=head1->next;
            head1->next=p;
        }
        else
        {
            p->next=head2->next;
            head2->next=p;
        }
        p=q;
        if(q!=NULL)
            q=q->next;
    }
    return (head2);
}
void display(struct node *head)//显示
{
    struct node *s;
    s=head;
    s=s->next;
    while(s!=NULL)
    {
        if(s->next==NULL)
        printf("%d\n",s->data);
        else
        printf("%d ",s->data);
        s=s->next;
    }
}
void insert(struct node *p,int key)//插入
{
    struct node *q;
    q=(struct node *)malloc(sizeof(struct node));
    if(!q)
    {
        printf("cannot assign memory space!");
        exit(0);
    }
    q->data=key;
    q->next=NULL;
    q->next=p->next;
    p->next=q;
}
struct node countandprint(struct node *number)
{
    int even=0,odd=0;
    while(number->next!=NULL)
    {
        if(number->data%2) even++;
        else odd++;
    }
    cout<<even<<" "<<odd<<endl;
}
int main()
{
    int N;
    cin>>N;
    struct node *temp,*head_odd;
    temp=creat2(N);
    countandprint(temp);
    head_odd=split(temp);
    /*display(head_odd);
    display(temp);*/
}//未完成!

未完待续!

你可能感兴趣的:(寒假集训作业(2)——链表)