链式存储结构

#include<stdlib.h>
#include<iostream>
using namespace std;
typedef struct IntChain{
        int value;//存储具体值
        struct IntChain *next;//用于寻找下一元素
}IntChain,*PIntChain;
int main(){
        IntChain *pre=NULL;
       //头
       PIntChain head=(IntChain*)malloc(sizeof(IntChain));
       pre=head;
       //创建(1,2,3,4,5)
       for(int i=1;i<=5;++i){
           //创建新结点
           IntChain* node=(IntChain*)malloc(sizeof(IntChain));
           node->value=i;
          //链接到上一个结点后
           pre->next=node;
          //node作为 后一个结点的上一个结点
           pre=node;
}
      //最后一个结点指向null
       pre->next=NULL;
      //打印
      for(IntChain*node=head->next;node!=NULL;node=node->next)
      cout<<node->value<<endl;
     //打印地址
     for(IntChain*node=head->next;node!=NULL;node=node->next)
     cout<<node<<endl;

}

posted @ 2020-04-10 22:07  iiiiiki  阅读(386)  评论(0)    收藏  举报