c++ 创建单项链表

建立单向链表
头指针Head
插入结点
 
  //建立头结点 Head
    Head=p= malloc(sizeof( struct stu_data)); //
     memset(stu,0,sizeof( struct stu_data)); //初始化内存区域

//尾部插入新结点 操作
        stu= malloc(sizeof( struct stu_data)); //3
        memset(stu,0,sizeof( struct stu_data)); //初始化内存区域

         stu->back=NULL; //新结点尾指针置空
         p->back=stu; //前驱结点back指针指向新结点
         p=stu; //重要移动标志指针
Head=p=1;         
 1                                                      back
        .    0    2
 2 p->back=stu; stu->back=NULL;p=stu;
name    stuTime    ...    其它数据    0    3
3
name    stuTime    ...    其它数据    0    NULL

 代码

 int main(int argn,char* argv[])// int a[1]//a[0]
 {       
    

     struct mytime
     {   
         //char name[256];
         int hour;//
         int min; //
         int sec; //
     };

     struct  stu_data
     {
         char name[256];//学生名字
         struct mytime stuTime;//签到时间
        // struct  stu_data* front; //指向前一个结点
         struct  stu_data* back;  //指向后一个结点

     }  ;
     struct mytime t2;
     struct stu_data *stu,*p,*Head;
     
     time_t t;// long int
     struct tm * timfo;
     int i;
 
     //建立Head头结点
     Head=p=malloc(sizeof( struct stu_data)); //256+12=268
     memset(p,0,sizeof( struct stu_data));

    
     
     
     do
     {//插入新的结点
         stu= malloc(sizeof( struct stu_data)); //
         memset(stu,0,sizeof( struct stu_data)); //初始化内存区域

        stu->back=NULL; //新结点尾指针置空
         p->back=stu; //前驱结点back指针指向新结点
         p=stu; //重要移动标志指针

         scanf("%s",&stu->name);
         time(&t);
         timfo= localtime(&t); //取当前系统时间 
         stu->stuTime.hour=timfo->tm_hour;//
         stu->stuTime.min=timfo->tm_min;//
         stu->stuTime.sec=timfo->tm_sec;//

     } while(strcmpi(stu->name,"exit")!=0);
 
     //初始指针p 使他头结点Head
     stu=Head->back;
     do 
     {
          printf("%s,到校时间:%d时%d分%d秒
",stu->name, stu->stuTime.hour, stu->stuTime.min, stu->stuTime.sec);

         stu=stu->back;
     } while (strcmpi(stu->name,"exit"));



     
 

 
    getchar();
    getchar();
    
    return 0;
}
原文地址:https://www.cnblogs.com/whzym111/p/6137399.html