#include <stdio.h>
#include <stdlib.h>
typedef struct _stack
{
struct _stack* prev;
struct _stack* next;
int value;
}
stack;
void push( int data );
void pop(void);
stack *first = NULL;
stack *real_first = NULL;
int main(void)
{
push(22);
push(32);
push(42);
push(52);
push(4);
pop();
pop();
push(15);
stack *temp = real_first;
while (temp != NULL)
{
temp = temp->next;
}
temp = real_first;
while (real_first != NULL)
{
real_first = real_first->next;
temp = real_first;
}
return 0;
}
void push( int data)
{
stack
*newptr
= malloc(sizeof(stack
));; if (newptr == NULL)
{
return;
}
newptr->value = data;
newptr->prev = NULL;
if (first == NULL)
{
first = newptr;
real_first = first;
}
else
{
newptr->prev = first;
first->next = newptr;
first = newptr;
}
return;
}
void pop(void)
{
stack *tmp = real_first;
if ( real_first == NULL)
return;
else if ( real_first->next == NULL)
real_first = NULL;
return;
}
else
{
while(real_first->next->next != NULL)
{
real_first= real_first->next;
}
first = NULL;
first = real_first;
real_first->next = NULL;
}
real_first = tmp;
}
I2luY2x1ZGUgPHN0ZGlvLmg+CiNpbmNsdWRlIDxzdGRsaWIuaD4KCgoKdHlwZWRlZiBzdHJ1Y3QgX3N0YWNrCiAgICB7CiAgICAgICAgCiAgICAgICAgc3RydWN0IF9zdGFjayogcHJldjsKICAgICAgICBzdHJ1Y3QgX3N0YWNrKiBuZXh0OwogICAgICAgIGludCB2YWx1ZTsKICAgICAgICAKICAgIH0gCiAgICBzdGFjazsgCiAKIAp2b2lkIHB1c2goIGludCBkYXRhICk7CnZvaWQgcG9wKHZvaWQpOwoKc3RhY2sgKmZpcnN0ID0gTlVMTDsKc3RhY2sgKnJlYWxfZmlyc3QgPSBOVUxMOwoKaW50IG1haW4odm9pZCkKewogICAgCgogICAgcHVzaCgyMik7CiAgICBwdXNoKDMyKTsKICAgIHB1c2goNDIpOwogICAgcHVzaCg1Mik7CiAgICBwdXNoKDQpOwogICAgcG9wKCk7CiAgICBwb3AoKTsKICAgIHB1c2goMTUpOwoKICAgCiAgICBzdGFjayAqdGVtcCA9IHJlYWxfZmlyc3Q7CiAgICAKICAgIHdoaWxlICh0ZW1wICAhPSBOVUxMKQogICAgeyAgIAogICAgICAgIHByaW50ZigiJWkgIiwgdGVtcCAtPnZhbHVlKTsKICAgICAgICB0ZW1wICA9IHRlbXAtPm5leHQ7CiAgICAgICAKICAgIH0KICAgIAogICAgdGVtcCA9IHJlYWxfZmlyc3Q7CiAgICAKICAgICB3aGlsZSAocmVhbF9maXJzdCAhPSBOVUxMKQogICAgeyAgIAogICAgICAgIGZyZWUodGVtcCk7CiAgICAgICAgcmVhbF9maXJzdCA9IHJlYWxfZmlyc3QtPm5leHQ7CiAgICAgICAgdGVtcCAgPSByZWFsX2ZpcnN0OwogICAgICAgCiAgICB9CiAgICAKICAgIAogICAgcHJpbnRmKCJcbiIpOwogICAgcmV0dXJuIDA7Cn0KCgoKCgp2b2lkIHB1c2goIGludCBkYXRhKQp7CiAgICBzdGFjayAqbmV3cHRyID0gIG1hbGxvYyhzaXplb2Yoc3RhY2spKTs7CiAgICBpZiAobmV3cHRyID09IE5VTEwpCiAgICB7CiAgICAgICAgcmV0dXJuOwogICAgfQogICAgCiAgICBuZXdwdHItPnZhbHVlID0gZGF0YTsKICAgIAogICAgbmV3cHRyLT5wcmV2ID0gTlVMTDsKICAgIGlmIChmaXJzdCA9PSBOVUxMKQogICAgewogICAgICAgIGZpcnN0ID0gbmV3cHRyOwogICAgICAgIAogICAgICAgIHJlYWxfZmlyc3QgPSBmaXJzdDsKICAgICAgICAKICAgIH0KICAgIAogICAgZWxzZQogICAgeyAgIAogICAgICAgIAogICAgICAgIG5ld3B0ci0+cHJldiA9IGZpcnN0OwogICAgICAgIAogICAgICAgIGZpcnN0LT5uZXh0ID0gbmV3cHRyOwogICAgCiAgICAgICAgZmlyc3QgPSBuZXdwdHI7CiAgICAgICAgCiAgICB9CiAgICAKICAgIHJldHVybjsgIAp9CiAgICAKCnZvaWQgcG9wKHZvaWQpCnsgICAKICAgIHN0YWNrICp0bXAgPSAgcmVhbF9maXJzdDsKICAgIAogICAgaWYgKCByZWFsX2ZpcnN0ID09IE5VTEwpCiAgICAgICAgcmV0dXJuOwogICAgCiAgICBlbHNlIGlmICggcmVhbF9maXJzdC0+bmV4dCA9PSBOVUxMKQogICAgeyAgIGZyZWUoIHJlYWxfZmlyc3QpOwogICAgICAgICByZWFsX2ZpcnN0ID0gTlVMTDsKICAgICAgICByZXR1cm47CiAgICB9CiAgICBlbHNlCiAgICB7CiAgICAgICAgCiAgICB3aGlsZShyZWFsX2ZpcnN0LT5uZXh0LT5uZXh0ICE9IE5VTEwpCiAgICB7CiAgICByZWFsX2ZpcnN0PSByZWFsX2ZpcnN0LT5uZXh0OwogICAgfQogICAgCiAgICBmaXJzdCA9IE5VTEw7CiAgICBmaXJzdCA9IHJlYWxfZmlyc3Q7CiAgICBmcmVlKHJlYWxfZmlyc3QtPm5leHQpOwogICAgcmVhbF9maXJzdC0+bmV4dCA9IE5VTEw7CiAgICAKICAgIAogICAgfQogICAgIHJlYWxfZmlyc3QgPSAgdG1wOwp9