您的位置:首页 > 理论基础 > 数据结构算法

C++数据结构-单向列表如何操作

2016-05-16 00:00 417 查看
源码如下:

// C_OnewayListDataStruct.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <stdio.h>

int _tmain(int argc, _TCHAR* argv[])
{
struct myTime{
int h;
int m;
int s;
};
struct myStudent{
int age;
char name[30];
struct myTime ltime;
//上一个节点
myStudent * pre;
//下一个节点
myStudent * next;
};
struct myStudent *Head , *p,*stu;
int pSize = sizeof(struct myStudent);
time_t t;
struct tm *timeInfo;
//需要强制类型转换C语言不需要,C++需要
//Head 与 p临时变量指针一致。
Head = p = (myStudent *)malloc(pSize);
memset(p,0,pSize);
do{
stu = (myStudent *)malloc(pSize);
stu->next = NULL;
printf("请输入学生名称: \n");
scanf("%s",&stu->name);
printf("请输入年龄: \n");
scanf("%d",&stu->age);

time(&t);
timeInfo = localtime(&t);
stu->ltime.h = timeInfo->tm_hour;
stu->ltime.m = timeInfo->tm_min;
stu->ltime.s = timeInfo->tm_sec;
//Head->next = p->next 这一点要注意
p->next = stu;
p = stu;

}while(strcmp(stu->name,"exit") != 0);

p = Head->next;
while(p){
printf("到校的学生名称: %s,年龄: %d,时间: %d时%d分%d秒\n",p->name,p->age,p->ltime.h,p->ltime.m,p->ltime.s);
p = p->next;
}
//程序自己输出的字符也算
getchar();
getchar();
return 0;
}

效果如下:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  数据结构 单向表