#import "GSTDViewController.h"

#import "AllViewController.h"
@interface GSTDViewController ()

@end

@implementation GSTDViewController
@synthesize myTextField;
- (void)viewDidLoad
{
[super viewDidLoad];
head=0;tail=0; //初始化为空队列。
counta=0;
}





#pragma mark=======显示所有的方法。============
- (IBAction)showAll:(id)sender {
NSMutableString *tempStr=[[NSMutableString alloc]init];
[tempStr appendFormat:@"head==%d,,tail===%d\n",head,tail];
//输出队列的元素。
printf("\n下面是整个表的元素显示:\n");
for (int i=head; i<tail; ++i) {
printf("a[%d]=%d\n",i%MAX1,a[i%MAX1]);
[tempStr appendFormat:@" a[%d]=%d ,",i%MAX1,a[i%MAX1]];
}
printf("head==%d,,tail===%d",head,tail);

AllViewController *all=[[AllViewController alloc]initWithNibName:@"AllViewController" bundle:nil];
[self presentViewController:all animated:YES completion:^{
all.myTextView.text=tempStr;
}];




}
#pragma mark-=====判断队列是否为空的方法======
- (int)isEmpty:(id)sender {
if (tail-head==0) {
self.label_IsEmpty.text=@"空";
return 1;
}
self.label_IsEmpty.text=@"不为空";
return 0;
}
#pragma mark======判断队列的长度的方法。======
- (int)length:(id)sender {
self.label_Length.text=[NSString stringWithFormat:@"%d",tail-head];
return tail-head;
}
#pragma mark======返回队列头的方法==========
- (int)head:(id)sender {
int isEmpty=tail-head;
if (isEmpty==0) {
[self alertEmpty];
return 0;
}
else{
self.label_Head.text=[NSString stringWithFormat:@" %d",a[head]];
return a[head%MAX1];
}

}
#pragma mark======删除队列头的方法==========
- (int)delete_head:(id)sender {
int isEmpty=tail-head;
if (isEmpty==0) {
[self alertEmpty];
return 0;
}
else{
a[head%MAX1]=0;
head++;
self.label_Delete_Head.text=@"成功";
return 1;
}

}

#pragma mark--====添加到队列尾部的方法=======下面三个方法需要用户输入值。=============
- (void)insert_tail:(id)sender {
[self insetValue];//调用输入数据的弹出框。
flag=1;
}
#pragma mark======判断元素是否在队列中的方法。=====
- (int)isIn:(id)sender {
int isEmpty=tail-head;
if (isEmpty==0) {
[self alertEmpty];
return 0;
}
else{
[self insetValue];//调用输入数据的弹出框。
flag=3;
}
return 1;
}
#pragma mark======删除某个值的方法=============
- (int)delete_value:(id)sender {
int isEmpty=tail-head;
if (isEmpty==0) {
[self alertEmpty];
return 0;
}
else{

[self insetValue];//调用输入数据的弹出框。
flag=2;
return 0;
}
}





//需要用户输入值的时候用到的方法。
-(void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
if (buttonIndex==1) {
self.myTextField.text=self.myTextField.text;
}
else{
self.myTextField.text=@"";
}
NSLog(@"myTextField.text====%@",myTextField.text);

if (flag==1) {
[self insertTotail];
}
if (flag==2) {
[self deleteValue];
}
if (flag==3) {
[self isIn];
}

}
//用户输入数值后添加到队列尾的方法。
-(int)insertTotail{
int tempLength=tail-head;
if ([self.myTextField.text intValue]==0) {
self.label_Insert_tail.text=@"失败";
return 0;
}
if (tempLength<MAX1) {
a[tail%MAX1]=[self.myTextField.text intValue];
tail++;
}
else{
a[tail%MAX1]=[self.myTextField.text intValue];
tail++;
head++;
}
self.label_Insert_tail.text=@"成功";
return 1;
}
//用户输入数值后删除某个值的方法。
-(int)deleteValue{
int mylength=tail-head;
int tempa=0;
for (int i=0; i<mylength; ++i) {
if (a[(head+i)%MAX1]==[self.myTextField.text intValue]) {
if (i<mylength/2) {//把前半部分往后移动。
for (int j=i; j>0; --j) {
a[(head+j)%MAX1]=a[(head+j-1)%MAX1];
}
a[head%MAX1]=0;//把移动后的第一个元素赋值为0;
head++;
}
else{//把后半部分往前移动。
for (int j=i; j<mylength; ++j) {
a[(head+j)%MAX1]=a[(head+j+1)%MAX1];
}
a[tail%MAX1]=0;//把移动后的第一个元素赋值为0;
tail--;
}
self.label_Delete_value.text=@"成功";

tempa=1;
counta++;
}
}
if (tempa==1) {//如果tempa==1则证明至少找到过一次。
counta=1;
[self deleteValue];
}
else if(counta==0){//只要是为了判断有多个相同的值的情况。
self.label_Delete_value.text=@"没有找到元素";
}

return 0;
}
//用户输入数值后判断该数值是否在方法。
-(int)isIn{
int mylength=tail-head;
for (int i=0; i<mylength; ++i) {
if (a[(head+i)%MAX1]==[self.myTextField.text intValue]) {
self.label_IsIn.text=@"元素在队列中";
return 1;
}
}
self.label_IsIn.text=@"元素不在队列中";
return 0;
}


//弹出队列为空的提示。
-(void)alertEmpty{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"对不起" message:@"队列为空" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil, nil];
[alert show];
}
//弹出输入数据的提示框。
-(void)insetValue{
UIAlertView *alert=[[UIAlertView alloc]initWithTitle:@"请输入一个数字" message:nil delegate:self cancelButtonTitle:@"取消" otherButtonTitles:@"确定", nil];
self.myTextField = [[UITextField alloc]
initWithFrame:CGRectMake(12, 45, 260, 25)];
[myTextField setBackgroundColor:[UIColor whiteColor]];
[alert addSubview:myTextField];
[myTextField release];
[alert show];
}