链表打印从尾到头打印链表
在本文中,我们主要介绍链表打印的内容,自我感觉有个不错的建议和大家分享下
#include <stdlib.h>
#include <stdio.h>
#include <stack>
#include <algorithm>
using namespace std;
struct ListNode{
int m_Value;
ListNode *m_pNext;
}*List;
bool deleted =false;
void AddNodeToTail(ListNode** pHead,int value){
ListNode* pNew = new ListNode();
pNew->m_Value = value;
pNew->m_pNext = NULL;
if (NULL== *pHead)
{
*pHead=pNew;
}
else{
ListNode* pNode=*pHead;
while(pNode->m_pNext!=NULL)
pNode= pNode->m_pNext;
pNode->m_pNext=pNew;
}
}
void PrintListFromTail(ListNode* pHead){
std::stack<int> nodes;
ListNode* pNode=pHead;
while(NULL!=pNode){
nodes.push(pNode->m_Value);
pNode=pNode->m_pNext;
}
printf("从尾到头打印链表:\n");
while(!nodes.empty()){
printf("%d",nodes.top());
nodes.pop();
if(!nodes.empty())
printf("->");
}
}
int main(void){
for (int i=0;i<20;i++)
{
AddNodeToTail(&List,i);
}
ListNode* p=List;
while(p!=NULL){
printf("%d",p->m_Value);
p = p->m_pNext;
if(p!=NULL)
printf("->");
}
printf("\n");
PrintListFromTail(List);
getchar();
return 0;
}
文章结束给大家分享下程序员的一些笑话语录: 很多所谓的牛人也不过如此,离开了你,微软还是微软,Google还是Google,苹果还是苹果,暴雪还是暴雪,而这些牛人离开了公司,自己什么都不是。

浙公网安备 33010602011771号