双栈结构

一 代码结构

二 代码详解

1. doublestack.h

/*************************************************************************
    > File Name: doublestack.h
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Mon 16 Feb 2015 10:35:07 AM WST
 ************************************************************************/
#ifndef DOUBLESTACK_H
#define DOUBLESTACK_H
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <iostream>
#include <memory>
using namespace std;

template<class T>
class Double_stack {
	private:
		int top0;
		int top1;
		const int n;
		T *array;
	public:
		Double_stack();
		bool empty() const;
		bool full() const;
		bool push(const T&);
		bool pop(T &);
		~Double_stack();
};

#endif

2. doublestack.cpp

/*************************************************************************
    > File Name: doublestack.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Mon 16 Feb 2015 09:53:20 PM WST
 ************************************************************************/

#include "doublestack.h"
template<class T>
Double_stack<T>::Double_stack():n(1024) {
	this->top0 = -1;
	this->top1 = n;
	this->array = new T(n);
	if(!this->array) {
		cerr << "memory lack...!" << endl;
		exit(EXIT_FAILURE);
	}
}
template<class T>
bool Double_stack<T>::empty() const {
	return this->top0 == -1 && this->top1 == n;
}
template<class T>
bool Double_stack<T>::full() const {
	return this->top0 + 1 == this->top1;
}
template<class T>
bool Double_stack<T>::push(const T& m) {
	if(this->full()) {
		cerr << "the stack is full...!" << endl;
		exit(EXIT_FAILURE);
	}
	static int choice;
	if(!choice) {
		this->top0++;
		this->array[top0] = m;
	}
	else {
		this->top1--;
		this->array[top1] = m;
	}
	choice = (choice + 1) & 1;
}
template<class T>
bool Double_stack<T>::pop(T& m) {
	if(this->empty()) {
		cerr << "the stack is empty...!" << endl;
		exit(EXIT_FAILURE);
	}
	static int choice;
	if(!choice) {
		m = this->array[top0];
		this->top0--;
	}
	else {
		m = this->array[top1];
		this->top1++;
	}
	choice = (choice + 1) & 1;
}
template<class T>
Double_stack<T>::~Double_stack() {
	delete this->array;
	this->top0 = -1;
	this->top1 = n;
}

3. main.cpp

/*************************************************************************
    > File Name: main.cpp
    > Author: wangzhicheng
    > Mail: 2363702560@qq.com 
    > Created Time: Mon 16 Feb 2015 10:20:01 PM WST
 ************************************************************************/
#include "doublestack.cpp"
int main() {
	Double_stack<int>doublestack;
	doublestack.push(1);
	doublestack.push(2);
	doublestack.push(3);
	int m;
	doublestack.pop(m);
	cout << m << endl;

	return 0;
}

4. makefile

CC=g++
all:
	$(CC) -g -o main main.cpp doublestack.cpp doublestack.h

三 程序运行截图


以下是双栈的基本操作代码C语言实现: 1. 采用顺序存储结构实现双栈 ```c #define MAXSIZE 100 typedef struct { int data[MAXSIZE]; int top1; // 栈1的栈顶指针 int top2; // 栈2的栈顶指针 } DoubleStack; // 初始化双栈 void InitDoubleStack(DoubleStack *S) { S->top1 = -1; S->top2 = MAXSIZE; } // 判断栈1是否为空 int IsEmpty1(DoubleStack *S) { return S->top1 == -1; } // 判断栈2是否为空 int IsEmpty2(DoubleStack *S) { return S->top2 == MAXSIZE; } // 判断双栈是否为空 int IsEmpty(DoubleStack *S) { return IsEmpty1(S) && IsEmpty2(S); } // 判断栈1是否已满 int IsFull1(DoubleStack *S) { return S->top1 + 1 == S->top2; } // 判断栈2是否已满 int IsFull2(DoubleStack *S) { return S->top2 - 1 == S->top1; } // 判断双栈是否已满 int IsFull(DoubleStack *S) { return IsFull1(S) || IsFull2(S); } // 栈1入栈 void Push1(DoubleStack *S, int x) { if (IsFull1(S)) { printf("Stack1 is full.\n"); return; } S->data[++S->top1] = x; } // 栈2入栈 void Push2(DoubleStack *S, int x) { if (IsFull2(S)) { printf("Stack2 is full.\n"); return; } S->data[--S->top2] = x; } // 栈1出栈 int Pop1(DoubleStack *S) { if (IsEmpty1(S)) { printf("Stack1 is empty.\n"); return -1; } return S->data[S->top1--]; } // 栈2出栈 int Pop2(DoubleStack *S) { if (IsEmpty2(S)) { printf("Stack2 is empty.\n"); return -1; } return S->data[S->top2++]; } ``` 2. 采用链表结构实现双栈 ```c typedef struct StackNode { int data; struct StackNode *next; } StackNode, *LinkStackPtr; typedef struct { LinkStackPtr top1; // 栈1的栈顶指针 LinkStackPtr top2; // 栈2的栈顶指针 } DoubleLinkStack; // 初始化双栈 void InitDoubleLinkStack(DoubleLinkStack *S) { S->top1 = NULL; S->top2 = NULL; } // 判断栈1是否为空 int IsEmpty1(DoubleLinkStack *S) { return S->top1 == NULL; } // 判断栈2是否为空 int IsEmpty2(DoubleLinkStack *S) { return S->top2 == NULL; } // 判断双栈是否为空 int IsEmpty(DoubleLinkStack *S) { return IsEmpty1(S) && IsEmpty2(S); } // 栈1入栈 void Push1(DoubleLinkStack *S, int x) { LinkStackPtr p = (LinkStackPtr)malloc(sizeof(StackNode)); p->data = x; p->next = S->top1; S->top1 = p; } // 栈2入栈 void Push2(DoubleLinkStack *S, int x) { LinkStackPtr p = (LinkStackPtr)malloc(sizeof(StackNode)); p->data = x; p->next = S->top2; S->top2 = p; } // 栈1出栈 int Pop1(DoubleLinkStack *S) { if (IsEmpty1(S)) { printf("Stack1 is empty.\n"); return -1; } LinkStackPtr p = S->top1; int x = p->data; S->top1 = p->next; free(p); return x; } // 栈2出栈 int Pop2(DoubleLinkStack *S) { if (IsEmpty2(S)) { printf("Stack2 is empty.\n"); return -1; } LinkStackPtr p = S->top2; int x = p->data; S->top2 = p->next; free(p); return x; } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值