Пример #1
0
int main(void)
{
	//创建栈并且进行初始化
	Stack* ps = create(5);
	
	printf("%s\n",full(ps)?"栈满了":"栈没有满"); // 栈没有满
	printf("%s\n",empty(ps)?"栈空了":"栈没有空"); // 栈空了

	printf("-----------------------\n");
	int i = 0;
	for(i = 1; i < 7; i++)
	{
		push(ps,i*10+i); //元素66入栈失败
	}
	travel(ps); // 11 22 33 44 55 
	printf("出栈的元素是:%d\n",pop(ps));//55
	travel(ps);// 11 22 33 44 
	printf("出栈的元素是:%d\n",pop(ps));// 44
	travel(ps);// 11 22 33 

	printf("-------------------\n");
	printf("栈顶元素值是:%d\n",peek(ps));// 33
	printf("栈中元素的个数是:%d\n",size(ps)); // 3
	printf("出栈的元素是:%d\n",pop(ps));// 33
	printf("栈顶元素值是:%d\n",peek(ps));// 22
	printf("栈中元素的个数是:%d\n",size(ps)); // 2
	travel(ps); // 11 22


	//销毁栈,数值传递 而不是址传递
	destroy(ps);
	ps = NULL;
	return 0;
}
Пример #2
0
int main()
{
	//创建队列并且初始化
	Queue queue;
	queue.head = NULL;
	queue.tail = NULL;

	push(&queue, 12);
	push(&queue, 22);
	push(&queue, 32);
	push(&queue, 42);
	push(&queue, 52);
	push(&queue, 62);
	push(&queue, 72);
	push(&queue, 82);
	push(&queue, 92);
	printf("--------------------\n");	
	travel(&queue);
	printf("--------------------\n");	
	printf("出队的元素是: %d\n", pop(&queue));
	printf("%s\n", empty(&queue) ? "队列空" : "队列没空");
	printf("%s\n", full(&queue) ? "队列满" : "队列没满");
	printf("队列中节点元素个数 :%d\n", size(&queue));
	travel(&queue);
	printf("--------------------\n");	
	printf("队首元素: %d\n", get_head(&queue));
	printf("队尾元素: %d\n", get_tail(&queue));
	printf("--------------------\n");	
	clean(&queue);
	travel(&queue);

	return 0;
}
Пример #3
0
struct TreeNode* lowestCommonAncestor(struct TreeNode* root, struct TreeNode* p, struct TreeNode* q) {
	struct TreeNode* ancestorP[MAX] ;
	struct TreeNode* ancestorQ[MAX] ;

	int countP = 0 ;
	int countQ = 0 ;

	flag = true ;
	travel(root,p,&countP) ; 
	for(int i=0;i<=countP;i++){
		ancestorP[i] = ancestor[i] ;
	} 

	flag = true ;

	travel(root,q,&countQ) ;
	for(int i=0;i<=countQ;i++){
		ancestorQ[i] = ancestor[i] ;
	}
	
	for(int i=countP;i>=0;i--){
		for(int j=countQ;j>=0;j--){
			if(ancestorP[i]==ancestorQ[j]){
				return ancestorP[i];
			}
		}
		
	}
	return NULL ;
}
Пример #4
0
int main()
{
	//创建栈以及进行初始化
	Stack stack;
	stack.top = NULL;
	stack.cnt = 0;
	
	push(&stack, 12);
	travel(&stack);
	
	push(&stack, 24);
	travel(&stack);
	
	printf("%s\n", full(&stack) ? "满" : "没满");
	printf("%s\n", empty(&stack) ? "空" : "没空");

	printf("栈顶的元素是: %d\n", peek(&stack)); //bug

	printf("栈中元素个数: %d\n", size(&stack));

	printf("出栈的元素是: %d", pop(&stack)); //bug

	clear(&stack);

	return 0;
}
Пример #5
0
 void travel(TreeNode * root)
 {
     if(!root) return;
     travel(root->left);
     ret.push_back(root->val);
     travel(root->right);
 }
