qq_45180612 2019-12-13 14:58 采纳率: 0%
浏览 113

哪位大佬能帮我把这段c程序改成Java谢谢。

#include
#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10
#define OVERFLOW -1
#define OK 1
#define ERROR 0
typedef int ElemType;
typedef int Status;

//顺序表存储结构的定义
typedef struct
{
ElemType *elem;
int length;
int listsize;
}sqlist;

//顺序表的初始化
Status InitList_sq(sqlist *L)
{
L->elem = (ElemType *)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if (!L->elem)
exit(OVERFLOW);
L->length = 0;
L->listsize = LIST_INIT_SIZE;
return OK;
}

//顺序表的插入
Status ListInsert_sq(sqlist L, int i, int e)
{
ElemType *newbase, *p, *q;
if (i < 1 || i > L->length+1)
{
return ERROR;
}
if (L->length >= L->listsize)
{
newbase = (ElemType
)realloc(L->elem,(L->listsize+LISTINCREMENT)*sizeof(ElemType));
if (!newbase)
exit(OVERFLOW);
L->elem = newbase;
L->listsize = L->listsize+LISTINCREMENT;
}
q = &(L->elem[i-1]);
for(p = &(L->elem[L->length-1]) ; p >= q ; --p)
{
*(p+1) = *p;
}
*q = e;
++L->length;
return OK;
}

//顺序表的删除
Status ListDelete_sq(sqlist *L, int i, ElemType *e)
{
ElemType *p, *q;
if ( i < 1 || i > L->length)
{
return ERROR;
}
p = &(L->elem[i-1]);
*e = *p;
q = L->elem + L->length-1;
for(++p; p<= q; ++p)
{
*(p-1) = *p;
}
--L->length;
return OK;

}

//顺序表的归并
void MergeList_sq(sqlist La, sqlist *Lb, sqlist *Lc)
{
ElemType *pa ,*pb,*pc,*pa_last,*pb_last;
pa = La->elem;
pb = Lb->elem;
Lc->listsize = Lc->length = La->length + Lb->length;
pc = Lc->elem = (ElemType
)malloc(Lc->listsize*sizeof(ElemType));
if(!Lc->elem)
{
exit(OVERFLOW);
}
pa_last = La->elem + La->length - 1;
pb_last = Lb->elem + Lb->length - 1;
while(pa <= pa_last && pb <= pb_last)
{
if(*pa <= *pb)
*pc++ = *pb++;
else
*pc++ = *pb++;
}
while(pa <= pa_last)
*pc++ = *pa++;
while(pb <= pb_last)
*pc++ = *pb++;
}

int main() {
int i;
int n = 5;
ElemType e;
sqlist La,Lb,Lc;
if(InitList_sq(&La))
{
printf("Init is ok\n");
printf("Length:%d\n",La.length);
printf("listsize:%d\n",La.listsize);

} else {
printf("error!");
}

//顺序表输入初始值
printf("Please input the value of sq:\n");
for (i = 1; i <= n; i++) 
{
    scanf("%d",&e);
    if (ListInsert_sq(&La, i, e) != OK) 
    {
        break;
    }
}
for(i = 1; i <= La.length; i++) 
{
    printf("e[%d]=%d\n",i-1, La.elem[i-1]);
}
printf("Length:%d\n\n",La.length);

///插入 
printf("Please input the place of insert:\n");
scanf("%d",&i);
printf("Please input the value of insert:\n");
scanf("%d",&e);
if(ListInsert_sq(&La,i,e) == OK) 
{
    for (i = 1; i <= La.length; i++) 
    {
        printf("e[%d]=%d\n",i-1, La.elem[i-1]);
        printf("Length:%d\n\n",La.length);
    }
}
//删除
printf("Please input the place of delet:\n");
scanf("%d",&i);
if(ListDelete_sq(&La, i, &e) == OK) 
{
    printf("The delete elem is:%d\n",e);
}
for (i = 1; i < La.length; i++) 
{
        printf("e[%d]=%d\n",i-1, La.elem[i-1]);
        printf("Length:%d\n\n",La.length);
}

//顺序表的归并
printf("Please input the value of sq2:\n");
InitList_sq(&Lb);
InitList_sq(&Lc);
for (i = 1; i <= n; i++) 
{
    scanf("%d",&e);
    if (ListInsert_sq(&Lb, i, e) != OK) 
    {
        break;
    }
}
MergeList_sq(&La, &Lb, &Lc);
for(i = 1; i <= Lc.length; i++) 
{
    printf("e[%d]=%d\n",i-1, Lc.elem[i-1]);
}
    return 0;

}

  • 写回答

