c语言动态链表的经典例题(语言设计单向静态链表实例)
c语言动态链表的经典例题(语言设计单向静态链表实例)int day;int month;struct birthday{int year;
(一)程序代码:
#include<iostream>
#include<string>
using namespace std;
struct birthday
{
int year;
int month;
int day;
};
struct person
{
string name;
birthday bd;
person *next;
};
int main()
{
person *head s1 s2 s3 s4;
s1.name="caocao";
s1.bd.year=201;
s1.bd.month=3;
s1.bd.day=15;
s1.next=&s2;
s2.name="liubei";
s2.bd.year=203;
s2.bd.month=5;
s2.bd.day=28;
s2.next=&s3;
s3.name="sunquan";
s3.bd.year=231;
s3.bd.month=7;
s3.bd.day=31;
s3.next=&s4;
s4.name="hanxiandi";
s4.bd.year=229;
s4.bd.month=4;
s4.bd.day=7;
s4.next=NULL;
head=&s1;
while(head!=NULL)
{
cout<<head->name<<":"<<head->bd.year<<"-"<<head->bd.month<<"-"<<head->bd.day<<endl;
head=head->next;
}
system("pause>nul");
return 0;
}
(二)程序输出结果:
caocao:201-3-15
liubei:203-5-28
sunquan:231-7-31
hanxiandi:229-4-7
(三)程序说明:
1、链表是一种数据结构,它由结构体数据和指向结构体数据变量的指针组成;
2、单向链表意即从“头部”(一个方向)指向“尾部”(另一个方向);
3、静态链表意即在程序运行过程中不开辟存储空间用于存放链表数据;
4、链表数据结构由两部分组成,一个是指向链表的第一个节点的指针(head指针),一个是链表的节点。节点由两部分组成,一个是数据,一个是指向下一个节点的指针。