1、最快的查找方式是:二分法查找。
十堰郧阳网站建设公司创新互联公司,十堰郧阳网站设计制作,有大型网站制作公司丰富经验。已为十堰郧阳成百上千提供企业网站建设服务。企业网站搭建\成都外贸网站建设要多少钱,请找那个售后服务好的十堰郧阳做网站的公司定做!
2、查找的线性表分:无序线性表、有序线性表、分块有序线性表。
3、对无序线性表只能采用顺序查找,顺序查找的平均比较次数为(n+1)/2
4、对有序线性表可以采用二分查找,二分查找的比较次数为log2n
5、对分块有序线性表可以采用分块法查找。
C语言是一种计算机程序设计语言,它既具有高级语言的特点,又具有汇编语言的特点。它由美国贝尔研究所的D.M.Ritchie于1972年推出,1978年后,C语言已先后被移植到大、中、小及微型机上,它可以作为工作系统设计语言,编写系统应用程序,也可以作为应用程序设计语言,编写不依赖计算机硬件的应用程序。它的应用范围广泛,具备很强的数据处理能力,不仅仅是在软件开发上,而且各类科研都需要用到C语言,适于编写系统软件,三维,二维图形和动画,具体应用比如单片机以及嵌入式系统开发。
表述是没有错,但是
b.elem[5]={1,2,3,4,5};
赋值能这么赋值么······,虽然elem是b的元素,但是也是个数组,数组我记得是不能这么赋值的········
其他的就没发现有啥问题了····
typedefstruct_List{intdata;struct_List*next;}List;intQuery(List**head,intx){List*p=(*head)-next;intn=1;while(pp-data!=x){p=p-next;n++;}if(p)returnn;elsereturn-1;}
#includefstream.h
#includeconio.h
#includeiostream.h
#includeiomanip.h
#includestring.h
#includestdlib.h
struct friend_node//结构体类型定义,包括:姓,名,电话号码
{
char last_name[20];
char first_name[20];
char phone_num[12];
friend_node * next;
};
friend_node *head_ptr;//全程变量,链头指针
friend_node *current_ptr;//全程变量,指明位置
void handle_choice(int choice);
void add_record();
void insert_node(friend_node *new_rec_ptr);
friend_node *position_insertion_point(char lastname[20]);
void make_node_new_head(friend_node *new_rec_ptr);
void add_node_to_end(friend_node *new_rec_ptr);
void move_current_to_end();
void display_list();
void delete_record();
void delete_head_of_list();
void delete_end_of_list(friend_node *previous_ptr);
void delete_from_middle_of_list(friend_node *previous_ptr);
int verify_delete();
void delete_node(friend_node *previous_ptr);
void delete_list();
void search_by_lastname();
void write_list_to_file();
void load_list_from_file();
//void help_me();
char pause;
//main function
int main()
{
cout"欢迎使用mmc电话簿程序\nPress Enter To Continue\n";
cin.get(pause);
system("cls");
int choice;
head_ptr=NULL;
do
{
cout"1-新增\n";
cout"2-显示所有记录\n";
cout"3-通过姓来查找记录\n";
cout"4-删除记录\n";
cout"5-帮助\n";
cout"6-退出\n";
cinchoice;
handle_choice(choice);
} while(choice !=6);
return 0;
}
void handle_choice(int choice)
{
switch(choice)
{
case 1:
add_record();
break;
case 2:
display_list();
break;
case 3:
search_by_lastname();
break;
case 4:
delete_record();
break;
case 5:
cout"mmc制作\n这个程序的主要功能为:\n对电话簿的管理\n通过此程序可以实现对新增,显示,删除记录等功能\n";
break;
case 6:
write_list_to_file();
if(head_ptr !=NULL)
{
delete_list();
}
break;
default:
cout"Invalid choice\n";
break;
}
}
void add_record()
{
friend_node * new_rec_ptr;
new_rec_ptr = new friend_node;
if(new_rec_ptr !=NULL)
{
system("cls");
cin.ignore(20,'\n');
cout"First Name:";
cin.get(new_rec_ptr-first_name,15);
cin.ignore(20,'\n');
cout"last name:";
cin.get(new_rec_ptr-last_name,20);
cin.ignore(20,'\n');
cout"Phone Number: ";
cin.get(new_rec_ptr-phone_num,15);
cin.ignore(20,'\n');
insert_node(new_rec_ptr);
}
else //
{
cout"WARNING:错误,无法增加\n";
}system("cls");
}//end
//
void insert_node(friend_node * new_rec_ptr)
{
system("cls");
friend_node * before_ptr;
friend_node * after_ptr;
if(head_ptr==NULL)
{
new_rec_ptr-next=NULL;
head_ptr=new_rec_ptr;
}
else
{
if(strcmp(new_rec_ptr-last_name,head_ptr-last_name)0)
{
make_node_new_head(new_rec_ptr);
}
else
{
current_ptr=position_insertion_point(new_rec_ptr-last_name);
before_ptr=current_ptr;
after_ptr=current_ptr-next;
if(after_ptr==NULL)
{
add_node_to_end(new_rec_ptr);
}
else
{
before_ptr-next=new_rec_ptr;
new_rec_ptr-next=after_ptr;
}
}
}
}
friend_node * position_insertion_point(char lastname[20])
{//根据姓氏返回正确位置,新节点插入此点
char temp_name[20];
friend_node* temp_ptr;
int tempint;
if(head_ptr-next!=NULL)
{
current_ptr=head_ptr;
temp_ptr=current_ptr-next;
strcpy(temp_name,temp_ptr-last_name);
tempint=strcmp(lastname,temp_name);
while((tempint0)(current_ptr-next!=NULL))
{
current_ptr=temp_ptr;
if(current_ptr-next!=NULL)
{
temp_ptr=current_ptr-next;
strcpy(temp_name,temp_ptr-last_name);
tempint=strcmp(lastname,temp_name);
}
}
}
else
{
current_ptr=head_ptr;
}
return(current_ptr);
}
void make_node_new_head(friend_node * new_rec_ptr)
{
friend_node * temp_ptr;
temp_ptr=head_ptr;
new_rec_ptr-next=temp_ptr;
head_ptr=new_rec_ptr;
}
void add_node_to_end(friend_node *new_rec_ptr)
{
new_rec_ptr-next=NULL;
move_current_to_end();
current_ptr-next=new_rec_ptr;
} //
void move_current_to_end()
{
current_ptr=head_ptr;
while(current_ptr-next!=NULL)
{
current_ptr=current_ptr-next;
}
} //
//
void display_list()
{
char fullname[36];
current_ptr=head_ptr;
if(current_ptr!=NULL)
{
coutendl;
cout"Name Phone Number\n";
cout"---------------------------------------------------------\n";
do
{
strcpy(fullname," ");//
strcat(fullname,current_ptr-last_name);
strcat(fullname,", ");
strcat(fullname,current_ptr-first_name);
cout.setf(ios::left);
coutsetw(36)fullname;
cout.unsetf(ios::left);
cout.setf(ios::right);
coutsetw(12)current_ptr-phone_numendl;
current_ptr=current_ptr-next;
coutendl;
}while(current_ptr!=NULL);
cout"Press Enter to continue\n";
cin.get(pause);
cin.ignore(1,pause);
system("cls");}
else
{
cout"\n无记录显示\n";
}
}
void search_by_lastname() //根据姓名查询
{
system("cls");
char search_string[20];
current_ptr=head_ptr;
cin.ignore(20,'\n');
cout"\nEnter the last name for which you want to search:";
cin.get(search_string,20);
cin.ignore(20,'\n');
while((current_ptr!=NULL)
(strcmp(current_ptr-last_name,search_string)!=0))
{
current_ptr=current_ptr-next;
}
if(current_ptr!=NULL)
{
cout"\n记录找到\n";
coutcurrent_ptr-first_namecurrent_ptr-last_nameendl;
coutcurrent_ptr-phone_numendl;
}
else
{
cout"没有匹配记录\n";
cout"PRESS ENDTER TO CONTIUNE\n";
cin.get(pause);
system("cls");
}
} //
//
void delete_record()
{
system("cls");
char search_string[20];
friend_node *previous_ptr;
previous_ptr=NULL;
current_ptr=head_ptr;
cin.ignore(20,'\n');
cout"\nEnter the last name of the friend you want to delete:";
cin.get(search_string,20);
cin.ignore(20,'\n');
//
while((current_ptr!=NULL)(strcmp(current_ptr-last_name,search_string)!=0))
{
previous_ptr=current_ptr;
current_ptr=current_ptr-next;
}
if(current_ptr!=NULL)
{
cout"\nRECORD FOUND\n";
coutcurrent_ptr-first_namecurrent_ptr-last_nameendl;
coutcurrent_ptr-phone_numendl;
if(verify_delete())
{
delete_node(previous_ptr);
cout"\nRECORD DELETED\n";
}
else
{
cout"\nRECORD NOT DELETED\n";
}
}
else
{
cout"\nNO MATCH FOUND.NO RECORD DELETED.\n";
}
system("cls");
} //
int verify_delete()//确认删除
{
char YesNo;
cout"\nAre you sure?(Y/N)";
cinYesNo;
if((YesNo=='Y')||(YesNo=='y'))
{
return(1);
}
else
{
return(0);
}
}
void delete_node(friend_node *previous_ptr)
{
if(current_ptr==head_ptr)
{
delete_head_of_list();
}
else
{
if(current_ptr-next==NULL)
{
delete_end_of_list(previous_ptr);
}
else
{
delete_from_middle_of_list(previous_ptr);
}
}
}
void delete_head_of_list()
{
current_ptr=head_ptr;
if(head_ptr-next!=NULL)
{
head_ptr=current_ptr-next;
}
else
{
head_ptr=NULL;
}
delete current_ptr;
}
void delete_end_of_list(friend_node * previous_ptr)
{
delete current_ptr;
previous_ptr-next=NULL;
current_ptr=head_ptr;
}
void delete_from_middle_of_list(friend_node * previous_ptr)
{
previous_ptr-next=current_ptr-next;
delete current_ptr-next;
current_ptr=head_ptr;
}
void delete_list()
{
friend_node * temp_ptr;
current_ptr=head_ptr;
do
{
temp_ptr=current_ptr-next;
delete current_ptr;
current_ptr=temp_ptr;
}while(temp_ptr!=NULL);
}
void write_list_to_file()
{
ofstream outfile;
outfile.open("FRIENDS.DAT",ios::out);
if(outfile)
{
current_ptr=head_ptr;
if(head_ptr!=NULL)
{
do
{
outfilecurrent_ptr-last_nameendl;
outfilecurrent_ptr-first_nameendl;
outfilecurrent_ptr-phone_numendl;
current_ptr=current_ptr-next;
}
while(current_ptr!=NULL);
outfile"END OF FILE"endl;
outfile.close();
}
}
}
/*
std::ifstream infile;
infile.open("in.txt");
if (!infile)
return -1; //invail file
char token;
int index = 0;
while (infile)
{
infile token
node_array[index].power token
node_array[index].coeff token;
++index;
}
infile.close();
*/
#include stdio.h
#include string.h
typedef struct student {
char name[30];
unsigned matrno;
}student;
student participants[] = {
{"Jonh Doe",111},
{"Bob Baumei",139},
{"Jane Smith",140},
{"Jack Archer",140},
{"Jonhnny Mahony",167},
{"Simo Anthony",170},
{"Marylin Anderson",174},
{"Melanie Thomson",181},
{"Sydney Core",183},
{"Steve Smith",186},
};
int noFstud = 10;
int linSearchByName(char name[]) {
int i;
for(i = 0; i noFstud; ++i) {
if(strcmp(name,participants[i].name) == 0)
return i;
}
return -1;
}
int main() {
char name[30];
int res;
printf("Input a name:");
res = linSearchByName(name);
if(res == -1) printf("no find.\n");
else printf("%s %u\n",participants[res].name,participants[res].matrno);
return 0;
}
#include"stdio.h"
#define N 10
#define Three 5
int ReadName(char name[][Three],long num[]); /*Readname()函数原型*/
int LinSearch(long num[],long x,int n); /*LinSeach()函数原型*/
int main()
{
char name[N][Three]; /*定义二维字符数组*/
int n,pos;
long num[N],x;
n=ReadName(name,num); /*输入教师姓名和工号*/
printf("Total teachers are %d\n",n);
printf("Input the searching ID");
scanf("%d",x); /*以整型格式从键盘输入待查找的工号*/
pos=LinSearch(num,x,n); /*查找工号为num的教师*/
if(pos!=-1)
printf("name = %s\n",name[pos]); /*若找到打印姓名*/
else
printf("Not found!\n"); /*若未找到,则打印未找到提示信息*/
return 0;
}
/*函数功能:输入教师的工号和姓名,当输入负值时,结束输入,返回教师人数*/
int ReadName(char name[][Three],long num[]) /*函数定义*/
{
int i=-1;
do{
i++;
printf("Input teacher's ID and name:");
scanf("%d %s",num+i,name[i]);//参数错误.
}while (num[i]0); /*输入负值时结束输入*/
return i;
}
/*按线性查找法查找值为x的数组元素,若找到则返回x在数组中的下标位置,否则返回-1*/
int LinSearch(long num[],long x,int n) /*函数定义*/
{
int i;
for (i=0;in;i++)
{
if(num[i]==x)
return i; /*若找到则返回x在数组中的下标*/
}
return -1; /*若循环结束任然未找到,则返回-1*/
}