题目:前插法建立一个单链表
应用模型:单链表
代码:
#include<iostream>
#include<cstdio>
using namespace std;
struct List{
int d;
List *next;
}List;
void Insert(List *&h){
for(int i=0;i<n;i++){
cin>>x;
L *p;
p=new List;
p->d=x;
t->next=p->next;
t->next=p;
}
}
void show(List &t){
while(t!=NULL){
cout<<t->d<<endl;
t=t->next;
}
}
int main(){
int N;
while(cin>>N){
int n;
cin>>n;
List *h;
h=new List;
Insert(h);
show(h);
}
return 0;
}
以下有插入时指针操作图示
革命尚未成功!