Пример #6
0
//1 travel
void travel(tree** root)
{
   if((*root)==NULL)return;
   printf("%d\n",(*root)->data);
   travel(&((*root)->left));
   travel(&((*root)->right));
}
Пример #7
0
void travel(int m, int na){
	int i,sum=0;
	if(m == 1){
		putchar('X');
		return;
	}
	for(i=0; sum<na; i++){
		sum += a[i]*a[m-1-i];
	}
	i--;
	sum -= a[i]*a[m-1-i];
	na -= sum;
	
	if(i>0){
		putchar('(');
		travel(i,(na-1)/a[m-1-i]+1);
		putchar(')');
	}
	putchar('X');
	
	if(m-1-i>0){
		putchar('(');
		travel(m-1-i,(na-1)%a[m-1-i]+1);
		putchar(')');
	}
}
Пример #8
0
int main()
{
	node* headptr;//保存第一个节点的地址
	createlist(&headptr);
	insert(&headptr, 10, 2);//无效位置,会更正成末尾
	insert(&headptr, 20, 0);//20,10
	insert(&headptr, 30, 2);//20,10,30
	insert(&headptr, 40, 1);//20,40,10,30
	insert(&headptr, 50, 8);//无效位置,会更正成末尾
	travel(headptr);
	printf("剩余%d个数据\n", listsize(headptr));
	int n = find(headptr, 10);
	printf("查找10在第%d个节点\n", n);
	erase(&headptr, n);
	travel(headptr);
	n = find(headptr, 10);
	printf("查找10在第%d个节点\n", n);
	erase(&headptr, n);
	travel(headptr);
	setdata(headptr, 10, 100);
	setdata(headptr, 2, 99);
	travel(headptr);
	clear(&headptr);
	printf("剩余%d个数据\n", listsize(headptr));
	return 0;
}
Пример #9
0
int main(void){
	//freopen("data.in", "r", stdin);
	int n;
	scanf("%d", &n);
	int** lab=(int**)malloc(n*sizeof(int*));
	int** track=(int**)malloc(n*sizeof(int*));
	for(int i=0; i<n; i++){
		lab[i]=(int*)malloc(n*sizeof(int));
		track[i]=(int*)malloc(n*sizeof(int));
		memset(lab[i], 0, n*sizeof(int));
		memset(track[i], 0, n*sizeof(int));
	}
	char* string=(char*)malloc((n+1)*sizeof(char));
	for(int i=0; i<n; i++){
		scanf("%s", string);
		for(int j=0; j<n; j++){
			char temp;
			sscanf(&string[j], "%c", &temp);
			lab[i][j]=(temp=='.')?1:0;
		}
	}
	travel(0,0, lab, track, n);
	if(!flag){
		travel(n-1,n-1, lab, track, n);
	}
	printf("%d\n", (numb-4)*9);
	return 0;
}
Пример #10
0
void travel(int i, int j, int** lab, int** track, int n){
	track[i][j]=1;
	if(i==n-1 && j==n-1){
		flag=true;
	}
	
	if(i==0 || lab[i-1][j]==0){
		numb++;
	}
	else if(track[i-1][j]==0){
		travel(i-1, j, lab, track, n);
	}

	if(j==n-1 || lab[i][j+1]==0){
		numb++;
	}
	else if(track[i][j+1]!=1){
		travel(i, j+1, lab, track, n);
	}

	if(i==n-1 || lab[i+1][j]==0){
		numb++;
	}
	else if(track[i+1][j]!=1){
		travel(i+1, j, lab, track, n);
	}

	if(j==0 || lab[i][j-1]==0){
		numb++;
	}
	else if(track[i][j-1]!=1){
		travel(i, j-1, lab, track, n);
	}
}
 int travel(TreeNode *root, int num) {
     if (root == NULL)
         return 0;
     if (root->left == NULL && root->right == NULL) {
         return num * 10 + root->val;
     }
     int val = root->val;
     return travel(root->left, num * 10 + val) + travel(root->right, num * 10 + val);
 }
