啥也不会!! 2022-03-19 17:53 采纳率: 60%
浏览 1716
已结题

假设一个顺序表中所有元素为整数,设计一个算法,使其中小于零的元素移动到所有大于等于零的前面

假设一个顺序表中所有元素为整数,设计一个算法,使其中小于零的元素移动到所有大于等于零的前面
大家帮看看哪里出错了,第一次写,谢谢各位了


#define MaxSize 100000
#include<stdio.h>
#include<stdlib.h>

typedef int ElemType;
typedef struct{
    ElemType data[MaxSize];
    int length;
}List;

void InitList(List &l){
    l.length=0;
}
int InsElem(List &l,ElemType x,int i){
    int j;
    if(i<1||i>l.length+1)
        return 0;
    for(j=l.length;j>i;j--)
        l.data[j]=l.data[j-1];
    l.data[i-1]=x;
    l.length++;
    return 1;
}
void DispList(List l){
    int i;
    for(i=0;i<l.length;i++)
        printf("%d",l.data[i]);
    printf("\n");
}
void move(List &l){
    int i=0,j=l.length-1;
    while (l.data[i]>=0,i++,l.data[j]<0,j--);
    if (i<j)
        swap(l.data[i],l.data[j]);
    
}

int main(){
    int i;
    ElemType e;
    List l;
    InitList(l);
    InsElem(l,-1,1);
    InsElem(l,9,2);
    InsElem(l,-2,3);
    InsElem(l,0,4);
    InsElem(l,5,5);
    InsElem(l,-3,6); 
    printf("线性表:");DispList(l);
    move(l);
    printf("线性表:");DispList(l);
}

img

  • 写回答

1条回答 默认 最新

  • fuill 2022-03-19 18:13
    关注
    #define MaxSize 100000
    #include<stdio.h>
    #include<stdlib.h>
     
    typedef int ElemType;
    typedef struct{
        ElemType data[MaxSize];
        int length;
    }List;
     
    void InitList(List &l){
        l.length=0;
    }
    void swap(int *a,int *b) 
    {
        int t=*a;*a=*b;*b=t;
    }
    int InsElem(List &l,ElemType x,int i){
        int j;
        if(i<1||i>l.length+1)
            return 0;
        for(j=l.length;j>i;j--)
            l.data[j]=l.data[j-1];
        l.data[i-1]=x;
        l.length++;
        return 1;
    }
    void DispList(List l){
        int i;
        for(i=0;i<l.length;i++)
            printf("%d ",l.data[i]);
        printf("\n");
    }
    void move(List &l){
        int i=0,j=l.length-1;
        /*while (l.data[i]>=0,i++,l.data[j]<0,j--)
        if (i<l.length)
        {
        swap(&l.data[i],&l.data[j]);
        } */
        for(i=0;i<l.length-1;i++)
        for(j=0;j<l.length-1-i;j++)
        if(l.data[j]>l.data[j+1]) swap(&l.data[j],&l.data[j+1]);
    }
     
    int main(){
        int i;
        ElemType e;
        List l;
        InitList(l);
        InsElem(l,-1,1);
        InsElem(l,9,2);
        InsElem(l,-2,3);
        InsElem(l,0,4);
        InsElem(l,5,5);
        InsElem(l,-3,6); 
        printf("线性表:");DispList(l);
        move(l);
        printf("线性表:");DispList(l);
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

问题事件

  • 系统已结题 3月28日
  • 已采纳回答 3月20日
  • 修改了问题 3月19日
  • 创建了问题 3月19日

悬赏问题

  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改