表,栈,队列程序代码片段

10 篇文章 0 订阅
5 篇文章 0 订阅
#include<iostream>
#include<list>
using namespace std;

template <typename object>
void printLots(const list<object> & L,const list<object> & P)
{
	list<object>::const_iterator iter1=L.begin();
	list<object>::const_iterator iter2=P.begin();
	int start=1;
	for(;iter2!=P.end()&&iter1!=L.end();iter2++)
	{
		while(start<*iter2 && iter1!=L.end())
		{
			start++;
			iter1++;
		}
		if(iter1!=L.end())
			cout<<*iter1<<" ";
	}
}

int main(){
	int a[]={1,2,3,4,5,6,7,8};
	int b[]={1,3,4,6};
	list<int> L(a,a+8);
	list<int> P(b,b+4);
	printLots(L,P);
	cout<<endl;
	return 0;
}


/*交换单链表结点,不是简单的数据交换*/
void swapWithNext(Node *beforeP)
{
	Node *p,afterP;
	p=beforeP->next;
	afterP=p->next;/*假设这两结点不为空*/

	p->next=afterP->next;
	afterP->next=p;
	beforeP->next=afterP;
}

/*交换双链表相邻结点*/
void swapWithNext(Node *P)
{
	Node *beforeP;
	Node *afterP;
	beforeP=P->prev;
	afterP=P->next;

	P->next=afterP->next;
	afterP->next->prev=p;
	afterP->next=p;
	p->prev=afterP;
	beforeP->next=afterP;
	afterP->prev=beforeP;
}

#include<iostream>
#include<string>
#include<algorithm>
#include<stack>
using namespace std;
/*由后缀式求结果*/
double evalPostFix(const string & str)
{
	char space=' ';
	string::const_iterator ptr=str.begin();
	string::const_iterator trailer;
	stack<double> s;
	double a,b,result;
	while(ptr!=str.end())
	{
		trailer=ptr;
		ptr=find(ptr,str.end(),space);
		ptr++;
		string token(trailer,ptr);
		if(token[0]!= '=')
		{
			result=atof(token.c_str());
			if(result!=0)
				s.push(result);
			else if(token=="0.0" || token=="0")
				s.push(result);
			else
			{
				b=s.top();
				s.pop();
				a=s.top();
				s.pop();
				switch(token[0]){
					case '+':
						result=a+b;
						break;
					case '-':
						result=a-b;
						break;
					case '*':
						result=a*b;
						break;
					case '/':
						result=a/b;
						break;
				}
				s.push(result); 
			}
		}
	}
	cout<<result<<endl;
	return result;
}

/*由中缀式构造后缀式*/
void inToPostfix(string & result)
{
	stack<char> s;
	string token;
	cout<<"中缀式是:";
	cin>>token;
	double temp;
	while(token!="=")
	{
		temp=atof(token.c_str());
		if(temp!=0.0)
			result=result+token+" ";
		else if(token=="0.0" || token=="0")
			result=result+token+" ";
		else
		switch(token[0])
		{
		case '(':
			s.push('(');
			break;
		case ')':
			while(s.top()!='(')
			{
				result=result+s.top()+" ";
				s.pop();
			}
			s.pop();
			break;
		case '+':
		case '-':
			while( !s.empty() && s.top()!='(')
			{
				result=result+s.top()+" ";
				s.pop();
			}
			s.push(token[0]);
			break;
		case '*':
		case '/':
			while(!s.empty() && s.top()!='+' && s.top()!='-' && s.top()!='(')
			{
				result=result+s.top()+" ";
				s.pop();
			}
			s.push(token[0]);
			break;
		}
		cin>>token;
	}
	while(!s.empty())
	{
		result=result+s.top()+" ";
		s.pop();
	}
	result=result+"= ";	
	cout<<"后缀式是:";
	cout<<result<<endl;
}

int main(){
	string str;
	inToPostfix(str);
	evalPostFix(str);
	return 0;
}


/*一个数组实现两个栈*/

#include<iostream>
using namespace std;

template <typename Object>
class Stack{
public:
	explicit Stack(int n=0):theSize(n),theSize1(0),theSize2(0)
	{
		objects=new Object[n];
	}
	~Stack(){
		delete [] objects;
	}
	bool isfull()
	{
		return theSize1+theSize2>=theSize;
	}
	bool push1(const Object & x)
	{
		if(isfull())
		{
			cout<<"数组空间已满!"<<endl;
			return false;
		}
		objects[theSize1]=x;
		theSize1++;
		return true;
	}
	const Object &top1()
	{
		return objects[theSize1-1];
	}
	bool isEmpty1()
	{
		return theSize1==0;
	}
	bool pop1()
	{
		if(isEmpty1())
		{
			cout<<"栈1为空!不能pop!"<<endl;
			return false;
		}
		theSize1--;
		return true;
	}


