您的位置:首页 > 其它

单向链表->创建节点

2012-11-01 09:21 337 查看
#include <stdio.h>
#include <iostream>
#include <stdlib.h>

using namespace std;

struct Person{
int age;
struct Person *next;
};
Person *CrtTailNode(){
Person *head ,*tmp;
head = tmp = NULL;
int n;
cout<<"Node Number:"<<endl;
cin>>n;
for(int i = 0;i<n;i++){

tmp = new Person;//(Person*)(malloc(sizeof(Person)));
cout<<"Age:" <<endl;
cin >>tmp->age;
tmp->next = head;
head = tmp ;

}
return head;

}

Person *CrtHeadNode(){
Person *head,*tail,*tmp;
int n;
cout<<"Node Number:"<<endl;
cin>>n;
head = tail = tmp = NULL;
for(int i = 0;i<n;i++){
tmp = new Person;//(Person*)(malloc(sizeof(Person)));
cout<<"Age:" <<endl;
cin >>tmp->age;
if(head){
while(tail->next!=NULL){
tail = tail->next;
}
tail->next = tmp;
}else{
head = tail = tmp;
}

}
return head;

}
void show(Person *head){
while(head !=NULL){
cout<<head->age<<endl;
head = head->next;
}
}
int main(){
Person *p;
// p = CrtHeadNode();
p = CrtTailNode();
show(p);
return 0;

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