单向链表->创建节点
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;
}
#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;
}
相关文章推荐
- 单向链表小练习--》创建链表头,添加删除链表节点,释放链表内存,选择插入数据
- 程序员面试100题(算法)之查找链表中倒数第k个节点(含单向链表的创建和打印)
- 单向链表(一) 节点结构体、创建链表、释放链表、遍历链表
- java创建节点和单向链表
- 利用类对象为节点创建简单单向链表
- 单向链表的创建、节点删除、链表输出、翻转(写法参考计蒜客数据结构课程)
- C语言:创建动态单向链表,创建完成后,输出每一个节点的数据信息。
- 单向链表的创建与遍历(先进先出和先进后出)
- 单向循环链表创建、遍历、插入、删除、查找(按位置,按元素值)、清空、销毁
- 单向链表的相关操作总结:创建、删除、查找、排序、统计链表大小、链表的反转和遍历等
- 实现单向循环链表的创建、测长、打印、插入、删除及逆置
- 单向链表的创建和操作
- 给出一个单向链表的头指针,输出该链表中倒数第K个节点的指针
- C语言 单向链表的创建、遍历、排序、删除操作
- 在单向链表中如何快速查到倒数第n个节点 这简直是一种神奇的思路!!!!leetcode 删除倒数第n个节点
- linux 下c语言创建单向动态链表的理解
- 华为机试题(10)--删除单向链表中的某些节点
- 2012年7月4日 题:创建一个 10节点 内容随机但不重复的 链表
- 13. 求单向链表中倒数第k个节点
- 单向链表在O(1)时间内删除一个节点