class List
{
public:
List(){}
List(int num)
{
this->Data = num;
}
int Data;
List *next;
};
class ListStack
{
public:
void CreatListStack()
{
head->next = nullptr;
}
bool isEmpty()
{
return head->next == nullptr;
}
void Push(int val)
{
List * newNode = new List(val);
List * temp = head;
newNode->next=temp->next;
temp->next = newNode;
}
void Pop()
{
if (isEmpty())
{
cout << "堆栈为空,没有元素可以弹出" << endl;
return;
}
List * firstNode = new List[sizeof(int)];
int Topnum;
firstNode = head->next;
head->next = firstNode->next;
Topnum = firstNode->Data;
delete firstNode;
cout << "栈弹出元素为:" << Topnum << endl;
}
void ListPrint()
{
if (isEmpty())
{
cout << "栈为空,没有元素可以打印" << endl;
return;
}
List *temp = head->next;
while (true)
{
if (temp == nullptr)
{
break;
}
cout << temp->Data << " ";
temp = temp->next;
}
}
private:
List *head = new List(-1);
};
int main()
{
ListStack Stack;
Stack.CreatListStack();
Stack.Push(10);
Stack.Push(20);
Stack.Push(30);
Stack.Push(40);
Stack.Push(50);
Stack.Push(60);
Stack.ListPrint();
cout << endl;
Stack.Pop();
Stack.Pop();
Stack.Pop();
Stack.ListPrint();
cout << endl;
Stack.Pop();
Stack.Pop();
Stack.Pop();
Stack.ListPrint();
cout << endl;
system("pause");
return 0;
}