1条回答

  • 藏柏 2023-07-18 17:11
    关注
    
    import java.util.Arrays;
    
    public class Main {
        static final int LIST_INIT_SIZE = 100;
        static final int LISTINCREMENT = 10;
        static final int OVERFLOW = -1;
        static final int OK = 1;
        static final int ERROR = 0;
    
        static class Sqlist {
            int[] elem;
            int length;
            int listsize;
    
            Sqlist() {
                elem = new int[LIST_INIT_SIZE];
                length = 0;
                listsize = LIST_INIT_SIZE;
            }
        }
    
        static boolean InitList_sq(Sqlist L) {
            L.elem = new int[LIST_INIT_SIZE];
            if (L.elem == null)
                System.exit(OVERFLOW);
            L.length = 0;
            L.listsize = LIST_INIT_SIZE;
            return true;
        }
    
        static boolean ListInsert_sq(Sqlist L, int i, int e) {
            if (i < 1 || i > L.length + 1) {
                return false;
            }
            if (L.length >= L.listsize) {
                L.elem = Arrays.copyOf(L.elem, L.listsize + LISTINCREMENT);
                if (L.elem == null)
                    System.exit(OVERFLOW);
                L.listsize += LISTINCREMENT;
            }
            for (int p = L.length - 1; p >= i - 1; p--) {
                L.elem[p + 1] = L.elem[p];
            }
            L.elem[i - 1] = e;
            L.length++;
            return true;
        }
    
        static boolean ListDelete_sq(Sqlist L, int i, int[] e) {
            if (i < 1 || i > L.length) {
                return false;
            }
            *e = L.elem[i - 1];
            for (int p = i; p < L.length; p++) {
                L.elem[p - 1] = L.elem[p];
            }
            L.length--;
            return true;
        }
    
        static void MergeList_sq(Sqlist La, Sqlist Lb, Sqlist Lc) {
            Lc.listsize = Lc.length = La.length + Lb.length;
            Lc.elem = new int[Lc.listsize];
            int pa = 0, pb = 0, pc = 0;
            while (pa < La.length && pb < Lb.length) {
                if (La.elem[pa] <= Lb.elem[pb]) {
                    Lc.elem[pc++] = La.elem[pa++];
                } else {
                    Lc.elem[pc++] = Lb.elem[pb++];
                }
            }
            while (pa < La.length) {
                Lc.elem[pc++] = La.elem[pa++];
            }
            while (pb < Lb.length) {
                Lc.elem[pc++] = Lb.elem[pb++];
            }
        }
    
        public static void main(String[] args) {
            int i;
            int n = 5;
            int e;
            Sqlist La, Lb, Lc;
    
            La = new Sqlist();
            if (InitList_sq(La)) {
                System.out.println("Init is ok");
                System.out.println("Length: " + La.length);
                System.out.println("Listsize: " + La.listsize);
            } else {
                System.out.println("Error!");
            }
    
            // 顺序表输入初始值
            System.out.println("Please input the value of sq:");
            for (i = 1; i <= n; i++) {
                e = Integer.parseInt(System.console().readLine());
                if (!ListInsert_sq(La, i, e)) {
                    break;
                }
            }
            for (i = 1; i <= La.length; i++) {
                System.out.println("e[" + (i - 1) + "] = " + La.elem[i - 1]);
            }
            System.out.println("Length: " + La.length);
    
            // 插入
            System.out.println("Please input the place of insert:");
            i = Integer.parseInt(System.console().readLine());
            System.out.println("Please input the value of insert:");
            e = Integer.parseInt(System.console().readLine());
            if (ListInsert_sq(La, i, e)) {
                for (i = 1; i <= La.length; i++) {
                    System.out.println("e[" + (i - 1) + "] = " + La.elem[i - 1]);
                    System.out.println("Length: " + La.length);
                }
            }
    
            // 删除
            System.out.println("Please input the place of delete:");
            i = Integer.parseInt(System.console().readLine());
            if (ListDelete_sq(La, i, &e)) {
                System.out.println("The delete elem is: " + e);
            }
            for (i = 1; i < La.length; i++) {
                System.out.println("e[" + (i - 1) + "] = " + La.elem[i - 1]);
                System.out.println("Length: " + La.length);
            }
    
            // 顺序表的归并
            System.out.println("Please input the value of sq2:");
            Lb = new Sqlist();
            Lc = new Sqlist();
            for (i = 1; i <= n; i++) {
                e = Integer.parseInt(System.console().readLine());
                if (!ListInsert_sq(Lb, i, e)) {
                    break;
                }
            }
            MergeList_sq(La, Lb, Lc);
            for (i = 1; i <= Lc.length; i++) {
                System.out.println("e[" + (i - 1) + "] = " + Lc.elem[i - 1]);
            }
        }
    }
    
    
    评论

报告相同问题?

悬赏问题

  • ¥50 易语言把MYSQL数据库中的数据添加至组合框
  • ¥20 求数据集和代码#有偿答复
  • ¥15 关于下拉菜单选项关联的问题
  • ¥20 java-OJ-健康体检
  • ¥15 rs485的上拉下拉,不会对a-b<-200mv有影响吗,就是接受时,对判断逻辑0有影响吗
  • ¥15 使用phpstudy在云服务器上搭建个人网站
  • ¥15 应该如何判断含间隙的曲柄摇杆机构,轴与轴承是否发生了碰撞?
  • ¥15 vue3+express部署到nginx
  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况