数据结构——栈

顺序栈

#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&);//向链表第i个位置添加数据
    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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值