#include<bits/stdc++.h>
using namespace std;
struct Listnode{
int val;
Listnode* next;
Listnode(int x) : val(x) ,next(nullptr){}
};
Listnode* InsertList(Listnode* head, int data){
Listnode* node = new Listnode(data);
if(head == nullptr){
return node;
}
if(data < head -> val){
node -> next = head;
head = node;
return head;
}
else{
Listnode* current = head;
while(current -> next != nullptr && current -> next -> val < data){
current = current -> next;
}
node -> next = current -> next;
current -> next = node;
return head;
}
}
void Listtravel(Listnode* head){
Listnode* current = head;
while(current != nullptr){
cout << current-> val << " ";
current = current -> next;
}
}
int main(){
int n;
while(cin >> n){
Listnode* head = nullptr;
for(int i = 0; i < n; i++){
int data;
cin >> data;
head = InsertList(head, data);
}
Listtravel(head);
cout << endl;
}
return 0;
}
在main()函数中调用insert函数时忘记接收返回值,已经是第二次这样错了

京公网安备 11010502036488号