寒假集训之链表小结

使用链表首先建立节点的结构体:

struct node
{
    int data;
    struct node *next;
};

链表的建立有两种一种是顺序建立一种是逆序建立,两者相比较而言逆序更简单一些,他比顺序少设置一个指针下面是逆序建立链表的函数

struct node *creat(int len)//逆序建立链表
{
    struct node *head,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    while(len--)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=head->next;
        head->next=p;

    }
    return head;//因为返回值是一个指针所以函数名前会有*
}

 

这是顺序建立链表:

struct node *creat1(int len/*len是链表的长度*/)//顺序建立单链表
{
    struct node *head,*t,*p;
    head=(struct node*)malloc(sizeof(struct node));
    head->next=NULL;
    t=head;//复制一下,通过t可以控制head,t等于head,t并没有开辟新的内存
    while(len--)
    {
        p=(struct node*)malloc(sizeof(struct node));
        scanf("%d",&p->data);
        p->next=NULL;
        t->next=p;//通过t来控制上一个节点
        t=p;//t再移动到p上
    }
    return (head);
}

链表的输出函数:

void shuchu(struct node*head)
{
   struct node *p;
  p=head->next;//把游动指针移到第一个节点
   while(p!=NULL)
   {
    if(p->next==NULL)printf("%d\n",p->data);
    else printf("%d ",p->data);
    p=p->next;
   }
}

 

以下是一些单链表的应用,有单链表的逆置、单链表的归并、单链表的拆分、单链表节点的插入及删除、还有循环链表的建立。

单链表的逆置函数:

void reverse(struct node*head)//传过来要逆置的链表
{
   struct node *p,*q;
   p=head->next;
   head->next=NULL;
   q=p->next;
   while(p)
   {
       p->next=head->next;
       head->next=p;
       p=q;
       if(q)q=q->next;
   }
}

有序链表的归并:

struct node *merge(struct node *head1,struct node *head2)
{
    struct node *p1,*p2,*t;
    p1=head1->next;
    p2=head2->next;
    t=head1;
    free(head2);//不重要
    while(p1&&p2)//p1,p2同时不为空,只要有一个为空循环结束
    {
        if(p1->data<p2->data)
        {
            t->next=p1;
            t=p1;
            p1=p1->next;
        }
        else 
        {
            t->next=p2;
            t=p2;
            p2=p2->next;
        }
    }
    if(p1)t->next=p1;
    else t->next=p2;
    return head1;
    
    
};

单链表节点的插入:在链表的p节点之后插入一个值为key的节点函数

//在链表的p节点之后插入一个值为key的节点函数
void insert(struct node *p,int key)
{
    struct node *q;
    q=(struct node *)malloc(sizeof(struct node));
    q->data=key;
    q->next=NULL;
    q->next=p->next;
    p->next=q;
}

单链表节点的删除,要想删除节点p,需要找到p的前一个节点,这是关键,由于知道一个节点只能找到他后面的节点所以要先找到节点p的前驱节点;

void delete(struct node *head,int key)
{
    struct node *p,*q;
    int flag=0;
    p=head;//不是p=head->next;记住找前驱节点
    while(p->next)
    {
        if(p->next->data==key)
        {
            flag=1;
            break;
        }
        else p=p->next;
    }
    if(flag)
    {
        q=p->next;
        p->next=q->next;
        free(q);
    }
    else printf("Error\n");
}

循环表的建立

把表尾和表尾连在一起即

if(p->next==NULL)p->next=head->next;

还差个主函数

int main()
{
   ........自由发挥吧!!哈哈
    return 0;
}

 

 

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值