Stacks, queues and comprehensive application to explain

Disclaimer: This article is a blogger original article, follow the CC 4.0 BY-SA copyright agreement, reproduced, please attach the original source link and this statement.
This link: https://blog.csdn.net/qq_34070495/article/details/102765574

Integrated application stacks and queues

[Define] stack

Stack table is defined linear insertions and deletions in only the trailer, is inserted into the stack push, pop the stack is deleted, as a LIFO structure, where the stack is called a stack.

There are two storage stack, a sequential stack, generally realized by an array, another link stack implemented by a singly linked list pointer, select the sequence of stack array elements as the first stack, link stack pointer location as the selected head stack .

[Queue definition]

1, a queue (Queue) is allowed only in the insertion end and at the other end of the linear table delete operation.
2, contrary to the stack, the queue is a FIFO linear table.
3, the same need to implement a queue or list as a basis sequence table.
(The first node is not essential, but in order to facilitate operation or plus)
Here Insert Picture Description
(empty queues, front and rear head node point.)
When the queue is empty, front and rear head node point.

【Card game】

"Cat fishing" rules of the game is this: the average deck of cards divided into two, each holding one. U1 first player out of the hands of playing cards on the table first, then players U2 also took first in the hands of playing cards, and on top U1 players just play poker, like this two players alternating the cards. When the cards, if someone played the same cards on the table and a card face cards, two cards can be the same cards and the middle of all the clip removed, and in turn put the cards in their hands at the end. When any one of the hands of all the cards play out, the game ends, the match win.

Now we ask you to write an algorithm to simulate the game, and determine who the final winner, winning colleague Print out the winner in the hands of the cards on the table and possible remaining cards.

Before the program began to write, for the time being we do a convention, U1 and U2 in the hands of the player card poker face value of only 1 to 9.

Solution ideas
we can first analyze what kinds of operations the existence of this game. Players U1 operates in two, namely the cards and a winning hand, when the cards to play his cards out on the table a winning hand of cards when the cards into the hands of the end, which happens to correspond to the two operations queue , the brand team is listed team winning hand is the team included team. U1 and U2 players operating the player is the same. And it is clear that the table can be seen as a stack, players did not play on a stop sign on the table, the equivalent of the stack, because the order was back when it was a winning hand when the cards from the table to take the order go, which is equivalent to a stack. How to determine whether a winning hand that it? Winning hand of judgment is this: if someone played cards with a card the same table, to the middle of the two cards and folders Cards all removed. Then how until now what brand it on the table, it is easy to think of cards played after each traverse over existing cards on the table and then compare, there is the same brand is considered a winning hand. This is a simple and a bit brutal methods, in fact, there is a better way, that is with a bucket to solve this problem, the card face value of only 1-9, we can set a size for the array 10 as the bucket, each play a card, to this nominal value cards of the array as the index to find the corresponding position, if the position where the value is, it indicates the presence of the card table has, if no value, then the table is not the same card, while the through-mark, i.e., the array index position is set to 1. If you win that card, and the cards on the table took the bucket should be how to do it? It is also very simple, when a stack of cards in sequence according to the face value of empty buckets on the line. Ultimately how to determine which players get the final victory of it, to get the final victory of the standard is the other hand did not have a license, if viewed from the perspective of the queue, that is, head and tail pointers are equal a.

From the above analysis we can see the idea, in order to simulate the game, we need to prepare two queues, a stack and a barrel, respectively U1U2 hands of the player cards, table cards and table cards of face value. Here we write specific code, for ease of reading, the key code above I have given very detailed notes.

First, create two structures, one for a stack indicates that the queue

struct duilie
{
	int head;
	int tail;
	int data[1000];
};
struct zhan
{
	int top;
	int data[1000];
};

The first node initializing the queue tail node, loop initialization bucket for using
two inputs for each cycle of input six numbers to queue

struct duilie q1,q2;
	struct zhan s;
	int book[10];
	int i,t;
	q1.head=0;
	q1.tail=0;
	q2.head=0;
	q2.tail=0;
	s.top=0;
	for(i=0;i<10;i++)
	{
		book[i]=0;
	}
	for(i=0;i<6;i++)
	{
		scanf("%d",&q1.data[q1.tail]);
		q1.tail++;//每输入一个数字队尾加一
	}
	for(i=0;i<6;i++)
	{
		scanf("%d",&q2.data[q2.tail]);
		q2.tail++;//每输入一个数字队尾加一
	}

Small hum brand

		t=q1.data[q1.head];//将小哼打出的牌存放在临时变量t中
		if(book[t]==0)//如果桶中没有这张牌,表示此回合并没有赢牌
		{	
			q1.head++;//将牌从队列中取出
			s.top++;//将取出的牌放在桌子上,由于桌子代表栈,所以栈顶+1
			s.data[s.top]=t;//将取出的牌放在桌子上			
			book[t]=1;//将桌子上的牌放入桶中,标记为1,证明桌子上存在刚刚打出的这张牌
		}
		else//赢牌
		{
			q1.head++;//将牌从队列中取出
			q1.data[q1.tail]=t;//由于赢牌将刚刚打出的牌放回手中,也就是队尾
			q1.tail++;//队尾+1
			while(s.data[s.top]!=t)//如果桌子上的牌不是刚刚打出的牌,则全部收走
			{
				book[s.data[s.top]]=0;//取消桶中的标记
				q1.data[q1.tail]=s.data[s.top];//讲桌上的牌放入手中
				q1.tail++;//队尾+1			
				s.top--;//由于在桌子上取走牌,所以栈顶-1
			}
			book[s.data[s.top]]=0;//这张牌是t的牌,也就是与刚打出牌相同的牌,取消标记
			q1.data[q1.tail]=s.data[s.top];//取出这张牌,放手里
			q1.tail++;//手里多张牌,队尾+1
			s.top--;//桌上少一张牌,栈顶-1
		}
		if(q1.head==q1.tail)//如果小哈没牌,则结束循环
		{
			break;
		}

