데이터 구조의 순서 스 택 SqStack

10644 단어 교과서 학습
순차 스 택 SqStack
기본 조작
Status InitStack()//      S
Status DestroyStack()//   S,S    
Status ClearStack()// S    
Status StackEmpty()// S   ,   true,    false
int StackLength()//  S     ,     
Status GetTop(SElemType &e)//    ,  e  S     ,   OK,    ERROR
Status Push(SElemType e)//    e       
Status Pop(SElemType &e)//    ,   S     ,  e    ,   OK,    ERROR
Status StackTraverse(int p)//                   visit().  visit()       

몇 개의 작은 프로그램 (코드 오류 검사, 디지털 변환, 괄호 일치 검사, 줄 편집 프로그램)
CheckStackCode();//  Stack     
Conversion();//    
BracketsMatching();//       
LineEdit();//     
//
//by coolxxx
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#define min(a,b) ((a)(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 104

using namespace std;
typedef long long LL;
/*
double anss;
LL aans,sum;
int cas,cass;
int n,m,lll,ans;
*/


const int OK=1;
const int ERROR=0;
const int INFEASIBLE=-1;
const int STACK_INIT_SIZE=100;//         
const int STACKINCREMENT=10;//        
typedef int Status;
typedef int SElemType;

Status OutputInt(SElemType e);
Status OutputChar(SElemType e);
typedef struct
{
	SElemType *base;//          ,base   NULL
	SElemType *top;//    
	int stacksize;//          ,      
	
	Status InitStack()//      S
	{
		base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
		if(!base)exit(OVERFLOW);//      
		top=base;
		stacksize=STACK_INIT_SIZE;
		return OK;
	}//InitStack
	
	Status DestroyStack()//   S,S    
	{
		free(base);
		base=NULL;
		top=NULL;
		stacksize=0;
		return OK;
	}//DestroyStack
	
	Status ClearStack()// S    
	{
		top=base;
		return OK;
	}//ClearStack
	
	Status StackEmpty()// S   ,   true,    false
	{
		if(top==base)return true;
		return false;
	}//StackEmpty
	
	int StackLength()//  S     ,     
	{
		return top-base;
	}//StackLength
	
	Status GetTop(SElemType &e)//    ,  e  S     ,   OK,    ERROR
	{
		if(top==base)return ERROR;
		e=*(top-1);
		return OK;
	}//GetTop
	
	Status Push(SElemType e)//    e       
	{
		if(top-base>=stacksize)//  ,      
		{
			base=(SElemType *)realloc(base,(stacksize+STACKINCREMENT)*sizeof(SElemType));
			if(!base)exit(OVERFLOW);//      
			top=base+stacksize;
			stacksize+=STACKINCREMENT;
		}
		(*top++)=e;
		return OK;
	}//Push
	
	Status Pop(SElemType &e)//    ,   S     ,  e    ,   OK,    ERROR
	{
		if(top==base)return ERROR;
		e=(*--top);
		return OK;
	}//Pop
	
	Status StackTraverse(int p)//                   visit().  visit()       
	{
		SElemType *i=base;
		Status (*visit)(SElemType);
		if(p==1)visit=OutputInt;
		else if(p==0)visit=OutputChar;
		while(top>i)
			visit(*i++);
		puts("");
		return OK;
	}//StackTraverse
}SqStack;

Status OutputInt(SElemType e)
{
	printf("%d ",e);
	return OK;
}
Status OutputChar(SElemType e)
{
	printf("%c",e);
	return OK;
}


