剑指offer-反向打印链表结点

#include <iostream>
#include <stack>
#include <cstdio>
#include <cstdlib>

using namespace std;

// bool clearList(LinkList *pList); // 清空链表操作

typedef struct ListNode{
    int m_value;
    ListNode*next;
}Node, LinkList;

bool initList(LinkList**pHead)
{
    // *pHead = (LinkList*)malloc(sizeof(Node));
    *pHead = new LinkList();
    if(!pHead)
        return false;
    (*pHead)->m_value = 0;
    (*pHead)->next = NULL;
    return true;
}

void travseList(LinkList*pHead)
{
    // pHead头节点是使用一个空的节点代替,数据为空但是指针指向第一个节点
    ListNode*pNode = pHead->next;
    while(pNode != NULL)
    {
        cout << pNode->m_value << " ";
        pNode = pNode->next;
    }
    cout << endl;
}

/* 在链表的尾部添加新的结点 */
bool addToTail(LinkList*pHead, int value)
{
    // ListNode*pNew = (LinkList*)malloc(sizeof(Node));
    LinkList*pNew = new LinkList();
    if(!pNew)
        return false;
    pNew->next = NULL;
    pNew->m_value = value;
    if(pHead == NULL)
    {
        // 原链表为空,直接插入作为头节点
        pHead->next = pNew;
    }else{
        // 非空,找到尾节点再插入
        ListNode*pNode = pHead;
        while(pNode->next != NULL)
            pNode = pNode->next;
        pNode->next = pNew;
    }
    return true;
}

/* 在链表的头部插入节点 */
bool addToHead(LinkList*pHead, int value)
{
    // LinkList*pNew = (LinkList*)malloc(sizeof(Node));
    LinkList*pNew = new LinkList();
    if(!pNew)
        return false;
    pNew->m_value = value;
    pNew->next = NULL;
    if(pHead == NULL)
    {
        // 原链表为空,直接插入作为头节点
        pHead->next = pNew;
    }else{
        pNew->next = pHead->next;
        pHead->next = pNew;
    }
}

/* 在链表中找到某个含有某值的第一个节点并且删除 */
bool removeNode(LinkList*pHead, int value)
{
    if(pHead == NULL)
        return false;
    // 获取第一个节点
    LinkList*pNode = pHead->next;
    if(pNode->m_value == value)
    {
        // 如果第一个节点就是满足条件的第一个节点,直接返回
        pHead->next = pNode->next;
        delete pNode;
        return true;
    }else{
        // 查找满足条件节点的前一个节点,这样下面的操作可以保证链表的连接性
        while(pNode->next != NULL && pNode->next->m_value != value)
            pNode = pNode->next;
        if(pNode->next != NULL)
        {
            LinkList*pDeleted = pNode->next;
            pNode->next = pNode->next->next;
            delete pDeleted;
            return true;
        }else{
            return false;
        }
    }
}

/* 根据某个位置的节点信息插入节点 */
bool insertList(LinkList*pHead, int i, int&value)
{
    if(pHead == NULL || i < 1)
        return false;
    LinkList*pNode = pHead;
    int cnt = 0;
    // 查找满足位置的前一个节点
    while(pNode->next != NULL && cnt < (i-1))
    {
        pNode = pNode->next;
        ++cnt;
    }
    // 当查找的节点位置超出插入新节点后的长度所容许的最大位置
    // 不操作直接返回
    if(cnt < (i - 1))
        return false;
    // 执行插入操作
    LinkList*pBefore = pNode;
    pNode = pNode->next;
    
    LinkList*p_insert = new LinkList();
    p_insert->m_value = value;
    p_insert->next = pNode;
    pBefore->next = p_insert;
    return true;
}

