单链表逆序或者逆序输出

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

 

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

<span style="font-size:14px;">#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;


typedef struct node{
    int data;
    node * next;
}node;

//尾部添加
node * add(int n, node * head){
    node * t = new node;
    t->data = n;
    t->next = NULL;
    if (head == NULL){
        head = t;
    }
    else if (head->next == NULL){
        head->next = t;
    }
    else{
        node * p = head->next;
        while (p->next != NULL){
            p = p->next;
        }
        p->next = t;
    }
    return head;
}

//顺序输出
void print(node * head){
    node * p = head;
    while (p != NULL){
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}

//递归
void reversePrint(node * p){
    if (p != NULL){
        reversePrint(p->next);
        cout << p->data << " ";
    }
}

//栈
void reversePrint2(node * head){
    stack<int> s;
    while (head != NULL){
        s.push(head->data);
        head = head->next;
    }

    while (!s.empty()){
        cout << s.top() << " ";
        s.pop();
    }
}

int main(){

    node * head = NULL;
    for (int i = 1; i <= 5; i++){
        head = add(i, head);
    }
        print(head);
        reversePrint(head);
        reversePrint2(head);
    system("pause");
        return 0;
}</span>

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

 

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

<span style="font-size:14px;">#include<iostream>
#include<stack>
#include<assert.h>
using namespace std;


typedef struct node{
    int data;
    node * next;
}node;

node * add(int n, node * head){
    node * t = new node;
    t->data = n;
    t->next = NULL;
    if (head == NULL){
        head = t;
    }
    else if (head->next == NULL){
        head->next = t;
    }
    else{
        node * p = head->next;
        while (p->next != NULL){
            p = p->next;
        }
        p->next = t;
    }
    return head;
}

//循环
node * reverse(node * head){

    if (head == NULL || head->next == NULL){
        return head;
    }

    node * p1 = head;
    node * p2 = head->next;
    node * p3 = NULL; 
    head->next = NULL;

    while (p2 != NULL){
        p3 = p2;
        p2 = p2->next;
        p3->next = p1;
        p1 = p3;
    }
    head = p1;
    return head;
}

void print(node * head){
    node * p = head;
    while (p != NULL){
        cout << p->data << " ";
        p = p->next;
    }
    cout << endl;
}


//递归
node * reverse2(node * p){
    if (p == NULL || p->next == NULL){
        return p;
    }

    node * newHead = reverse2(p->next);
    p->next->next = p;
    p->next = NULL;
    return newHead;
}


int main(){

    node * head = NULL;
    for (int i = 1; i <= 5; i++){
        head = add(i, head);
    }
    print(head);
    head = reverse(head);
    print(head);
    head = reverse2(head);
    print(head);

    system("pause");
    return 0;
}</span>

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值