void CheckStackCode()
{
	typedef int SElemType;
	int i;
	SqStack S;
	SElemType e;
	if(S.InitStack()==OK)
	{
		for(i=1;i<=12;i++)
			S.Push(i);
	}
	printf("       :");
	S.StackTraverse(1);
	S.Pop(e);
	printf("        e=%d
",e); printf(" :%d(1: 0: )
",S.StackEmpty()); S.GetTop(e); printf(" e=%d %d
",e,S.StackLength()); S.Push(13); printf(" :"); S.StackTraverse(1); S.GetTop(e); printf(" e=%d %d
",e,S.StackLength()); S.DestroyStack(); printf(" ,S.top=%d S.base=%d S.stacksize=%d
",S.top,S.base,S.stacksize); } void Conversion()// n, { typedef int SElemType; int n; SqStack S; SElemType e; S.InitStack();// scanf("%d",&n); while(n) { S.Push(n%8); n/=8; } while(!S.StackEmpty()) { S.Pop(e); printf("%d",e); } puts(""); S.DestroyStack(); } void BracketsMatching()// ()[]{}, { char s[N]; SqStack S; SElemType e; int i; S.InitStack(); scanf("%s",s); for(i=0;i

말 보드게임 문제 (NxN)
폭력 (순차 스 택 구현)
//
//by coolxxx
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#define min(a,b) ((a)(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 8
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
LL n,m,lll,ans;
int a[N][N];
bool u[N][N];
int dx[]={-2,-1,1,2,2,1,-1,-2};
int dy[]={1,2,2,1,-1,-2,-2,-1};

const int OK=1;
const int ERROR=0;
const int INFEASIBLE=-1;
const int STACK_INIT_SIZE=10000;//         
const int STACKINCREMENT=10000;//        
typedef int Status;

typedef struct
{
	int x,y,dir;
}SElemType;

Status OutputInt(SElemType e);
Status OutputChar(SElemType e);
typedef struct
{
	SElemType *base;//          ,base   NULL
	SElemType *top;//    
	int stacksize;//          ,      
	
	Status InitStack()//      S
	{
		base=(SElemType *)malloc(STACK_INIT_SIZE*sizeof(SElemType));
		if(!base)exit(OVERFLOW);//      
		top=base;
		stacksize=STACK_INIT_SIZE;
		return OK;
	}//InitStack
	
	Status DestroyStack()//   S,S    
	{
		free(base);
		base=NULL;
		top=NULL;
		stacksize=0;
		return OK;
	}//DestroyStack
	
	Status ClearStack()// S    
	{
		top=base;
		return OK;
	}//ClearStack
	
	Status StackEmpty()// S   ,   true,    false
	{
		if(top==base)return true;
		return false;
	}//StackEmpty
	
	int StackLength()//  S     ,     
	{
		return top-base;
	}//StackLength
	
	Status GetTop(SElemType &e)//    ,  e  S     ,   OK,    ERROR
	{
		if(top==base)return ERROR;
		e=*(top-1);
		return OK;
	}//GetTop
	
	Status Push(SElemType e)//    e       
	{
		if(top-base>=stacksize)//  ,      
		{
			base=(SElemType *)realloc(base,(stacksize+STACKINCREMENT)*sizeof(SElemType));
			if(!base)exit(OVERFLOW);//      
			top=base+stacksize;
			stacksize+=STACKINCREMENT;
		}
		(*top++)=e;
		return OK;
	}//Push
	
	Status Pop(SElemType &e)//    ,   S     ,  e    ,   OK,    ERROR
	{
		if(top==base)return ERROR;
		e=(*--top);
		return OK;
	}//Pop
	
	Status StackTraverse(int p)//                   visit().  visit()       
	{
		SElemType *i=base;
		Status (*visit)(SElemType);
		if(p==1)visit=OutputInt;
		else if(p==0)visit=OutputChar;
		while(top>i)
			visit(*i++);
		puts("");
		return OK;
	}//StackTraverse
}SqStack;
Status OutputInt(SElemType e)
{
	printf("%d ",e);
	return OK;
}
Status OutputChar(SElemType e)
{
	printf("%c",e);
	return OK;
}

SqStack S;
void print()
{
	int i,j;
	for(i=0;i=0 && xx=0 && yy

말 이 바둑판 을 밟 는 것 은 욕심 이 최적화 되 었 다.
// 
//by coolxxx
//#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
//#include
#include
#define min(a,b) ((a)(b)?(a):(b))
#define abs(a) ((a)>0?(a):(-(a)))
#define lowbit(a) (a&(-a))
#define sqr(a) ((a)*(a))
#define swap(a,b) ((a)^=(b),(b)^=(a),(a)^=(b))
#define mem(a,b) memset(a,b,sizeof(a))
#define eps (1e-10)
#define J 10000
#define mod 1000000007
#define MAX 0x7f7f7f7f
#define PI 3.14159265358979323
#pragma comment(linker,"/STACK:1024000000,1024000000")
#define N 8
using namespace std;
typedef long long LL;
double anss;
LL aans;
int cas,cass;
LL n,m,lll,ans;
int a[N][N];
bool u[N][N];
int dx[]={-2,-1,1,2,2,1,-1,-2};
int dy[]={1,2,2,1,-1,-2,-2,-1};
struct xxx
{
	int c,num;
};
bool cmp(xxx aa,xxx bb)
{
	return aa.c=0 && xx=0 && yy=0 && x1=0 && y1=0 && xx=0 && yy

빨리 쓰기 위해 서 마음대로 최적화 하 는 것 은 별로 생각 하지 않 았 다. 만약 나의 알고리즘 복잡 도가 너무 높다 고 생각한다 면 돌 릴 수 있다.http://blog.csdn.net/bone_ace/article/details/41579957

좋은 웹페이지 즐겨찾기