在已经排好序的线性表中插入一个数,还是升序
生活随笔
收集整理的這篇文章主要介紹了
在已经排好序的线性表中插入一个数,还是升序
小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
在已經(jīng)排好序的線性表中插入一個(gè)數(shù),還是升序
#include<stdio.h> #define Maxsize 100 typedef struct s {int elem[Maxsize];int last; }Seqlist; int Locate(Seqlist l,int e)//查找 {int i=0;while(i<=l.last&&l.elem[i]<=e)i++;if(i==l.last)return l.last;if(i<l.last)return i; } int InsList(Seqlist *l,int e,int i)//插入 {int k;if(l->last==Maxsize-1){return 0;}if(i==l->last){l->elem[l->last]=e;return 1;}else{for(k=l->last;k>=i-1;k--){l->elem[k+1]=l->elem[k];}l->elem[i]=e;//l->last++;return 1; } } int main() {Seqlist l;l.last=0;int m,i,n,k;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&l.elem[i]);l.last++;}scanf("%d",&m);k=Locate(l,m);InsList(&l,m,k);for(i=0;i<=l.last;i++){printf("%d ",l.elem[i]);}printf("\n");return 0; }
#include<stdio.h> #define Maxsize 100 typedef struct s {int elem[Maxsize];int last; }Seqlist; int Locate(Seqlist l,int e)//查找 {int i=0;while(i<=l.last&&l.elem[i]<=e)i++;if(i==l.last)return l.last;if(i<l.last)return i; } int InsList(Seqlist *l,int e,int i)//插入 {int k;if(l->last==Maxsize-1){return 0;}if(i==l->last){l->elem[l->last]=e;return 1;}else{for(k=l->last;k>=i-1;k--){l->elem[k+1]=l->elem[k];}l->elem[i]=e;//l->last++;return 1; } } int main() {Seqlist l;l.last=0;int m,i,n,k;scanf("%d",&n);for(i=0;i<n;i++){scanf("%d",&l.elem[i]);l.last++;}scanf("%d",&m);k=Locate(l,m);InsList(&l,m,k);for(i=0;i<=l.last;i++){printf("%d ",l.elem[i]);}printf("\n");return 0; }
總結(jié)
以上是生活随笔為你收集整理的在已经排好序的线性表中插入一个数,还是升序的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 攻防比赛_2020年度泉州市大学生网络安
- 下一篇: 李雅普诺夫稳定性、李雅普诺夫第一法、李雅