链表的构建方法

4 篇文章 0 订阅

目录

构建一个链表,指定链表长度,返回链表元素的平均值。

通过二级指针构建链表

头文件

#include<stdio.h>
#include<stdlib.h>

typedef struct moveaverage MA;
struct moveaverage
{
    double num;
    struct moveaverage *pNext;
};

//更新链表数据
float update_node (MA **phead,int ltlength,float price);//pass head node address,

void showall (MA *head);//pass head node

实现

#include"linknode.h"

float update_node (MA **phead,int ltlength,float price)   //pass head node 指针的 address,因为需要对头指针内容进行修改
{
    float sum=0;
    if (*phead ==NULL)//phead的值为节点指针的地址,*phead间指了节点指针的内容(值),它指向一个结构体节点,即表示节点指针;如果头节点为空,添加头节点
    {
        MA *newnode=(MA*)malloc(sizeof(MA));//声明新节点,为新节点分配空间
        newnode->num=price;
        newnode->pNext=NULL;//新节点的指针域置为空
        *phead=newnode;//把新节点置为头节点
    }else
    {
        //遍历节点,计算链表长度   
        int lengthCount=0;  //定义链表长度
        MA *tmpPoint=*phead;//声明一个临时指针,存放头节点值
        while(tmpPoint->pNext!=NULL)
        {
            tmpPoint=tmpPoint->pNext;
            lengthCount++;
            sum=sum+tmpPoint->num;
        }
        //设定链表长度,如果没有超过,则增加一个节点
        if(lengthCount<ltlength)
        {   //判断链表长度
            MA *newnode=(MA*)malloc(sizeof(MA));
            newnode->num=price;
            newnode->pNext=NULL;
            tmpPoint->pNext=newnode;//新节点接入到遍历到最后的节点位置
            lengthCount ++;//增加了一个节点,链表长度加1
            sum=sum+newnode->num;
        }
        //删除头节点
        if(lengthCount>=ltlength)
        {
            printf("delete ltlent=%d\n",lengthCount);
            *phead=(*phead)->pNext;
        }
    }
    return sum;
}

void showall (MA *head)
{
    while(head!=NULL)
    {
        printf("num=%f\n",head->num);
        printf("head=%p,pNext=%p\n",head,head->pNext);

        head=head->pNext;
    }
}//pass head node

测试

#include"linknode.h"

float update_node (MA **phead,int ltlength,float price)   //pass head node 指针的 address,因为需要对头指针内容进行修改
{
    float sum=0;
    if (*phead ==NULL)//phead的值为节点指针的地址,*phead间指了节点指针的内容(值),它指向一个结构体节点,即表示节点指针;如果头节点为空,添加头节点
    {
        MA *newnode=(MA*)malloc(sizeof(MA));//声明新节点,为新节点分配空间
        newnode->num=price;
        newnode->pNext=NULL;//新节点的指针域置为空
        *phead=newnode;//把新节点置为头节点
    }else
    {
        //遍历节点,计算链表长度   
        int lengthCount=0;  //定义链表长度
        MA *tmpPoint=*phead;//声明一个临时指针,存放头节点值
        while(tmpPoint->pNext!=NULL)
        {
            tmpPoint=tmpPoint->pNext;
            lengthCount++;
            sum=sum+tmpPoint->num;
        }
        //设定链表长度,如果没有超过,则增加一个节点
        if(lengthCount<ltlength)
        {   //判断链表长度
            MA *newnode=(MA*)malloc(sizeof(MA));
            newnode->num=price;
            newnode->pNext=NULL;
            tmpPoint->pNext=newnode;//新节点接入到遍历到最后的节点位置
            lengthCount ++;//增加了一个节点,链表长度加1
            sum=sum+newnode->num;
        }
        //删除头节点
        if(lengthCount>=ltlength)
        {
            printf("delete ltlent=%d\n",lengthCount);
        *phead=(*phead)->pNext;
        }
    }
    return sum;
}

void showall (MA *head)
{
    while(head!=NULL)
    {
        printf("num=%f\n",head->num);
        printf("head=%p,pNext=%p\n",head,head->pNext);

        head=head->pNext;
    }
}//pass head node

通过封装类构建链表

头文件

#include<stdio.h>
#include<stdlib.h>

//定义节点类
class MA
{
    public:
    double num;
    MA *pNext;

};
//定义linklist类
class LinkList
{
    private:
        MA *phead;
    public:
        LinkList();
        ~LinkList();

        //更新链表数据
        double update_node (int ltlength,double price);//pass head node address,

        void showall ();//pass head node
};  

实现

#include"linknode.h"
//initial  
LinkList::LinkList()
{
    this->phead=NULL;
}

double LinkList::update_node (int ltlength,double price)   
{
    double sum=0;
    int lengthCount=0;  
    if (phead ==NULL)
    {
        MA *newnode=new MA();
        newnode->num=price;
        newnode->pNext=NULL;//新节点的指针域置为空
        phead=newnode;//把新节点置为头节点
        printf("phead=%p\n",phead);
    }else
    {
        //遍历节点,计算链表长度   
        MA *tmpPoint=phead;//声明一个临时指针,存放头节点值
        while(tmpPoint->pNext!=NULL)
        {
            tmpPoint=tmpPoint->pNext;
            lengthCount++;
            sum=sum+tmpPoint->num;
        }
        //设定链表长度,如果没有超过,则增加一个节点
        //判断链表长度
        if(lengthCount<ltlength)
        {  
            MA *newnode=new MA();
            newnode->num=price;
            newnode->pNext=NULL;
            tmpPoint->pNext=newnode;//新节点接入到遍历到最后的节点位置
            lengthCount ++;//增加了一个节点,链表长度加1
            sum=sum+newnode->num;
        }
        //删除头节点
        if(lengthCount>=ltlength)
        {
            printf("链表的长度=%d\n",lengthCount);
            phead=phead->pNext;
        }
    }
    return ((lengthCount==0)?price:sum) ;
}

void LinkList::showall ()
{
    while(this->phead!=NULL)
    {
        printf("num=%f\n",phead->num);
        printf("head=%p,pNext=%p\n",phead,phead->pNext);

        phead=phead->pNext;
    }
}//pass head node

测试

#include<stdio.h>
#include<stdlib.h>
#include"linknode.h"

int  main()
{
    LinkList * ll=new LinkList();
    for(int i=0;i<20;i++)
    {
    printf("ma=%f\n",ll->update_node(4,i));
    }
    ll->showall();

return 0;
}

结论

用类好像看起来更舒服些,没有了令人费解的二级指针。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值