1. Question:
Problem description creates a single-chain table, inserts a specified node after the nth node, and completes the traversal. There are multiple groups of input data, each of which occupies two rows:
The first row has two numbers (n, m). The first number N indicates the node position, and the second number indicates the number to be inserted. Each node of the subsequent documentary linked list (no more than 100) ends a single-chain table with 0.
For example:
3 5
1 2 3 4 5 0
0 0
When 0 0 is encountered, end the program. Output output the inserted single-chain table. Sample Input
3 51 2 3 4 5 00 0
Sample output
1 2 3 5 4 5
2. Code:
# Include <iostream> using namespace STD; struct node {int data; node * Next;}; Class linklist {PRIVATE: node * head; public: linklist (int A [], int N );~ Linklist (); void insert (int I, int X); void show () ;}; linklist: linklist (int A [], int N) {int I; node * r, * s; head = new node; r = head; for (I = 0; I <n; I ++) {S = new node; s-> DATA = A [I]; S-> next = r-> next; r-> next = s; r = s;} r-> next = NULL ;} linklist ::~ Linklist () {node * P, * q; P = head; while (p) {q = P; P = p-> next; Delete Q ;}} void linklist :: insert (int I, int X) {node * P, * s; P = head; int COUNT = 0; while (P & count <I-1) {P = p-> next; count ++;} If (P = NULL) throw "location"; else {S = new node; S-> DATA = X; s-> next = p-> next; P-> next = s;} void linklist: Show () {node * P; P = head-> next; cout <p-> data; P = p-> next; while (p) {cout <"<p-> data; P = p-> next ;} cout <Endl;} int main () {int n, m, a [100], I; while (CIN> N> m) {I = 0; if (n = 0 | M = 0) break; else {While (CIN> A [I]) {if (a [I] = 0) break; else I ++;} linklist K (A, I); K. insert (n + 1, m); K. show () ;}} return 0 ;}