这里最好采用链表的形式,每次取出最尾部的元素即可
#include <iostream>
using namespace std;
struct NODE
{
int num;
NODE* next;
}* PNODE;
int main()
{
NODE* head = new NODE;
head->next = NULL;
for(int i = 0; i < 10; ++i)
{
NODE* node = new NODE;
node->num = i;
node->next = head->next;
head->next = node;
}
NODE* pTmp = head->next;
while (pTmp)
{
cout << pTmp->num << endl;
pTmp = pTmp->next;
}
//弹出
pTmp = head->next;
while (pTmp->next->next)
{
pTmp = pTmp->next;
}
NODE* value_node = pTmp->next;
pTmp->next = NULL;
pTmp = head->next;
while (pTmp)
{
cout << pTmp->num << endl;
pTmp = pTmp->next;
}
return 1;
}
这里有一些判空的行为没有做,精力有限,就不做了,只是锻炼一下思维。