Пример #12
0
void travel(link p)
{
	if (p == NULL)
		return;

	printf("%c: counter = %d\n", p->item, p->counter);
	travel(p->l);
	travel(p->r);
}
Пример #13
0
void RBTree::travel(Node *root)
{
	if(root != NIL)
	{
		travel(root->left);
		printf("addr:%x value:%d color:%s parent:%x left:%x right%x\n",
			root, root->value, root->color==BLACK?"BLACK":"RED", root->parent, root->left, root->right);
		travel(root->right);
	}
}
Пример #14
0
    void travel ( TreeNode * root , int current_sum , int sum){
	    if (root == NULL ) return ;
	    if ( flag ) return;
	    if ( ((root->right) == NULL) && ( (root->left) == NULL )){
		    if ( sum == (current_sum+root->val) ) flag = true;
		    return;
	    }
	    travel( root->right, current_sum + root->val, sum);
    	    travel( root->left, current_sum + root->val, sum);
    }
 void travel(TreeNode* root, int level, vector<vector<int>> &res )
 {
     if(!root)
         return;
     if(level > res.size())
         res.push_back(vector<int>());
     res[level-1].push_back(root->val);
     travel(root->left, left+1, res);
     travel(root->right, left+1, res);
 }
 void travel(TreeLinkNode* root, int level, vector<vector<TreeLinkNode*> > &nodes){
     if(root==NULL) return;
     
     if(level>nodes.size())
       nodes.push_back(vector<TreeLinkNode*>());
       
      nodes[level-1].push_back(root);
      travel(root->left, level+1, nodes);
      travel(root->right, level+1, nodes);
 }
Пример #17
0
void travel(struct node* haff, int &hafflen, int h)    // travel a haffman tree and get the haffman code lenth
{
	h++;//h represents depth of a node
	if(haff->value != 0){
		haff->codelen = h-1;
		hafflen+=haff->fre*haff->codelen;
		return;
	}
	travel(haff->lc, hafflen, h);
	travel(haff->rc, hafflen, h);
}
Пример #18
0
void ferry_loop()
{
	/*
	Start, check if the south flag is checked, if so
	we want to skip doing north and just go straight to south.
	Once we get to south we want to check if the flag was set
	and if so we want to redeposit passengers before we pick up again.

	The same goes for the second loop, we check to see if the north flag
	was checked, if so we want to skip the south and go straight back 
	to north and pick up the queen there. 

	If the flag is set and we return to a bank that we were travelling away
	from, we want to re-deposit the passengers currently on the ferry back
	onto the shore, then reload them in the correct order (queen first)
	*/

	if(queen_south_flag!=1)
	{
		if(queen_north_flag==1)
		{
			queen_north_flag=0;
			//redeposit passengers
			redeposit_north();
		}
		arrive_north();
		//Set bank to transit
		curr_ferry.curr_bank = 2;
		print_proc();
		travel();
		//We reset the south flag as to not deposit passengers that wanted
		//To go to south in the first place, then they get mixed up with north
		//Passengers and go back to where they came from
		queen_south_flag=0;
	}

	//if north has queen waiting skip south and go north
	if(queen_north_flag!=1)
	{
		//if south has queen we are already here so just reset flag
		if(queen_south_flag==1)
		{
			queen_south_flag=0;
			//reposit passengers
			redeposit_south();
		}
		arrive_south();
		//Set bank to transit
		curr_ferry.curr_bank = 2;
		print_proc();
		travel();
		queen_north_flag=0;
	}
}
Пример #19
0
//遍历的递归函数
void travel(Node* pn)
{
	if(pn != NULL)
	{
		//1.遍历左子树,采用递归
		travel(pn->left);
		//2.遍历根节点
		printf("%d ",pn->data);
		//3.遍历右子树,采用递归
		travel(pn->right);
	}
}
Пример #20
0
int main(int argc, const char *argv[])
{
    int i;
    node_t *head = malloc(sizeof(node_t));
    node_t *p = head;
    head->id = 1;
    for(i = 1; i < 10; i++)
        p = create(p, i + 1);
    travel(head);
    head = tailtohead(head);
    travel(head);
    return 0;
}
Пример #21
0
int main(int argc, const char *argv[])
{
    loop *head = NULL,*p;
    int i;
    srand((unsigned)time(NULL));
    head = create(head,rand()%1000);
    for(i = 1;i < 100;i++)
        p = create((i-1)?p:head,rand()%1000);
    travel(head);
    putchar('\n');
    travel(sequence(head));
    return 0;
}
Пример #22
0
int main(void)
{
	btree_pnode t;

	create_btree(&t);

	travel("先序遍历序列为:",pre_order,t);
	travel("先序非递归遍历序列为:",pre_order_un,t);
	travel("中序遍历序列为:",mid_order,t);
	travel("后序遍历序列为:",post_order,t);
	travel("按层遍历序列为:",level_order,t);
	return 0;
}
 void travel(TreeNode *root, int &prev) {
     if (!(ok && root)) return;
     if (root->left) travel(root->left, prev);
     int mid = root->val;
     if (!first) {
         if (mid <= prev) ok = false;
         prev = mid;
     } else {
         first = false;
         prev = mid;
     }
     if (root->right) travel(root->right, prev);
 }
