69,370
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Number//单链表结构体结点
{
int a;
struct Number *next;
};
int main()
{
/*
函数声明,Insert()是插入函数,Delete()是删除函数,Research()是查询函数
InsertSort()是插入排序函数
*/
void Insert(struct Number *first,int x);
void Delete(struct Number *first,int x);
void Research(int *arr,int n,int x,struct Number *first);
void InsertSort(int *arr,int n);
int n,x;
char ch;
int i;
struct Number *first=(struct Number *)malloc(sizeof(struct Number));//头结点,初始化头结点
first->a=0;
first->next=NULL;
while(scanf("%d",&n)!=EOF)
{
int *arr=(int *)malloc(sizeof(int)*(n+1));//用一个数组来放与x异或的值,初始化
memset(arr,0,sizeof(int)*(n+1));
for(i=0;i<n;i++)
{
scanf(" %c %d",&ch,&x);
switch(ch)
{
case '+': Insert(first,x);break;
case '-': Delete(first,x);break;
case '?': Research(arr,n,x,first);break;
}
}
printf("%d\n",arr[1]);
}
}
void Insert(struct Number *first,int x)
{
struct Number *p=first;
while(p!=NULL)
{
p=p->next;
}
struct Number *s=(struct Number *)malloc(sizeof(struct Number));
s->a=x;
p=s;
s->next=NULL;
}
void Delete(struct Number *first,int x)
{
struct Number *q=first;
struct Number *p=q->next;
while(p!=NULL)
{
if(p->a==x)
{
q->next=p->next;
free(p);
return;
}
q=q->next;
p=p->next;
}
}
void Research(int *arr,int n,int x,struct Number *first)
{
InsertSort(int *arr,int n);
int i=1;
struct Number *p=first;
while(p!=NULL)
{
arr[i]=p->a^x;//与链表中的每个数异或求最大值
p=p->next;
i++;
}
InsertSort(arr,n);//排序,是数组第一个数为最大值
}
void InsertSort(int *arr,int n)
{
int i,j;
for(i=2;i<=n;i++)
{
arr[0]=arr[i];
for(j=i-1;arr[j]<arr[0];j--)
arr[j+1]=arr[j];
arr[j+1]=arr[0];
}
}
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
struct Number
{
int a;
struct Number *next;
};
/*
函数声明,Insert()是插入函数,Delete()是删除函数,Research()是查询函数
InsertSort()是插入排序函数
*/
void Insert(struct Number *first,int x);
void Delete(struct Number *first,int x);
void Research(int *arr,int n,int x,struct Number *first);
void InsertSort(int *arr,int n);
int main()
{
int n,x;
char ch;
int i;
struct Number *first=(struct Number *)malloc(sizeof(struct Number));//头结点,初始化头结点
first->a=0;
first->next=NULL;
while(scanf("%d",&n)!=EOF)
{
int *arr=(int *)malloc(sizeof(int)*(n+1));//用一个数组来放与x异或的值,初始化
memset(arr,0,sizeof(int)*(n+1));
for(i=0;i<n;i++)
{
scanf(" %c %d",&ch,&x);
switch(ch)
{
case '+': Insert(first,x);break;
case '-': Delete(first,x);break;
case '?': Research(arr,n,x,first);break;
}
}
printf("%d\n",arr[1]);
}
}
void Insert(struct Number *first,int x)
{
struct Number *p=first;
while(p!=NULL)
{
p=p->next;
}
struct Number *s=(struct Number *)malloc(sizeof(struct Number));
s->a=x;
p=s;
s->next=NULL;
}
void Delete(struct Number *first,int x)
{
struct Number *q=first;
struct Number *p=q->next;
while(p!=NULL)
{
if(p->a==x)
{
q->next=p->next;
free(p);
return;
}
q=q->next;
p=p->next;
}
}
void Research(int *arr,int n,int x,struct Number *first)
{
//InsertSort(int *arr,int n);
int i=1;
struct Number *p=first;
while(p!=NULL)
{
arr[i]=p->a^x;//与链表中的每个数异或求最大值
p=p->next;
i++;
}
InsertSort(arr,n);//排序,是数组第一个数为最大值
}
void InsertSort(int *arr, int n)
{
int i,j;
for(i=2;i<=n;i++)
{
arr[0]=arr[i];
for(j=i-1;arr[j]<arr[0];j--)
arr[j+1]=arr[j];
arr[j+1]=arr[0];
}
}
参考一下吧
函数声明建议放在函数外,不建议放在main函数内,这样只会让main函数逻辑更乱。
若被调用函数在调用函数之下,建议在调用函数外,前面提前声明一下即可。