您的位置:首页 > 其它

用头插法实现单链表整表创建

2014-12-28 14:36 246 查看
#include<iostream>
#include<cstring>
#include<stdio.h>
#include<stdlib.h>
using namespace std;
typedef struct node{
int id;
node *next;
node();
node(int);
}node;
node::node(){
id = 0;
next = NULL;
}
node::node(int i){
id = i;
next = NULL;
}
node *head;
void createList(int n){
if(n<0) return;
head=new node(1);
node *temp;
for(int i=2;i<=n;i++){
temp=new node(i);
temp->next=head->next;
head->next=temp;
}
}
void createList_2(int n){//顺序建表
if(n<0) return;
head=new node(1);
node *temp,*p=head;
for(int i=2;i<=n;i++){
temp=new node(i);
p->next=temp;
p=temp;
}
}

void print(){
node *p=head;
while(p){
cout<<p->id<<" ";
p=p->next;
}cout<<endl;
}
int main(){
int n;
while(cin>>n){
createList_2(n);
print();
}
return 0;
}


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