利用头插法和头结点删除的方法,实现栈储存的特点
#include<iostream>
#include <stdio.h>
struct Node
{
int data;
Node *next;
};
class myStack
{
public:
myStack();
void push(int a);
int pop();
bool isEmpty();
int size();
private:
Node *head;
int N;
};
#include<iostream>
#include"myStack.h"
using namespace std;
myStack::myStack()
{
head=new Node;
N=0;
head->next=NULL;
}
void myStack::push(int a)
{
Node *p=new Node;
p->data=a;
if(head->next==NULL)
{
head->next=p;
p->next=NULL;
}
else
{
p->next=head->next;
head->next=p;
}
N++;
}
int myStack::pop()
{
Node *p=head->next;
int a=p->data;
head->next=p->next;
N--;
delete p;
return a;
}
bool myStack::isEmpty()
{
return head->next==NULL;
}
int myStack::size()
{
return N;
}