数据结构---无重复元素链表的实现

//节点结构体
struct inv
{
    int i;//节点元素
    struct inv*nextpointer;
};



//返回指定元素节点的指针,不包含时返回NULL
struct inv*  contain(int i,struct inv*head)
{
    struct inv *headp=head;
    while(headp!=NULL)
    {
        if(headp->i==i)
        {
            break;
        }
        headp=headp->nextpointer;
    }
    return headp;
}


//追加元素
int append(int i,struct inv **headpp)
{
    struct inv *headp=*headpp;
    struct inv *prev;
    if(*headpp==NULL)
    {
        *headpp=(struct inv*)malloc(sizeof(struct inv));
        (*headpp)->i=i;
        (*headpp)->nextpointer=NULL;
        return 1;
    }


    while(headp!=NULL)
    {
        prev=headp;        
        if(headp->i==i)
        {
            return 0;
        }
        headp=headp->nextpointer;
    }


    prev->nextpointer=(struct inv*)malloc(sizeof(struct inv));
    prev=prev->nextpointer;

    prev->i=i;
    prev->nextpointer=NULL;
    
    return 1;

}



//删除指定元素
int dele(int i,struct inv **headpp)
{
    int returnvalue=0;
    struct inv*prev;
    struct inv*headp=*headpp;

    if(headp==NULL)
    {
        return 0;
    }

    while(headp->i!=i&&headp->nextpointer!=NULL)
    {
        prev=headp;
        headp=headp->nextpointer;
    }
    if(headp->i==i)
    {
        if(headp==*headpp)
        {
            *headp=*headp->nextpointer;
        }
        prev->nextpointer=headp->nextpointer;
        free(headp);
        return 1;

    }
    return 0;
}



//删除整个链表
void deleteall(struct inv**head)
{
    struct inv*headp=*head;
    struct inv*temp;
    while(headp!=NULL)
    {
        temp=headp->nextpointer;
        free(headp);
        headp=temp;
    }
    *head=NULL;
}



//扫描打印
void paint(struct inv*headp)
{
    while(headp!=NULL)
    {
        printf("    %d\n",headp->i);
        headp=headp->nextpointer;
    }
}

 

 

cpp源码文件:   http://pan.baidu.com/s/1bodkQRX   

linked list.cpp文件

转载于:https://www.cnblogs.com/magicianlyx/p/5161309.html

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值