码迷,mamicode.com
首页 > 其他好文 > 详细

单链表逆序或者逆序输出

时间:2014-08-22 19:26:19      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   os   io   for   div   log   new   

分为两种情况,一种是只逆序输出,实际上不逆序;另一种是把链表逆序。

 

********************逆序输出***********************

 

 1 #include<iostream>
 2 #include<stack>
 3 #include<assert.h>
 4 using namespace std;
 5 
 6 
 7 typedef struct node{
 8     int data;
 9     node * next;
10 }node;
11 
12 //尾部添加
13 node * add(int n, node * head){
14     node * t = new node;
15     t->data = n;
16     t->next = NULL;
17     if (head == NULL){
18         head = t;
19     }
20     else if (head->next == NULL){
21         head->next = t;
22     }
23     else{
24         node * p = head->next;
25         while (p->next != NULL){
26             p = p->next;
27         }
28         p->next = t;
29     }
30     return head;
31 }
32 
33 //顺序输出
34 void print(node * head){
35     node * p = head;
36     while (p != NULL){
37         cout << p->data << " ";
38         p = p->next;
39     }
40     cout << endl;
41 }
42 
43 //递归
44 void reversePrint(node * p){
45     if (p != NULL){
46         reversePrint(p->next);
47         cout << p->data << " ";
48     }
49 }
50 
51 //
52 void reversePrint2(node * head){
53     stack<int> s;
54     while (head != NULL){
55         s.push(head->data);
56         head = head->next;
57     }
58 
59     while (!s.empty()){
60         cout << s.top() << " ";
61         s.pop();
62     }
63 }
64 
65 int main(){
66 
67     node * head = NULL;
68     for (int i = 1; i <= 5; i++){
69         head = add(i, head);
70     }
71         print(head);
72         reversePrint(head);
73         reversePrint2(head);
74     system("pause");
75         return 0;
76 }                

 

逆序输出可以用三种方法: 递归,栈,逆序后输出。最后一种接下来讲到。

 

*********************单链表逆序********************

 

 1 #include<iostream>
 2 #include<stack>
 3 #include<assert.h>
 4 using namespace std;
 5 
 6 
 7 typedef struct node{
 8     int data;
 9     node * next;
10 }node;
11 
12 node * add(int n, node * head){
13     node * t = new node;
14     t->data = n;
15     t->next = NULL;
16     if (head == NULL){
17         head = t;
18     }
19     else if (head->next == NULL){
20         head->next = t;
21     }
22     else{
23         node * p = head->next;
24         while (p->next != NULL){
25             p = p->next;
26         }
27         p->next = t;
28     }
29     return head;
30 }
31 
32 //循环
33 node * reverse(node * head){
34 
35     if (head == NULL || head->next == NULL){
36         return head;
37     }
38 
39     node * p1 = head;
40     node * p2 = head->next;
41     node * p3 = NULL; 
42     head->next = NULL;
43 
44     while (p2 != NULL){
45         p3 = p2;
46         p2 = p2->next;
47         p3->next = p1;
48         p1 = p3;
49     }
50     head = p1;
51     return head;
52 }
53 
54 void print(node * head){
55     node * p = head;
56     while (p != NULL){
57         cout << p->data << " ";
58         p = p->next;
59     }
60     cout << endl;
61 }
62 
63 
64 //递归
65 node * reverse2(node * p){
66     if (p == NULL || p->next == NULL){
67         return p;
68     }
69 
70     node * newHead = reverse2(p->next);
71     p->next->next = p;
72     p->next = NULL;
73     return newHead;
74 }
75 
76 
77 int main(){
78 
79     node * head = NULL;
80     for (int i = 1; i <= 5; i++){
81         head = add(i, head);
82     }
83     print(head);
84     head = reverse(head);
85     print(head);
86     head = reverse2(head);
87     print(head);
88 
89     system("pause");
90     return 0;
91 }

 

这里链表逆序用了两种方法:循环,递归。理解的方法是在纸上自己画一下。

单链表逆序或者逆序输出

标签:style   blog   color   os   io   for   div   log   new   

原文地址:http://www.cnblogs.com/piginthetree/p/3929963.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!