	bool push2(const Object & x)
	{
		if(isfull())
		{
			cout<<"数组空间已满!"<<endl;
			return false;
		}
		objects[theSize-theSize2-1]=x;
		theSize2++;
		return true;
	}
	const Object &top2()
	{
		return objects[theSize-theSize2];
	}
	bool isEmpty2()
	{
		return theSize2==0;
	}
	bool pop2()
	{
		if(isEmpty2())
		{
			cout<<"栈2为空!不能pop!"<<endl;
			return false;
		}
		theSize2--;
		return true;
	}
private:
	int theSize1;
	int theSize2;
	int theSize;
	Object *objects;
};

int main(){
	Stack<int> s(10);
	s.push1(1);

	for(int i=1;i!=10;i++)
		s.push2(i);
	cout<<s.isfull()<<endl;
	s.push1(2);/*已满*/
	s.pop2();/*删除第二个栈顶*/
	s.push1(2);/*ok!*/
	for(int i=1;i!=3;i++)
	{
		cout<<s.top1()<<" ";
		s.pop1();
	}
	cout<<endl;

	for(int i=1;i!=9;i++)
	{
		cout<<s.top2()<<" ";
		s.pop2();
	}
	cout<<endl;
	return 0;
}

/*用list链表实现的双端队列*/

#include<iostream>
#include<list>
using namespace std;

template<typename Object>
class Deque{
public:
	Deque(){l=list<Object>();}
	void push(const Object &x)
	{
		l.push_front(x);
	}
	const Object &pop()
	{
		Object old=l.front();
		l.pop_front();
		return old;
	}
	void inject(const Object &x)
	{
		l.push_back(x);
	}
	const Object &eject()
	{
		Object old=l.back();
		l.pop_back();
		return old;
	}
private:
	list<Object> l;
};

int main(){
	Deque<int> d;
	d.push(1);
	d.push(2);
	cout<<d.pop()<<endl;
	d.inject(3);
	cout<<d.eject()<<endl;
	cout<<d.eject()<<endl;
	return 0;
}

#include<iostream>
#include<vector>
using namespace std;

/*自我调整表的数组实现*/
/*只在表头插入*/
/*如果表中find元素找到,则把该元素放到表头,其他元素顺序保持不变*/
template <typename Object>
class self_adjust{
public:
	self_adjust():theSize(0),theCapacity(20){
		objects=new Object[theCapacity];
	}
	void push(const Object & x)
	{
		Object *old=objects;
		objects=new Object[theCapacity];
		objects[0]=x;
		for(int i=0;i!=theSize;i++)
			objects[i+1]=old[i];
		delete []old;
		theSize++;
	}
	bool find(const Object & x)
	{
		int i=0;
		while(i!=theSize && objects[i]!=x)
			i++;
		if(i!=theSize)
		{
			Object *old=objects;
			objects=new Object[theCapacity];
			objects[0]=x;
			int j;
			for(j=0;j!=i;j++)
				objects[j+1]=old[j];
			for(j=i+1;j!=theSize;j++)
				objects[j]=old[j];
			return true;
		}
		return false;
	}
	void print()
	{
		for(int i=0;i!=theSize;i++)
			cout<<objects[i]<<" ";
	}
private:
	Object *objects;
	int theSize;/*长度*/
	int theCapacity;/*容量*/
};

int main(){
	self_adjust<int> s;
	for(int i=0;i!=5;i++)
		s.push(i);
	s.print();
	cout<<endl;

	s.find(2);/*2在表中*/
	s.print();
	cout<<endl;
	
	s.find(1);/*1在表中*/
	s.print();
	cout<<endl;
	
	s.find(5);/*5不在表中*/
	s.print();
	cout<<endl;
	return 0;
}

#include<iostream>
using namespace std;

template<typename Object>
struct node{
	node(Object d=0,node *p=NULL):data(d),next(p){}
	Object data;
	node * next;
};

template<typename Object>
class Queue{
public:
	Queue(){
		tail=NULL;
		head=tail;
	}
	void enQueue(const Object &x)
	{
		node<Object> *newNode=new node<Object>(x,NULL);
		if(tail)
			tail=tail->next=newNode;
		else
			head=tail=newNode;
	}
    Object deQueue()
	{
		Object val=head->data;
		node<Object> *ptr=head;
		if(head->next==NULL)
			head=tail=NULL;
		else
			head=head->next;
		delete ptr;
		return val;
	}
private:
	node<Object> * head;
	node<Object> * tail;
};

int main(){
	Queue<int> q;
	for(int i=0;i!=5;i++)
		q.enQueue(i);
	for(int j=0;j!=5;j++)
		cout<<q.deQueue()<<" ";
	cout<<endl;
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值