剑指Offer系列---(7)从尾到头打印链表

1.题目描述:

输入一个链表的头结点,从尾到头反过来打印出每个结点的值。链表结点的定义如下:

struct ListNode{

    int m_nKey;

    ListNode *m_pNext;

};

2.源代码:

A1.用栈实现的“从尾到头打印链表”程序代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <memory.h>

#ifndef ERROR
#define ERROR (0)
#endif
#ifndef OK
#define OK   (!ERROR)
#endif

#define STACK_INIT_SIZE 1
#define STACKINCREMENT 10

typedef int ElemType;
typedef struct Node{
    ElemType data;
    struct Node *next;
}Node,*pNode;

typedef int SElemType;
typedef struct SqStack{
    SElemType  *base;
    SElemType  *top;
    int  stacksize;
}SqStack,*pStack;

pStack S;

pStack InitStack(pStack S)
{
    S = (pStack)malloc(STACK_INIT_SIZE * sizeof(SElemType));
    if (S == NULL) {
        return ERROR;
    }
    S->base = (SElemType *)S;
    S->top = S->base;
    S->stacksize = STACK_INIT_SIZE;
    
    return S;
}

pStack Push(pStack S,SElemType e)
{
    if((S->top - S->base) >= S->stacksize){
        S->base = (SElemType*)realloc(S, (S->stacksize +STACKINCREMENT)*sizeof(SElemType));
        if (S->base == NULL) {
            return ERROR;
        }
        S->top = S->base + S->stacksize;
        S->stacksize += STACKINCREMENT;
    }
    *S->top++ = e;
    return S;
}

SElemType Pop(pStack S)
{
    return *(--S->top);
}

pNode CreateList()
{
    ElemType val;
    pNode pHead = NULL;
    pNode pCur = NULL;
    
    do{
        scanf("%d",&val);
        if(val != -1){
            pNode pNew = (pNode)malloc(sizeof(Node));
            if (pNew == NULL)
                    exit(EXIT_FAILURE);
                pNew->data = val;
                pNew->next = NULL;
                
                if (pHead == NULL) {
                    pHead = pNew;
                    pCur = pHead;
                }else{
                    pCur->next = pNew;
                    pCur = pCur->next;
                }
        }
    }while (val != -1);
    return pHead;
}

void DestroyList(pNode pHead)
{
    if(pHead == NULL)
        return;
    pNode p = NULL;
    while (pHead != NULL) {
        p = pHead->next;
        free(pHead);
        pHead = p;
    }
}

void PrintListReverse(pNode pHead)
{
    if(pHead == NULL)
        return;
    while (pHead != NULL) {
        Push(S, pHead->data);
        pHead = pHead->next;
    }
    while (S->top != S->base) {
        printf("%d\n",Pop(S));
    }
}

int main(int argc, char ** argv)
{
    S = InitStack(S);
    
    pNode pHead = CreateList();
    PrintListReverse(pHead);
    DestroyList(pHead);
    return 0;
}

A2.用递归实现的“从尾到头打印链表”程序代码如下:

#include <stdio.h>
#include <stdlib.h>

typedef int ElemType;

typedef struct Node{
    ElemType  data;
    struct Node *next;
}Node,*pNode;

void PrintListReverse(pNode pHead)
{
    if(pHead == NULL)
        return;
    if(pHead->next != NULL)
        PrintListReverse(pHead->next);
    printf("%d\n",pHead->data);
}

pNode CreateList()
{
    ElemType val;
    pNode pHead = NULL;
    pNode pCur = NULL;
    
    do{
        scanf("%d",&val);
        if(val != -1){
            pNode pNew = (pNode)malloc(sizeof(Node));
            if(pNew == NULL)
                exit(EXIT_FAILURE);
            pNew->data = val;
            pNew->next = NULL;
            
            if(pHead == NULL){
                pHead = pNew;
                pCur = pHead;
            }else{
                pCur->next = pNew;
                pCur = pCur->next;
            }
        }
    }while (val != -1);
    return pHead;
}

void DestroyList(pNode pHead)
{
    if (pHead == NULL) {
        return;
    }
    pNode p = NULL;
    while (pHead != NULL) {
        p = pHead->next;
        free(pHead);
        pHead = p;
    }
}


int main(int argc,char **argv)
{
    pNode pHead = CreateList();
    PrintListReverse(pHead);
    DestroyList(pHead);
    return 0;
}

3.小结:

1)对于反向输出时,应该考虑它的特性,选择数据结构类型来实现。一定要搞清楚各种数据结构类型的特点;

2)对于栈能实现的例子,一般要想到也可以用递归来完成。递归的缺点就是递归层级很深时,可能导致函数调用栈溢出;

3)在面试过程中,可以试着问面试官一些关于题目的信息,比如上面的从尾到头打印链表,是否允许对输入的数据做修改。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值