int main() { List L; int x,p; L = Create(); scanf("%d", &x); //输入要查找的关键字 Sort(L); p = BinarySearch(L,x); if(p != -1){ printf("所查找数据的位置为:%d",p); }else{ printf("NotFound"); } return 0; }
时间: 2024-01-24 08:02:29 浏览: 18
这是一个主函数的代码段,用于测试 `Create`、`Sort` 和 `BinarySearch` 函数的功能。首先声明一个指向 `LNode` 的指针类型变量 `L`,然后调用 `Create` 函数创建一个新的线性表,并将其赋值给 `L`。然后从标准输入中读取一个整数 `x`,表示要查找的关键字。
接着调用 `Sort` 函数对线性表进行排序,然后调用 `BinarySearch` 函数在线性表中查找关键字 `x` 的位置,并将结果存储到变量 `p` 中。
最后,根据 `p` 的值输出查找结果,如果找到了则输出关键字的位置,否则输出 "NotFound"。
需要注意的是,`Create` 函数中使用了 `malloc()` 函数分配了内存空间,需要在程序结束时使用 `free()` 函数释放这些内存空间,避免内存泄漏的问题。
相关问题
#include<stdio.h> #include<malloc.h> //申请空间 typedef int ElemType; typedef int Status; typedef struct Lnode { ElemType data; struct Lnode *next; }Lnode, *LinkList; void createList(LinkList L,int n); void print(LinkList L); void release(LinkList L); int main(void) { int n; LinkList L; L=NULL; printf("how many data do you want to input:"); scanf("%d",&n); createList(L,n); print(L); release(L); return 0; } void createList(LinkList L, int n) { L->next = NULL; // 初始化链表为空 int i; for (i = n; i > 0; i--) { Lnode *p = (Lnode*)malloc(sizeof(Lnode)); // 动态申请节点 scanf("%d", &p->data); // 输入节点数据 p->next = L->next; // 将新节点插入链表头部 L->next = p; } } void print(LinkList L) { Lnode *p = L->next; while (p != NULL) { printf("%d ", p->data); p = p->next; } printf("\n"); } void release(LinkList L) { Lnode *p; while(L!=NULL) { p=L; L=L->next; free(p); }//释放空间时指针所指地址很重要 }找出这段代码的错误
这段代码中的错误在于在 `createList` 函数中,没有分配头节点的内存空间,导致程序会崩溃。正确的代码应该是在 `main` 函数中先分配头节点的内存空间,然后将头节点传入 `createList` 函数中进行链表的创建。修改后的代码如下:
```
#include<stdio.h>
#include<malloc.h>
typedef int ElemType;
typedef int Status;
typedef struct Lnode
{
ElemType data;
struct Lnode *next;
}Lnode, *LinkList;
void createList(LinkList L, int n);
void print(LinkList L);
void release(LinkList L);
int main(void)
{
int n;
LinkList L = (Lnode*)malloc(sizeof(Lnode)); // 分配头节点的内存空间
L->next = NULL; // 初始化链表为空
printf("how many data do you want to input:");
scanf("%d", &n);
createList(L, n);
print(L);
release(L);
return 0;
}
void createList(LinkList L, int n)
{
int i;
for (i = n; i > 0; i--)
{
Lnode *p = (Lnode*)malloc(sizeof(Lnode));
scanf("%d", &p->data);
p->next = L->next;
L->next = p;
}
}
void print(LinkList L)
{
Lnode *p = L->next;
while (p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
void release(LinkList L)
{
Lnode *p;
while (L != NULL)
{
p = L;
L = L->next;
free(p);
}
}
```
本题要求实现两个函数:(1)排序;(2)二分查找 先利用排序算法将数据按关键字从小到大排序,再对排序后的数据进行二分查找 函数接口定义: void Sort(List L); //对用户传入的线性表进行排序 int BinarySearch( List L,int X ); //查找X,如果查找成功返回对应的数组下标位置;查找失败,则返回NotFound 其中List结构定义如下: struct LNode{ int Data[MAXSIZE]; //Data为待排序序列数组 int Last; //Last为最后一个元素的数组下标 }; typedef struct LNode *List; 裁判测试程序样例: #include <stdio.h> #include <stdlib.h> #define MAXSIZE 10 #define NotFound -1 struct LNode{ int Data[MAXSIZE]; //Data为待排序序列数组 int Last; //Last为最后一个元素的数组下标 }; typedef struct LNode *List; void Sort(List L); int BinarySearch( List L,int X ); List Create() { List L; L = (List)malloc(sizeof(struct LNode)); int j = 0,n,ch; scanf("%d",&n); for(j=0;j<n;j++) { scanf("%d",&ch); L->Data[j] = ch; } L->Last = n-1; return L; } int main() { List L; int x,p; L = Create(); scanf("%d", &x); //输入要查找的关键字 Sort(L); p = BinarySearch(L,x); if(p != -1){ printf("所查找数据的位置为:%d",p); }else{ printf("NotFound"); } return 0; } /* 请在这里填写答案 */ 输入样例1: 8 99 66 45 33 37 10 22 13 20 输出样例1: NotFound 输入样例2: 8 99 66 45 33 37 10 22 13 13 输出样例2: 所查找数据的位置为:1
```
void Sort(List L) {
int i, j, temp;
for (i = 0; i < L->Last; i++) {
for (j = i + 1; j <= L->Last; j++) {
if (L->Data[i] > L->Data[j]) {
temp = L->Data[i];
L->Data[i] = L->Data[j];
L->Data[j] = temp;
}
}
}
}
int BinarySearch(List L, int X) {
int left = 0, right = L->Last, mid;
while (left <= right) {
mid = (left + right) / 2;
if (L->Data[mid] == X) {
return mid;
} else if (L->Data[mid] > X) {
right = mid - 1;
} else {
left = mid + 1;
}
}
return NotFound;
}
```
相关推荐
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![docx](https://img-home.csdnimg.cn/images/20210720083331.png)
![doc](https://img-home.csdnimg.cn/images/20210720083327.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)