Insert a single-chain table

Source: Internet
Author: User

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 ;}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.