顺序栈
#include <string>
#include <iostream>
#include <cstdio>
using namespace std;
typedef int DataType;
class Node
{
public:
DataType data;
Node *next;
};
class List
{
public:
List();
~List();
int Create(int size);
int makeEmpty();
int Output();
int Insert(Node *data, int n);
int Delete(int n);
int GetLen();
bool IsEmply();
Node *head;
int size;
};
List::List()
{
head = new Node;
head->data = 0;
head->next = NULL;
size = 0;
}
List::~List()
{
delete head;
}
int List::Create(int n)
{
if (n<0) {
printf("error\n");
return -1;
}
Node *ptemp = NULL;
Node *pnew = NULL;
this->size = n;
ptemp = this->head;
for(int i =0 ; i<n ; i++)
{
pnew = new Node;
pnew->next = NULL;
cout << "输入第" << i+1 << "个节点值" << endl;
cin >> pnew->data;
ptemp->next = pnew;
ptemp = pnew;
}
cout << "创建完成" << endl;
return 0;
}
int List::makeEmpty()
{
Node *ptemp;
if (this->head == NULL) {
cout << "链表原本就为空" << endl;
return -1;
}
while (this->head)
{
ptemp = head->next;
delete head;
head = ptemp;
}
cout << "销毁链表完成" << endl;
return 0;
}
int List::Output()
{
Node *ptemp = this->head->next;
if (this->head == NULL) {
cout << "链表为空" << endl;
return -1;
}
while(ptemp)
{
cout << ptemp->data << "->";
ptemp = ptemp->next;
}
cout <<"NULL"<< endl;
return 0;
}
int List::Insert(Node *data, int n)
{
Node *ptemp;
if (this->head == NULL) {
cout << "链表为空" << endl;
return -1;
}
if (data == NULL) {
cout << "插入节点为空" << endl;
return -1;
}
if (n<2) {
Node *pnew = new Node;
pnew->data = data->data;
pnew->next = this->head->next;
this->head->next = pnew;
this->size++;
return 0;
}
if (n > this->size) {
ptemp = this->head;
while (ptemp->next != NULL) {
ptemp = ptemp->next;
}
Node *pnew = new Node;
pnew->data = data->data;
pnew->next = NULL;
ptemp->next = pnew;
this->size++;
return 0;
}
else {
ptemp = this->head;
for (int i = 1; i < n; i++) {
ptemp = ptemp->next;
}
Node *pnew = new Node;
pnew->data= data->data;
pnew->next = ptemp->next;
ptemp->next = pnew;
this->size++;
return 0;
}
}
int List::Delete(int n)
{
Node *ptemp;
Node *ptemp2;
if (n > this->size) {
cout << "n太大" << endl;
return -1;
}
if (n < 2) {
ptemp = this->head->next;
this->head->next = ptemp->next;
delete ptemp;
this->size--;
return 0;
}
if (n == this->size) {
ptemp = this->head;
for (int i = 1; i < this->size;i++) {
ptemp = ptemp->next;
}
ptemp2 = ptemp->next;
ptemp->next = NULL;
delete ptemp2;
this->size--;
return 0;
}
else
{
ptemp = this->head;
for (int i = 1; i < n; i++) {
ptemp = ptemp->next;
}
ptemp2 = ptemp->next;
ptemp->next = ptemp2->next;
delete ptemp2;
this->size--;
return 0;
}
}
int List::GetLen()
{
return this->size;
}
bool List::IsEmply()
{
if (this->head == NULL) {
return true;
}
else{
return false;
}
}
int main()
{
List list;
List *p = &list;
p->Create(5);
p->Output();
Node temp;
temp.data = 100;
temp.next = NULL;
p->Insert(&temp, 0);
p->Output();
p->Insert(&temp, p->GetLen()+1);
p->Output();
p->Insert(&temp, 5);
p->Output();
p->Delete(0);
p->Output();
p->Delete(list.GetLen());
p->Output();
p->Delete(2);
p->Output();
p->makeEmpty();
system("pause");
}
链式栈
#include <iostream>
#include <cassert>
using namespace std;
typedef int dt;
struct Node {
Node* link;
dt data;
Node(Node* ptr = NULL) { link = ptr; }
Node(const dt& item, Node* ptr = NULL) { data = item, link = ptr; }
};
class List {
public:
List() { first = new Node; }
List(const dt& x) { first = new Node(x); }
List(const List& ln);
~List() { makeEmpty(); };
void makeEmpty();
bool Insert(int, dt&);
bool Remove(int i, dt& x);
bool isEmpty()const { return first->link == NULL ? 1 : 0; }
int size()const { return length; }
void show();
Node* getHead()const { return first; }
Node* Search(dt);
Node* Locate(int)const;
bool getDate(int i, dt& x)const;
void setDate(int i, dt& x);
void output();
void Create(int n);
protected:
Node* first;
int length;
};
List::List(const List& ln) {
dt value;
Node* srcptr = ln.getHead();
Node* destptr = first = new Node;
while (srcptr->link) {
value = srcptr->link->data;
destptr->link = new Node(value);
destptr = destptr->link;
srcptr = srcptr->link;
length++;
}
destptr->link = NULL;
}
void List::makeEmpty() {
Node* q;
while (first->link) {
q = first->link;
first->link = q->link;
delete q;
}
}
Node* List::Search(dt x) {
Node* p = first->link;
while (p) {
if (p->data == x)break;
else p = p->link;
}
return p;
}
Node* List::Locate(int i) const {
Node* p = first->link;
if (i < 0)return NULL;
int k = 0;
while (p && k < i) {
p = p->link;
k++;
}
return p;
}
bool List::getDate(int i, dt& x)const {
if (i < 0)return NULL;
Node* p = Locate(i);
if (p == NULL)return 0;
else { x = p->data; return 1; }
}
void List::setDate(int i, dt& x) {
if (i <= 0)return;
Node* p = Locate(i);
if (p == NULL)return;
else p->data = x;
}
bool List::Insert(int i, dt& x) {
if (first == NULL || i == 0) {
Node* newnode = new Node(x);
if (newnode == NULL) { cerr << "存储分配错误!" << endl; exit(1); }
newnode->link = first;
first = newnode;
}
else {
Node* p = Locate(i - 1);
if (p == NULL)return 0;
Node* newnode = new Node(x);
if (newnode == NULL) { cerr << "存储分配错误!" << endl; exit(1); }
newnode->link = p->link;
p->link = newnode;
}
length++;
return 1;
}
bool List::Remove(int i, dt& x) {
Node* p = Locate(i - 1);
if (p == NULL || p->link == NULL)return 0;
Node* del = p->link;
p->link = del->link;
x = del->data; delete del; length--; return 1;
}
void List::output() {
Node* p = first->link;
while (p) {
cout << p->data << " ";
p = p->link;
}
cout << endl;
}
void List::Create(int n)
{
if (n < 0) {
printf("error\n");
return;
}
Node* ptemp = NULL;
Node* pnew = NULL;
this->length = n;
ptemp = this->first;
for (int i = 0; i < n; i++) {
pnew = new Node;
pnew->link = NULL;
cout << "输入第" << i + 1 << "个节点值" << endl;
dt x;
cin >> x;
pnew->data = x;
ptemp->link = pnew;
ptemp = pnew;
}
cout << "创建完成" << endl;
return;
}
class Stack {
public:
Stack(){}
virtual void Push(const dt& x) = 0;
virtual bool Pop(dt& x) = 0;
virtual bool getTop(dt& x) const = 0;
virtual bool IsEmpty()const = 0;
virtual int getSize()const = 0;
};
class linkStack :public Stack {
public:
linkStack() :Top(NULL) {}
~linkStack() { makeEmpty(); }
void makeEmpty();
friend ostream& operator<<(ostream& os, linkStack& s);
virtual void Push(const dt& x);
virtual bool Pop(dt& x);
virtual bool getTop(dt& x) const;
virtual bool IsEmpty()const { return (Top == NULL) ? 1 : 0; }
virtual int getSize()const;
private:
Node* Top;
};
void linkStack::makeEmpty() {
Node* p;
while (Top) {
p = Top;
Top = Top->link;
delete p;
}
}
void linkStack::Push(const dt& x) {
Top = new Node(x,Top);
assert(Top != NULL);
}
bool linkStack::Pop(dt& x) {
if (IsEmpty())return 0;
Node* p = Top;
Top = Top->link;
x = p->data; delete p;
return 1;
}
bool linkStack::getTop(dt& x) const {
if (IsEmpty())return 0;
x = Top->data; return 1;
}
int linkStack::getSize()const {
Node* p = Top; int k = 0;
while (p != NULL) {
p = p->link;
k++;
}
return k;
}
ostream& operator<<(ostream& os, linkStack& s) {
os << "个数 " << s.getSize() << endl;
Node* p = s.Top; int i = 0;
while (p!=NULL) {
os << ++i << ":" << p->data << endl;
p = p->link;
}
return os;
}
int main() {
linkStack ls1;
ls1.Push(1);
cout << ls1;
ls1.Push(2);
ls1.Push(3);
ls1.Push(4);
cout << ls1;
int x=0;
cout << ls1.getTop(x) << endl;
cout << x<<endl;
ls1.Pop(x);
cout << ls1;
}