链表程序中出现抛出异常
我想把两个链表连接起来后用冒泡排序法按照num大小排序,但是在第45行出现抛出异常
#include <iostream>
using namespace std;
struct node {
int num;
int grade;
node* next;
};
int main() {
node* head = 0;
head = new node;
head->next = 0;
node* s = head;
int n, m, a, c, i = 0;
cin >> n >> m;
while (i < n) {
cin >> a >> c;
node* t = 0;
t = new node;
t->num = a;
t->grade = c;
s->next = t;
s = t;
i++;
}
node* h = 0;
h = new node;
h->next = 0;
node* z = h;
i = 0;
while (i < m) {
cin >> a >> c;
node* b = 0;
b = new node;
b->num = a;
b->grade = c;
z->next = b;
z = b;
i++;
}
s->next = h->next;
s = head->next;
z = s->next;
for (int i = 0; i < n + m; i++) {
for (int j = 0; j < n + m - 1; j++) {
if (s->num > z->num) {
swap(s->num, z->num);
swap(s->grade, z->grade);
}
s = s->next;
z = z->next;
}
}
i = 0;
s = head->next;
while (i < n + m) {
cout << s->num << " " << s->grade << endl;
s = s->next;
i++;
}
}