Пример #24
0
int main()
{
	lp p = (lp)NULL;
	p=initialize(p);
	p = insert(p, 4);

	p = insert(p, 6);
	p = insert(p, 5);
	travel(p);
	p = del(p, 6);
	travel(p);
	p = del(p, 5);
	travel(p);
	return 0;
}
Пример #25
0
 void travel(TreeNode* root, const std::string &path) {
     ostringstream oss;
     // is leaf
     if (root->left == nullptr && root->right == nullptr) {
         oss << path << root->val;
         allPaths.push_back(oss.str());
         return;
     }
     oss << path << root->val << "->";
     
     if (root->left != nullptr) 
         travel (root->left, oss.str());
     if (root->right != nullptr)
         travel (root->right, oss.str());
 }
Пример #26
0
    void travel(TreeNode *root, int sum, int cur, vector<vector<int> > &res, vector<int> rcd) {
        if (!root) return;
        else {
		    cur += root->val;
		    rcd.push_back(root->val);
            if (cur == sum) {
                if (!root->left && !root->right) {
                    res.push_back(rcd);
                    return;
                }  
    	    }
			travel(root->left, sum, cur, res, rcd);
			travel(root->right, sum, cur, res, rcd);
	    }
    }
Пример #27
0
int main(int argc, char **argv) {
	int t, count = 0, i, j, k;
	S(t);
	while(t--){
		S(n), S(m);
		for(i=0;i<n;n++){
			scanf("%s", &forest[i]);
			for(j=0;j<m;i++){
				if(forest[i][j] == 'M'){
					hermione.x = i, hermione.y = j;
				}else if(forest[i][j] == '*'){
					ext.x = i, ext.y = j;
				}
			}
		}
		S(k);
		count = travel();
		if(count == k){
			printf("Impressed\n");
		}else{
			printf("Oops!\n");
		}
	}
	return 0;
}
Пример #28
0
int main(){
	
	int m, i, n;
	
	int *t = (int *)malloc(2 * sizeof(int));
	
	a = (int *)malloc(2 * sizeof(int));
	a[0] = 1, a[1] = 1;
	t[0] = 0, t[1] = 1;
	
	while(scanf("%d",&n) != EOF && n != 0){
		m = 1;
		while(t[m] < n){
			//printf("还未达到n\n");
			m++;
			a = (int *)realloc(a, (m+1)*sizeof(int));
			t = (int *)realloc(t, (m+1)*sizeof(int));
			a[m] = 0;
			for(i=0; i<=m-1; i++){
				a[m] += a[i] * a[m-1-i];
			}
			t[m] = t[m-1] + a[m];
			//printf("[a%d]%d\t[t%d]%d\n",m,a[m],m,t[m]);
		}
		//printf("[%d]%d\n",m,n-t[m-1]);
		travel(m,n-t[m-1]);
		printf("\n");
	}
	free(a);
	free(t);
}
Пример #29
0
int main(void)
{
	struct score tmp, *data;
	int i;

	srand(getpid());

	init();

	for (i = 0; i < 1024 * 1024; i++) {
		tmp.id = i;
		tmp.ch = 100 - i;
		//snprintf(tmp.name, NAMESIZE, "stu%d", i);
		rand_name(tmp.name, NAMESIZE);

		insert(&tmp);
	}

	//data = search("stu6");
	//printf("%d %s %d\n", data->id, data->name, data->ch);

	travel();

	return 0;
}
Пример #30
0
int main(void) {
	//Warning: This recursive implementation is VERY slow for travel(20,20)
	//For smaller grids it works better, and is an "elegant" solution
	long long count = travel(20,20);
	printf("%lld", count);
	return 0;
}