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

#include
#include
using namespace std;

template 
void printLots(const list & L,const list & P)
{
	list::const_iterator iter1=L.begin();
	list::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 L(a,a+8);
	list P(b,b+4);
	printLots(L,P);
	cout< 
  


/*交换单链表结点,不是简单的数据交换*/
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
#include
#include
#include
using namespace std;
/*由后缀式求结果*/
double evalPostFix(const string & str)
{
	char space=' ';
	string::const_iterator ptr=str.begin();
	string::const_iterator trailer;
	stack 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< 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<


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

#include
using namespace std;

template 
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<<"数组空间已满!"< s(10);
	s.push1(1);

	for(int i=1;i!=10;i++)
		s.push2(i);
	cout<

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

#include
#include
using namespace std;

template
class Deque{
public:
	Deque(){l=list();}
	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 l;
};

int main(){
	Deque d;
	d.push(1);
	d.push(2);
	cout< 
  
#include
#include
using namespace std;

/*自我调整表的数组实现*/
/*只在表头插入*/
/*如果表中find元素找到,则把该元素放到表头,其他元素顺序保持不变*/
template 
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< s;
	for(int i=0;i!=5;i++)
		s.push(i);
	s.print();
	cout<

#include
using namespace std;

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

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

int main(){
	Queue q;
	for(int i=0;i!=5;i++)
		q.enQueue(i);
	for(int j=0;j!=5;j++)
		cout< 
  

你可能感兴趣的:(数据结构,算法,C++,object,token,iterator,string,delete,null)