剑指offer-题目1511:从尾到头打印链表 (2013.12.27)

来源:互联网 发布:java语言好学吗 编辑:程序博客网 时间:2024/05/03 01:53
#include <iostream>
#include <string>
#include <stdio.h>
using namespace std;
struct Entry
{
    int m;
    Entry *next;
};
Entry *GetNewEntry()
{
    int p;
    cin>>p;
    if (p==-1) return NULL;
    Entry *newOne=new Entry;
    newOne->m=p;
    newOne->next=NULL;
    return newOne;
}
Entry *BuildList()
{
    Entry *listHead=NULL;
    while (true)
    {
        Entry *newOne=GetNewEntry();
        if(newOne==NULL) break;
        newOne->next=listHead;
        listHead=newOne;
    }
    return listHead;
}
 
 
void printList(Entry *list)
{
    while (list!=NULL)
    {
        printf("%d\n",list->m);
        list=list->next;
    }
}
int main()
{
    Entry *list=BuildList();
    printList(list);
    return 0;
}
/**************************************************************
    Problem: 1511
    User: 无梦楼主lv
    Language: C++
    Result: Accepted
    Time:160 ms
    Memory:4556 kb
****************************************************************/
0 0
原创粉丝点击