1 //倒序输出
2 #include<iostream>
3 using namespace std;
4 struct node{
5 int data;
6 node*next;
7 };
8
9 node*header()
10 {
11 node *head, *n;
12 int a;
13 head = NULL;
14 cout << "首节点插入法产生链表,输入数据(-1结束):" << endl;
15
16 for (cin >> a; a != -1; cin >> a)
17 {
18 n = new node;
19 n->data = a;
20 if (!head)
21 head = n, n->next = NULL;
22 else
23 n->next = head, head = n;
24 }
25 return head;
26 }
27 int va(node *head)
28 {
29 node *a, *b;
30 a = head;
31 b = head->next;
32
33 for (; b != NULL; a = a->next, b = b->next)
34 {
35 if (a->data > b->data)
36 return 0;
37 }
38 return 1;
39 }
40
41 int main()
42 {
43 node *p1;
44 p1 = header();
45 node *cp = p1;//先弄个副本,p1指针要始终指向人为的head,这样循环完内存都不能回收了已经没有指针指向申请的那段内存了.head始终指向当前节点, 也就是最后申请的一个
46 while (cp)
47 {
48 cout << cp->data << " ";
49 cp = cp->next;
50 }
51 if (va(p1) == 1)
52 cout << "递增单链表元素!" << endl;
53 else cout << "非递增单链表元素!" << endl;
54 system("pause");
55 return 0;
56 }