代码改变世界

2012-07-28 15:01  javaspring  阅读(292)  评论(0编辑  收藏  举报
//十进制数值M转换成m进制
void SysConvert(int N,int m)  
{
	while(N!=0)
	{
		push(p,N%m);   //将余数进栈
		N=N/m;
	}
}


1、栈的基本操作

// lb.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#include "stdlib.h"
#include <iostream>
#include <string>
using namespace std;

//定义栈的节点数据结构
template<class T>
struct TLinkNode{
	T data;
	TLinkNode *next;
};

//定义栈的基本操作类模版
template<class T> class TStack
{
public:
	TStack();
	~TStack();
	void Push(T val);
	T Pop();
	T GetTop();
	bool isEmpty();
	int lenStack();

private:
	TLinkNode<T> *top;		
};

//构造函数
template<class T>TStack<T>::TStack()
{
	top=NULL;
}


//栈是否为空
template<class T>bool TStack<T>::isEmpty()
{
	return top==NULL;
}

//栈的长度
template<class T>int TStack<T>::lenStack()
{
	int len=0;
    TLinkNode<T> *pTemp=top;
    while(pTemp!=NULL)
	{
		len++;
		pTemp=pTemp->next;
	}

	return len;
}

//进栈操作push
template<class T>void TStack<T>::Push(T val)
{
	typedef TLinkNode<T> *pNode;
	pNode pNew=(pNode)malloc(sizeof(TLinkNode<T>));
	pNew->data=val;
	pNew->next=NULL;

	if (top==NULL)
	{
		top=pNew;
	}
	else
	{
		pNew->next=top;
		top=pNew;
	}
}

//出栈操作pop
template<class T>T TStack<T>::Pop()
{
	if (isEmpty())
	{
		cout<<"栈为空,程序退出"<<endl;
		exit(-1);
	}
    typedef TLinkNode<T> *pNode;
	pNode pTemp=top;
	T val=pTemp->data;
	top=top->next;
	free(pTemp);
	pTemp=NULL;

	return val;
}

//取得栈顶元素
template<class T>T TStack<T>::GetTop()
{
	T val=top->data;

	return val;
}

//清空整个栈
template<class T>TStack<T>::~TStack()
{
	if(isEmpty())
		return;

	TLinkNode<T> *pTemp;
	while(top!=NULL)
	{
		pTemp=top;
		top=top->next;
		free(pTemp);
		pTemp=NULL;
	}
}


//test栈
int main(int argc, char* argv[])
{
	int data[]={34,678,90,345,78,3546,980,5476,23,89,56879};
	int len=sizeof(data)/sizeof(int);

	TStack<int> *s=new TStack<int>;

	cout<<"数组依次元素进栈,得到栈顶内容为:";
	for (int i=0;i<len;i++)
	{
		s->Push(data[i]);
		cout<<s->GetTop()<<" ";
	}
	cout<<endl;
	cout<<"此时栈的长度:"<<s->lenStack()<<endl;

	cout<<"数组依次元素出栈,得到内容为:    ";
	for (i=0;i<len;i++)
	{
		cout<<s->Pop()<<" ";
	}
	cout<<endl;

	cout<<"此时栈的长度:"<<s->lenStack()<<endl;
	return 0;
}

2、利用栈进行进制转换

//十进制数值M转换成m进制
void SysConvert(int N,int m)  
{
	while(N!=0)
	{
		push(p,N%m);   //将余数进栈
		N=N/m;
	}
}

3、迷宫程序

#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
#include "string.h"

//迷宫矩阵,2代表墙壁,0代表通道
int maze[7][7] = {{2, 2, 2, 2, 2, 2, 2}, 
{2, 0, 0, 0, 0, 0, 2}, 
{2, 0, 2, 0, 2, 0, 2}, 
{2, 0, 0, 2, 0, 2, 2}, 
{2, 2, 0, 2, 0, 2, 2}, 
{2, 0, 0, 0, 0, 0, 2}, 
{2, 2, 2, 2, 2, 2, 2}}; 

int startI = 1, startJ = 1;  // 入口
int endI = 5, endJ = 5;      // 出口
int success = 0;             //是否走成功

typedef struct Queue
{
	int x;
	int y;
	Queue *next;
	Queue *prevoius;
}*pQueue;

pQueue front=NULL;
pQueue rear=NULL;

void enQueue(int x,int y)    //双向队列
{
	pQueue p=(pQueue)malloc(sizeof(Queue));
	p->x=x;
	p->y=y;
	p->next=NULL;
	if (front==NULL)
	{
		front=(pQueue)malloc(sizeof(Queue));
		rear=front;
		front->next=p;
		p->prevoius=front;
	} 
	rear->next=p;
	p->prevoius=rear;
	rear=p;
}

void deQueue()     //从队尾移除元素
{
	pQueue p=rear;
	rear=p->prevoius;
	free(p);
	p=NULL;
}

void printQ()    //打印出整个队列元素
{
	if (front==NULL)
		return;
	do 
	{
		front=front->next;
		cout<<"("<<front->x<<","<<front->y<<")"<<" ";
	} while (front!=rear);

	cout<<endl;
}
int visit(int i, int j) 
{ 
	maze[i][j] = 1;             //该点走过,标记为1
	enQueue(i,j);               //进入队列
	
	if(i == endI && j == endJ)  //走到终点,成功,递归终止条件
		success = 1; 
		
	//向四个方向递归调用函数visit()
	if(success != 1 && maze[i][j+1] == 0) visit(i, j+1); 
    if(success != 1 && maze[i+1][j] == 0) visit(i+1, j); 
    if(success != 1 && maze[i][j-1] == 0) visit(i, j-1); 
    if(success != 1 && maze[i-1][j] == 0) visit(i-1, j); 
	
	//该点走过,但没成功,则该点重新置为0
	if(success != 1) 
	{
		maze[i][j] = 0; //该点走过,但没成功
		deQueue();      //从队尾移除该点
	}
    
    return success; 
}  

int main(void) 
{ 
    int i, j; 
	
    printf("显示迷宫:\n"); 
    for(i = 0; i < 7; i++)
	{ 
        for(j = 0; j < 7; j++) 
		{
			if(maze[i][j] == 2) 
                printf("█"); 
            else 
                printf("  "); 
        }
		printf("\n"); 
    } 
	
    if(visit(startI, startJ) == 0)
	{
		printf("\n没有找到出口!\n"); 
    }
	else 
	{ 
        printf("\n显示路径:\n"); 
        for(i = 0; i < 7; i++) 
		{ 
            for(j = 0; j < 7; j++)
			{ 
                if(maze[i][j] == 2) 
                    printf("█"); 
                else if(maze[i][j] == 1) 
                    printf("◇"); 
                else 
                    printf("  "); 
            } 
            printf("\n"); 
        } 
    } 
	
	printQ();  //打印迷宫路线坐标
    return 0; 
}