链表的 基本应用

链表的拆分
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *next;
};
int main()
{ int n=0,i=0,x=0,y=0;
    struct node *head,*head1,*head2,*p;
    wtruct node *q,*tail,*tail1,*tail2,*t,*em;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    tail=head;
head1=(struct node*)malloc(sizeof(struct node));
    head1->next=NULL;
tail1=head1;
    head2=(struct node*)malloc(sizeof(struct node));
    head2->next=NULL;
tail2=head2;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
       p=(struct node*)malloc(sizeof(struct node));
        p->next=NULL;
        scanf("%d",&p->data);
        tail->next=p;
        tail=p;
    }
 

    q=head->next;
    while(q!=NULL)
    {
t=(struct node*)malloc(sizeof(struct node));
        t->next=NULL;

t->data=q->data;
        if((t->data)%2==0)
        {
          tail1->next=t;
          tail1=t;
          x++;
        }
        else {
            tail2->next=t;
            tail2=t;
            y++;
}
q=q->next;
}
    printf("%d %d\n",x,y);
em=head1;
  while(em->next->next!=NULL)
    {
        printf("%d ",em->next->data);
       em=em->next;
    }
    printf("%d\n",em->next->data);
em=head2;
while(em->next->next!=NULL)
    {
        printf("%d ",em->next->data);
       em=em->next;
    }
    printf("%d\n",em->next->data);
 
    return 0;
}
链表删除指定字符
#include
#include
struct node
{
    int data;
    struct node *next;
};
int main()
{
    struct node *p,*head,*tail,*t,*em;
    int n,key,i=0,k=0;
    head = (struct node *)malloc(sizeof(struct node));
    head->next=NULL;
    tail = head;
    scanf("%d",&n);
    for(i=1; i<=n; i++)
    {
        p = (struct node *)malloc(sizeof(struct node));
        p->next = NULL;
        scanf("%d",&p->data);
        tail->next = p;
        tail=p;
    }
    scanf("%d",&key);
    printf("%d\n",n);
    em=head;
    while(em->next->next!=NULL)
    {
        printf("%d ",em->next->data);
        em=em->next;
    }
    printf("%d\n",em->next->data);
    em = head;
    while(em->next!=NULL)
    {
        if(em->next->data==key)
        {
            t= em->next;
            em->next=t->next;
            free(t);
            k++;
        }
        else
        {
            em = em->next;
        }
    }
    printf("%d\n",n - k);
    em=head;
    while(em->next->next!=NULL)
    {
        printf("%d ",em->next->data);
        em=em->next;
    }
    printf("%d\n",em->next->data);
    return 0;
}
链表的顺序和逆序
//顺序
#include
#include
struct xxx
{
    int data;
    struct xxx *next;
};

int main()
{
    struct xxx *head,*now,*tail;
    int n;
    head=(struct xxx *)malloc(sizeof (struct xxx));
    head->next=NULL;
    tail = head ;
    scanf("%d",&n);
    while(n-->0)
 {
        now=(struct xxx *)malloc(sizeof (struct xxx));
        now->next=NULL;
        scanf("%d",&now->data);
        tail->next=now;
        tail=now;
    }
    while(head->next->next!=NULL)
 {
  printf("%d ",head->next->data);
  head=head->next;
 }
 printf("%d\n",head->next->data);
    return 0;
}

//逆序
#include
struct node
{
    int data;
    struct node *next;
};

int main()
{
    struct node *head,*p,*tail;
    int n,i;
    head=(struct node *)malloc(sizeof (struct node));
    head->next=NULL;
    tail = head ;
    scanf("%d",&n);
    for(i=1;i<=n;i++)
    {
        p=(struct node *)malloc(sizeof (struct node));
        p->next=NULL;
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;
    }
    while(head->next->next!=NULL)
 {
  printf("%d ",head->next->data);
  head=head->next;
 }
 printf("%d\n",head->next->data);
    return 0;
}
 



 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值