zoukankan      html  css  js  c++  java
  • 单链表节点的删除

                                                                                                   单链表节点的删除
    删除单链表pos位置的节点,返回头指针
    pos从开始计算,1表示删除head后面的第一个节点
    node *delete_node(node *head,int pos)
    {
    node *item=NULL;
    node *p=head->next;
    if(p==NULL)
    {
    printf("link is empty ");
    return NULL;
    }
    p=search_node(head,pos-1);//获得位置pos的节点指针
    if(p!=NULL && p->next!=NULL)
    {
    item=p->next;
    p->next=item->next;
    delete item;
    }
    return head;
    }
      
    main()
    {
    node *head=create();//创建单链表(创建函数的实现见我的博客)
    printf("Length:%d ",length(head));//测单链表的长度
    head=insert_node(head,2,5);//在第二个节点的后面添加5
    printf("insert integer 5 after 2th node: ");
    printf(node);//打印单链表
    head=delete_node(head,2);删除第二个节点
    printf("delete the 2th node ");
    print(head);
    }
  • 相关阅读:
    [IOI2013]Dreaming
    Lost Cows
    Mobile Service
    [POI2005]Bank notes
    [CTSC2007]动物园zoo
    [CF1093F]Vasya and Array
    [雅礼集训 2017 Day1]市场
    [APIO2014]序列分割
    [CEOI2004]锯木厂选址
    [APIO2010]特别行动队
  • 原文地址:https://www.cnblogs.com/zhangaihua/p/3718072.html
Copyright © 2011-2022 走看看