设顺序表va中的数据元数递增有序。试写一算法,将x插入到顺序表的适当位置上,以保持该表的有序性。
void insert(btnode va,int x){
int sign=0;
int num=0;
int sign1=1;
while(sign1){
num++;
if(va[num]>va[num+1]){sign1=0;break;}
}
int i;
for(i=0;i<num;i++){
if(x<va[i]){sign=1;break;}
if(sign==1){
for(int j=num;j>i;j--){
va[j]=va[j-1];
va[i]=x;
void Insert_x(SeqList va[],int n, int x){
if(n > MAXSIZE -1) return ERROR;
for(int i = n - 1 ; i >= 0; i--){
if(va[i] > x){
va[i+1] = va[i];
else
break;
va[i+1] = x;
n++;
void insert(List l[], ElemType x) { int n = length(l); int pos; for(int i = 0; i < n; i ++) if(l[i] > x) { pos = i; break; } for(int i = n; i > pos; i --) l[i] = l[i - 1]; l[pos] = x; }
#define Maxsize 100;
void Insert_x(Sqlist *L,int x){
if(L->length >= Maxsize)
return;
int i,k; //i用来遍历顺序表,k用来寻找合适的插入位置。
for(i=0;i<L->length;i++){
if(L->data[i] > x){
k = i;
for(i=length-1;i<k;i--){
L->data[i+1] = L->data[i];
L->data[k] = x;
L->length++;
void insertsort(SqList *va, ElemType x) { ElemType loc=va->length-1; while(loc>=0&&va[loc]>x) { va[loc+1]=va[loc] loc--; } voc[loc]=x; va->length++; }
void func(List *L,int x){
for(int i=0;i<L->length;i++){
for(int j=L->length;j>i;j--){
L->data[j]=L->data[j-1];
L->data[i]=x;
先找到插入位置,再将后续数据后移
void Insert_sq(Sqlist va[], ElemType x)
{int i, j, n;
n=length(va[]);
if(x>=va[n-1])
va[n]=x;
{i=0;
while(x>va[i]) i++;
for(j=n-1;j>=I;j--)
va[j+1]=va[j];
va[i]=x; }
#include<stdio.h>
#define maxsize 100
typedef struct {
int data[maxsize];
int length;
} list;
void youxu(list *p,int n) {
if(p->length>maxsize) {
printf("表满\n");
int i=0;
while(i<p->length&&p->data[i]<n)
i++;
for(int j=p->length; j>i; j--)
p->data[j]=p->data[j-1];
p->data[i]=n;
p->length++;
void print(list *p) {
for(int i=0; i<p->length; i++)
printf("%d ",p->data[i]);
int main()
{
list a= {{2,5,8},3};
printf("插入前顺序:");
print(&a);
printf("\n");
int n;
scanf("%d",&n);
printf("插入后顺序:");
youxu(&a,n);
return 0;
#define V 20 int main() { int arr[21]; int key = 0; scanf_s("%d", &key); int arr1[V] = { 1,2,3,4,5,6,7,8,9,11,12,13,14,15,16,17,18,19,20,21}; for (int i = 0; i < V; i++) { arr[i] = INT_MAX; arr[i] = arr1[i]; } for (int i = 1; i < V; i++) { if (arr[i] > key && arr[i - 1] < key) { int j = 0; for ( j = V; j >= i; j--) { arr[j + 1] = arr[j]; } arr[j + 1] = key; } } for (int i = 0; i < V+1; i++) { printf("%d ", arr[i]); } return 0; }
#define MaxSize 100
typedef struct{
int data[MaxSize];
}Sqlist;
void ListInsert(Sqlist &va, int x){
for(i=0;i<va.length,i++)
if(va.data[i]>x)
for(j=va.length,j>i,j--)
va.data[j]=va.data[j-1];
va.data[i]=x;
va.length++;
void insert(Node* L,Element x ){
Node* p=L;
Node* q =malloc(sizeof(Node));
q->data = x;
while(x<p->data){
if(p->next==NULL){
break;}
p=p->next;
q->next = p->next;
p->next = q;
找到插入位置,将后续结点右移
void insert_sq(Sqlist &A,ElementType x){ int l=0,r=length-1; while(l<r){ int mid=(l+r)/2; if(A[mid]<x){ l=mid+1; } else r=mid-1; } for(int i=length-1;i>=r;i--){ A[i+1]=A[i]; } A[r]=x; return; }
int i,j;
for(i=0;i<va->length;i++)
if(va[i]<=x&&va[i+1]>x)break;
for(j=va->length-1;j>i;j--)
va[i+1]=x;
嘤狐 回复 嘤狐: 补充一句va->length++;
vbb
void Inser_sq(Sqlist va[], Elemtype x) { int i, j, n; n = length(va[]); if (x > va[n - 1]) va[n] = x; else { while (x > va[i]) i++; for (j = n - 1; j >= i; j--) va[j + 1] = va[j]; va[i] = x; } n++; }
typedef struct LNode{ int data; struct LNode *next; }LNode, * LList; void insert(LList &l, int x){ LNode *p; p->data = x; LNode *q = l; //case1:比最小的元素还小 if(q->data > x){ p->next = q; } //case2:插入在链表中间 while(q != NULL){ q = q->next; if(q->next > x){ p->next = q->next; q->next = p; break; } } //case3:比最大的元素还大 q->next = p; }
void insert(int va[],int x){ for(int i =length(va);i>=0;i--){ if(va[i-1]>x){ f[i] = f[i-1]; }else{ f[i] = x; break; } } }
void insert(ElementType va, ElementType[] a, int *length){
int i = 0;
for(; i < length; i ++){
if(va < a[i]) break;
if(i == length){
a[i] = va;
length ++;
}else{
for(int j = length - 1; j > i; j --){
a[j] = a[j - 1];
typedef struct{ int data[MAXSIZE]; int length; }SqList; void Test(SqList **va,int x){ SqList *p=va; for (int i = 0; i < p->length; ++i) { if(p->data[i]>=x){ for (int j = p->length; j >i; j--) { p->data[j]=p->data[j-1] } p->data[i]=x; break; } } }
void Insert_sq(Sqlist *va, ElemType x){ int i, j; if(va->length+1>MAXSIZE) { printf("存储空间已满"); return; } for(i=va->length-1;va->data[i]>x&&i>=0;i--) va->data[i+1]=va->data[i]; va->data[i+1]=x; va->length++; }
int ListInsert(Sqlist &L, int x)
for(i=0;i<l.length;i++)
if(i->data>x)
return false
for(j=l.length;j>=i+1;j++)
l.data[j]=l.data[j-1]
l.data[i]=e;
l.length++;
void Insert( SeqList *va,int x)
while(i<va->n-1&&va->data[i]<x) i++;
for(j=n-1;j>=i;j--)
va->data[j+1]=va->data[j];
va->data[i] = x;
va->n++;
woaixinzhou 回复 woaixinzhou: SqList va
int insert(SqList a[],int p){
int i,temp,n;
n=length(a);
a[n]=p;
for(i=n;i<0;i--){
if(a[i]<a[i-1]){
temp=a[i];
a[i]=a[i-1];
a[i-1]=temp;
}else
N诺的题有很多问题,va[i]=x;应该写在for循坏外
void InX(Sqlist &va int n)
int x=n;
if(x>va.elem[va.length]) va.elem[va.length+1]=x;
while(x<va.elem[i])
for(int j=va.length;j<=i;j--)
va.elem[j+1]=va.elem[j];
va.elem[i]=x;
void Insert_sq(Sqlist va[], ElemType x){ int i, j, n; n=length(va[]); if(x>=va[n-1]) va[n]=x; else{ i=0; while(x>va[i]) i++; for(j=n-1;j>=I;j--) va[j+1]=va[j]; va[i]=x; } n++; }
insert(Sqlist* va,int x){
i=0;
while(i<va->length&&va.data[i]<x){
while(i<va->length+1){
int tem=va->data[i];
va->data[i]=x;
length++;
void insertSorted(int arr[], int size, int x) { int i = size - 1; // 寻找插入位置 while (i >= 0 && arr[i] > x) { arr[i + 1] = arr[i]; i--; } // 插入元素 arr[i + 1] = x; }
void sort_insert(sqlist &va , int x)
int temp = 0;
while(x > va->data[temp]) temp++;
for(int i = va->length-1;i>=temp;i--) va->data[i+1] = va->data[i];
va->data[temp] = x;
va->length++;
#include <stdio.h>
void insert(int a[],int n,int num)
for(int i=0;i<=n;i++)
if(a[i]>=num)
for(int j=n+1;j>=i;j--)
a[j]=a[j-1];
a[i]=num;
int main ( )
int a[20],n,num;
printf("请输入小于20个的数组元素个数:\n");
printf("请输入%d个元素,以空格隔开:\n",n);
for(int i=0;i<n;i++)
scanf("%d",&a[i]);
printf("请输入要插入的元素:\n");
scanf("%d",&num);
insert(a,n,num);
for(int i=0;i<n+1;i++)
printf("%d ",a[i]);
#include<stdio.h> //成功返回插入的位置,失败返回-1 int Insert(int va[],int n,int x){ int i,j; for(i=0;i<n;i++){ if(x<va[i]){ for(j=n;j>i;j--) va[j]=va[j-1]; va[j]=x; return j; } } return 0; } int main(){ int va[20],len=10; for(int i=0;i<len;i++) va[i]=i; double n; scanf("%lf",&n); int pos=Insert(va,len++,n); if(pos==-1) printf("失败\n"); else printf("插入的位置处于%d\n",pos); return 0; }
void insertSq(SqList va[],ElemType e){ len=length(va[]); va[len+1]=e; while(va[len]>e){ va[len+1]=va[len]; va[len]=e; len--; }
lll
void insert(List va[], Ele x) { int temp; int n = length(va[]); for (int i = 0; i < n; i++) { if (x < va[i]) break; } for (int j = n; j > i; j--) va[j] = va[j - 1]; va[j] = x; }
答案:
void Inser...
用户登录可进行刷题及查看答案
登录后提交答案