数据结构_线性表_链表实现

之前已经完全忘了指针怎么用了。复习了一下,目前只写了一点点。

#include<iostream>
#include<cstdio>
#include<malloc.h>
using namespace std;

struct Node
{
    int e;
    double c;
    Node* next;
};

struct Head
{
    int cnt;
    Node* elem;
};

Head* inilHead()
{
    Head *head;
    head=(Head*)malloc(sizeof(Head));
    head->cnt=0;
    head->elem=NULL;
    return head;
}

void insert(Head *head,double c,int e)
{
    Node *pos=head->elem;
    while(pos!=NULL)
    {
        if(pos->e==e)
            break;
        pos=pos->next;
    }
    if(pos==NULL)
    {
        Node *node=(Node*)malloc(sizeof(Node));
        node->c=c;
        node->e=e;
        node->next=head->elem;
        head->elem=node;
    }
    else 
        pos->c+=c;
}

void add(int &a,int &b)
{
    b=a+b;
}
int main()
{
    Head *head;
    head=inilHead();
    insert(head,1,1);
    insert(head,1.2,1);
    Node *pos=head->elem;
    /*while(pos!=NULL)
    {
        cout<<pos->c<<' '<<pos->e<<endl;
        pos=pos->next;
    }*/
    
}
原文地址:https://www.cnblogs.com/jasonlixuetao/p/6680173.html