bool deleteList(LinkList*pHead, int i, int&value)
{
    if(pHead == NULL || i < 1)
        return false;
    LinkList*pNode = pHead;
    int cnt = 0;
    while(pNode->next != NULL && cnt < (i-1))
    {
        pNode = pNode->next;
        ++cnt;
    }
    // 这里跟插入不同,是因为有可能删除莫尾节点的下一个节点
    // 此时该节点是不存在的
    if(cnt < (i - 1) || pNode->next == NULL)
        return false;
    
    LinkList*pBefore = pNode;
    pNode = pNode->next;
    value = pNode->m_value;
    pBefore->next = pNode->next;
    delete pNode;
    return true;
}

bool getElem(LinkList*pHead, int i, int&value)
{
    if(pHead == NULL || i < 1)
        return false;
    LinkList*pNode = pHead;
    int cnt = 0;
    while(pNode->next != NULL && cnt < (i-1))
    {
        pNode = pNode->next;
        ++cnt;
    }
    if(cnt < (i - 1) || pNode->next == NULL)
        return false;
    
    value = pNode->next->m_value;
    return true;
}

bool clearList(LinkList*pHead)
{
    if(pHead == NULL || pHead->next == NULL)
        return false;
    
    LinkList*pHead_copy = pHead;
    LinkList*cur;
    LinkList*pNext;
    // 删除链表中拥有两个及其以上的节点
    while(pHead_copy->next->next != NULL)
    {
        cur = pHead_copy->next;
        pNext = pHead_copy->next->next;
        pHead_copy->next = pNext;
        delete cur;
    }
    // 删除最后一个节点
    cur = pHead_copy->next;
    pHead_copy->next = NULL;
    delete cur;
    
    return true;
}

/*
Q:
    从尾到头打印链表结点数据
A:
    1. 依次从头到尾遍历节点,然后将便利得到的节点依次压入一个堆栈
        遍历完成则依次pop出栈中的节点。(推荐)
    2. 使用递归,每次便利节点递归打印其后面一个节点。
    3. 从头到尾遍历结点,每次将遍历得到的节点用头插法插入一个新的临时链表
        遍历完成后,降临时链表从头到尾依次遍历打印即可。
*/
void reversePrintList_stack(LinkList*pHead)
{
    if(pHead == NULL || pHead->next == NULL)
        return;
    
    stack<LinkList*> stackNode;
    LinkList*pNode = pHead->next;
    while(pNode != NULL)
    {
        stackNode.push(pNode);
        pNode = pNode->next;
    }

    LinkList*node;
    while(!stackNode.empty())
    {
        node = stackNode.top();
        cout << node->m_value << " ";
        stackNode.pop();
    }
    cout << endl;

}

void reversePrintList_iteratively(LinkList*pHead)
{
    if(pHead != NULL)
    {
        if(pHead->next != NULL)
        {
            reversePrintList_iteratively(pHead->next);
        }
        cout << pHead->m_value << " ";
    }
}

void test_reversePrintList()
{
    cout << "test reversePrintList";
    LinkList*pHead;
    initList(&pHead);
    travseList(pHead);
    addToHead(pHead, 10);
    travseList(pHead);
    addToHead(pHead, 20);
    travseList(pHead);
    addToHead(pHead, 30);
    travseList(pHead);
    addToHead(pHead, -19);
    travseList(pHead);

    cout << "reverse print" << endl;
    reversePrintList_stack(pHead);
    reversePrintList_iteratively(pHead->next);

    LinkList*pHead_2;
    initList(&pHead_2);
    travseList(pHead_2);
    addToTail(pHead_2, 1);
    travseList(pHead_2);
    cout << "reverse print" << endl;
    reversePrintList_stack(pHead_2);
    reversePrintList_iteratively(pHead_2->next);

    LinkList*pHead_3;
    initList(&pHead_3);
    travseList(pHead_3);
    cout << "reverse print" << endl;
    reversePrintList_stack(pHead_3);
    reversePrintList_iteratively(pHead_3->next);

}

int main(int argc, char**argv)
{
    test_reversePrintList();

    return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值