#include <iostream>
#include <stdio.h>
#include <cstdio>
#include <string>
#include <string.h>
#include <math.h>
#include <algorithm>
#include <stack>
using namespace std;
#define maxSize 101
/*
//写出折半查找的递归算法 王道P242.6
typedef struct Sqlist
{
int data[maxSize];
int length;
}Sqlist;
int BinarySearch(Sqlist s,int low,int high,int k)
{
while(low<=high) //一定要带等号
{
int mid=low+(high-low)/2;
if(s.data[mid]==k)
{
return mid;
}
else if(s.data[mid]<k)
{
low=mid+1;
}
else if(s.data[mid]>k)
{
high=mid-1;
}
}
return -1;
}
int main()
{
int n,t;
cin>>n;
Sqlist s;
s.length=n;
for(int i=0;i<n;i++)
{
cin>>t;
s.data[i]=t;
}
int d=BinarySearch(s,0,n-1,5);
cout<<d<<endl;
}
8
0 1 3 4 5 6 7 8
4
*/
//找到查找节点,则把查找到的节点和他的前驱交换位置 王道P242.7
/*
//线性表形式
typedef struct Sqlist
{
int data[maxSize];
int length;
}Sqlist;
void SqSearch(Sqlist &s,int k) //记得加引用号,除了数组在被改变的时候不用加引用号,其他的都需要
{
int i=0;
while(i<s.length)
{
if(s.data[i]==k)
break;
else
i++;
}
int t=s.data[i];
s.data[i]=s.data[i-1];
s.data[i-1]=t;
}
int main()
{
Sqlist s;
int n,t;
cin>>n;
s.length=n;
for(int i=0;i<n;i++)
{
cin>>t;
s.data[i]=t;
}
SqSearch(s,5);
for(int i=0;i<s.length;i++)
{
cout<<s.data[i]<<" ";
}
}
8
0 1 3 4 5 6 7 8
0 1 3 5 4 6 7 8
*/
/*
//链表形式
typedef struct LNode
{
int data;
struct LNode *next;
}LNode;
void LSearch(LNode *&s,int k)
{
LNode *pre=s;
LNode *p=s->next;
while(p)
{
if(p->data==k)
{
int t=p->data;
p->data=pre->data;
pre->data=t;
break;
}
else
{
p=p->next;
pre=pre->next;
}
}
}
int main()
{
LNode *s=(LNode*)malloc(sizeof(LNode));
s->next=NULL;
LNode *q=s;
int n,t;
cin>>n;
for(int i=0;i<n;i++)
{
cin>>t;
LNode *p=(LNode*)malloc(sizeof(LNode));
p->data=t;
p->next=NULL;
q->next=p;
q=p;
}
LSearch(s,5);
while(s->next)
{
cout<<s->next->data<<" ";
s=s->next;
}
}
8
0 1 3 4 5 6 7 8
0 1 3 5 4 6 7 8
*/