#include <stdio.h>
class Node
{
public:
Node* pre;
int num;
Node* next;
Node()
{
pre=NULL;
next=NULL;
}
Node(int num):num(num)
{
pre=NULL;
next=NULL;
}
~Node()
{
}
};
class LinkedList
{
public:
Node* phead;
LinkedList()
{
phead=new Node();
}
~LinkedList()
{
}
void Add(int num)
{
Node* pcur=phead;
while(pcur->next!=NULL)
{
pcur=pcur->next;
}
Node* pnew=new Node(num);
pnew->pre=pcur;
pcur->next=pnew;
}
void Add(int num,int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<index)
{
pcur=pcur->next;
i++;
}
Node* pnew=new Node(num);
pnew->pre=pcur;
pnew->next=pcur->next;
pcur->next->pre=pnew;
pcur->next=pnew;
}
int Remove(int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<=index)
{
pcur=pcur->next;
i++;
}
if(pcur!=NULL&&pcur->pre!=NULL) pcur->pre->next=pcur->next;
if(pcur!=NULL&&pcur->next!=NULL) pcur->next->pre=pcur->pre;
int temp=pcur->num;
delete pcur;
return temp;
}
int GetElement(int index)
{
Node* pcur=phead;
int i=0;
while(pcur->next!=NULL&&i<=index)
{
pcur=pcur->next;
i++;
}
return pcur->num;
}
int GetSize()
{
int i=0;
Node* pcur=phead;
while(pcur->next!=NULL)
{
pcur=pcur->next;
i++;
}
return i;
}
};
class Stack:public LinkedList
{
public:
void push(int num)
{
Add(num);
}
int pop()
{
return Remove(GetSize()-1);
}
};
int main()
{
Stack stack;
stack.push(1);
stack.push(2);
stack.push(3);
stack.push(4);
stack.push(5);
while(stack.GetSize()>0)
{
printf("%d\n",stack.pop());
}
return 0;
}
类实现只Stack
最新推荐文章于 2021-07-13 10:33:55 发布