Small Hachu brand
empathy

	t=q2.data[q2.head];
		if(book[t]==0)
		{	
			q2.head++;
			s.top++;
			s.data[s.top]=t;	
			book[t]=1;
		}
		else
		{
			q2.head++;
			q2.data[q2.tail]=t;
			q2.tail++;
			while(s.data[s.top]!=t)
			{
				book[s.data[s.top]]=0;
				q2.data[q2.tail]=s.data[s.top];		
				q2.tail++;
				s.top--;
			}
			book[s.data[s.top]]=0;
			q2.data[q2.tail]=s.data[s.top];
			q2.tail++;
			s.top--;
		}
	}

Well not determine if the small card, then Patricia win


	if(q1.head==q1.tail)
	{
		printf("小哈win\n");
		printf("小哈手中的牌是");
		for(i=q2.head;i<q2.tail;i++)//将队列的牌取出
		{
			printf("%d ",q2.data[i]);
		}
		printf("\n");
		if(s.top>0)//如果桌子上还有牌
		{
			printf("桌上还有牌为");
			for(i=1;i<s.top;i++)//将桌子上的牌取出
			{
				printf("%d ",s.data[i]);
			}
		}
		printf("\n");
	}

Patricia did not determine if the card is small hum win

	else
	{
		printf("小哼win\n");
		printf("小哼手中的牌是");
		for(i=q1.head;i<q1.tail;i++)
		{
			printf("%d ",q1.data[i]);
		}
		printf("\n");
		if(s.top>0)
		{
			printf("桌上还有牌为");
			for(i=1;i<s.top;i++)
			{
				printf("%d ",s.data[i]);
			}
		}
		printf("\n");
	}

The complete code

#include<stdio.h>//玩牌游戏
struct duilie
{
	int head;
	int tail;
	int data[1000];
};
struct zhan
{
	int top;
	int data[1000];
};
int main()
{
	struct duilie q1,q2;
	struct zhan s;
	int book[10];
	int i,t;
	q1.head=0;
	q1.tail=0;
	q2.head=0;
	q2.tail=0;
	s.top=0;
	for(i=0;i<10;i++)
	{
		book[i]=0;
	}
	for(i=0;i<6;i++)
	{
		scanf("%d",&q1.data[q1.tail]);
		q1.tail++;
	}
	for(i=0;i<6;i++)
	{
		scanf("%d",&q2.data[q2.tail]);
		q2.tail++;
	}
	while(q1.head<q1.tail&&q2.head<q2.tail)
	{
		t=q1.data[q1.head];
		if(book[t]==0)
		{	
			q1.head++;
			s.top++;
			s.data[s.top]=t;			
			book[t]=1;
		}
		else
		{
			q1.head++;
			q1.data[q1.tail]=t;
			q1.tail++;
			while(s.data[s.top]!=t)
			{
				book[s.data[s.top]]=0;
				q1.data[q1.tail]=s.data[s.top];
				q1.tail++;		
				s.top--;
			}
			book[s.data[s.top]]=0;
			q1.data[q1.tail]=s.data[s.top];
			q1.tail++;
			s.top--;
		}
		if(q1.head==q1.tail)
		{
			break;
		}

		t=q2.data[q2.head];
		if(book[t]==0)
		{	
			q2.head++;
			s.top++;
			s.data[s.top]=t;	
			book[t]=1;
		}
		else
		{
			q2.head++;
			q2.data[q2.tail]=t;
			q2.tail++;
			while(s.data[s.top]!=t)
			{
				book[s.data[s.top]]=0;
				q2.data[q2.tail]=s.data[s.top];		
				q2.tail++;
				s.top--;
			}
			book[s.data[s.top]]=0;
			q2.data[q2.tail]=s.data[s.top];
			q2.tail++;
			s.top--;
		}
	}


	if(q1.head==q1.tail)
	{
		printf("小哈win\n");
		printf("小哈手中的牌是");
		for(i=q2.head;i<q2.tail;i++)
		{
			printf("%d ",q2.data[i]);
		}
		printf("\n");
		if(s.top>0)
		{
			printf("桌上还有牌为");
			for(i=1;i<s.top;i++)
			{
				printf("%d ",s.data[i]);
			}
		}
		printf("\n");
	}
	else
	{
		printf("小哼win\n");
		printf("小哼手中的牌是");
		for(i=q1.head;i<q1.tail;i++)
		{
			printf("%d ",q1.data[i]);
		}
		printf("\n");
		if(s.top>0)
		{
			printf("桌上还有牌为");
			for(i=1;i<s.top;i++)
			{
				printf("%d ",s.data[i]);
			}
		}
		printf("\n");
	}
	return 0;
}

Guess you like

Origin blog.csdn.net/qq_34070495/article/details/102765574