鱼C论坛

 找回密码
 立即注册
查看: 2718|回复: 14

[已解决]为什么我的插入和删除的输出不正确呢 求帮助啊~

[复制链接]
发表于 2018-3-22 21:25:04 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
# define INCREMENT 5
# define ERROR -1
# define LISTSIZE 10
# include<stdio.h>
# include<stdlib.h>
typedef struct{
        int *data;
        int length;
        int listsize;
}Seqlist;

int Initial(Seqlist *L)
{
        L->data=(int*)malloc(LISTSIZE*sizeof(int));
        if(!(L->data))
                return ERROR;
        L->length=0;
        L->listsize=LISTSIZE;
}

void creat(Seqlist *L)
{
        int a,i;
        printf("请输入要创建的元素个数: ");
        scanf("%d",&a);
        for(i=0;i<a;i++)
        {
                printf("第%d个元素为:",i+1);
                scanf("%d",&L->data[i]);
                L->length++;
        }
}

void show(Seqlist *L)
{
        int i;
        printf("线性表中的元素为:");
        for(i=0;i<L->length;i++)
                printf("%d ",L->data[i]);
        printf("\n");
}

int getelem(Seqlist *L)
        {
        int i;
        printf("请输入你需要查找第几号元素:");
        scanf("%d",&i);
        printf("您查询的结果是:%d\n",L->data[i-1]);
        }

int listinsert(Seqlist *L,int i,int e)
{
        int j;
        if(i<1||i>L->length+1)
                return ERROR;
        else if(L->length=LISTSIZE)
                return ERROR;
        else
        {
                for(j=L->length-1;j>=i-1;j--)
                        L->data[j+1]=L->data[j];
                L->data[i-1]=e;
                L->length++;
                return 0;
        }
}


int listdelete(Seqlist *L,int i,int e)
{
        int j;
        e=L->data[i-1];
        if(i<1||i>L->length)
                return ERROR;
        else
        {
                for(j=i;j<=L->length-1;j++)
                        L->data[j-1]=L->data[j];
                L->length--;
        }

                return 0;
}




int main()
{
        Seqlist L;
        int a;
        Initial(&L);
        creat(&L);
        show(&L);
        getelem(&L);
        listinsert(&L,2,a);
        show(&L);
        listdelete(&L,2,a);
        show(&L);
        putchar('\n');
        return 0;
}
最佳答案
2018-3-26 19:38:11
本帖最后由 ba21 于 2018-3-26 20:55 编辑
愿你 发表于 2018-3-26 17:43
我依旧觉得我这个帖子的线性表没有太大问题啊。
我今天又编写了一遍 结果都出来了,但我今天只是在这个 ...

  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. #define INCREMENT 5
  4. #define OK 1
  5. #define ERROR -1
  6. #define LISTSIZE 10


  7. typedef struct{
  8.         int *data;
  9.         int length;
  10.         int listsize;
  11. }Seqlist;

  12. int Initial(Seqlist *L)
  13. {
  14.         L->data=(int*)malloc(LISTSIZE*sizeof(int));
  15.         if(!(L->data))
  16.                 return ERROR;
  17.         L->length=0;
  18.         L->listsize=LISTSIZE;

  19.                 return OK;
  20. }

  21. int creat(Seqlist *L)
  22. {
  23.         int a,i;
  24.         printf("请输入要创建的元素个数: ");
  25.         scanf("%d",&a);
  26.                
  27.                 if(a>LISTSIZE)
  28.                 {
  29.                         printf("元素个数不能超过 %d 个。\n", LISTSIZE);
  30.                         return ERROR;
  31.                 }

  32.         for(i=0;i<a;i++)
  33.         {
  34.                 printf("第%d个元素为:",i+1);
  35.                 scanf("%d",&L->data[i]);
  36.                 L->length++;
  37.         }

  38.                 return OK;
  39. }

  40. void show(Seqlist *L)
  41. {
  42.         int i;
  43.         printf("线性表中的元素为:");
  44.         for(i=0;i<L->length;i++)
  45.                 printf("%d ",L->data[i]);
  46.         printf("\n");
  47. }

  48. int getelem(Seqlist *L)
  49. {
  50.         int i;

  51.         printf("请输入你需要查找第几号元素:");
  52.         scanf("%d",&i);
  53.                 if(i<1 || i>L->length) // 当i不在范围内时
  54.                 {
  55.                          printf("%d 号元素不存在!\n", i);
  56.                          return ERROR;
  57.                 }

  58.         printf("您查询的结果是:%d\n",L->data[i-1]);

  59.                 return OK;
  60. }

  61. int listinsert(Seqlist *L,int i,int e)
  62. {
  63.                 int k;

  64.                 if(L->length == LISTSIZE) // 顺序线性表已经满了
  65.                 {
  66.                         return ERROR;
  67.                 }
  68.                 if(i<1 || i>L->length) // 当i不在范围内时
  69.                 {
  70.                         return ERROR;
  71.                 }
  72.                 if(i <= L->length) // 若插入数据位置不在表尾
  73.                 {
  74.                         /* 将要插入位置后数据元素向后移动一位 */
  75.                         for(k=L->length-1; k>=i-1; k--)
  76.                         {
  77.                                 L->data[k+1] = L->data[k];
  78.                         }
  79.                 }

  80.                 L->data[i-1] = e;
  81.                 L->length++;

  82.                 return OK;
  83. }


  84. int listdelete(Seqlist *L,int i,int *e)
  85. {
  86.         int k;

  87.         if(L->length == 0) // 空表
  88.         {
  89.                 return ERROR;
  90.         }
  91.         if(i<1 || i>L->length) // 删除的位置不正确
  92.         {
  93.                 return ERROR;
  94.         }

  95.         *e = L->data[i-1];

  96.         printf("%d, %d\n", L->length, i);
  97.         if(i<=L->length)
  98.         {
  99.                 for(k=i; k<=L->length; k++)
  100.                 {
  101.                         L->data[k-1] = L->data[k];
  102.                 }
  103.         }

  104.         L->length--;

  105.         return OK;
  106. }




  107. int main()
  108. {
  109.         Seqlist L;
  110.         int a, b;

  111.                 // 初始化
  112.         Initial(&L);
  113.         if(creat(&L)==OK)
  114.                 {
  115.                         // 显示
  116.                         show(&L);

  117.                         // 获取元素
  118.                         getelem(&L);

  119.                         // 插入元素
  120.                         printf("请输入要插入的元素:");
  121.                         scanf("%d", &a);
  122.                         listinsert(&L,2,a);
  123.                         show(&L);

  124.                         // 删除元素
  125.                         printf("执行删除操作:\n");
  126.                         listdelete(&L,2,&b);
  127.                         printf("删除的元素是: %d\n", b);
  128.                         show(&L);

  129.                         putchar('\n');
  130.                 }

  131.         return 0;
  132. }
复制代码
QQ图片20180322212438.png
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2018-3-22 23:12:52 | 显示全部楼层
看了你的代码真会把人搞晕。我只能说是神一样的代码。。。
简单说下
L->data=(int*)malloc(LISTSIZE*sizeof(int)); // 分配了10个int大小的空间
// 下面确可以无限输入
void creat(Seqlist *L)
{
        int a,i;
        printf("请输入要创建的元素个数: ");
        scanf("%d",&a);
        for(i=0;i<a;i++)
        {
                printf("第%d个元素为:",i+1);
                scanf("%d",&L->data);
                L->length++;
        }
}

建议:::你线性不是线性表(顺序表),搞不懂是什么玩意,看的人越看越蒙。你再仔细看看教程加深巩固基础
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-23 08:35:48 | 显示全部楼层
我的怎么就不是顺序表了...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-23 09:01:49 | 显示全部楼层
ba21 发表于 2018-3-22 23:12
看了你的代码真会把人搞晕。我只能说是神一样的代码。。。
简单说下
L->data=(int*)malloc(LISTSIZE*siz ...

而且我输入顺序表的是输出它的表长啊...为什么会错...
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-23 12:31:40 | 显示全部楼层
愿你 发表于 2018-3-23 08:35
我的怎么就不是顺序表了...

你怎么就是顺序表了。你自定义的自认为是,那我也无话可说。喊你再仔细看看教程;对我可没有好处。
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-23 17:14:57 | 显示全部楼层
int listinsert(Seqlist *L,int i,int e)
{
        int j;
        if(i<1||i>L->length+1)
                return ERROR;
        else if(L->length=LISTSIZE)   //这行写错了吧。应该是==
                return ERROR;
        else
        {
                for(j=L->length-1;j>=i-1;j--)           //L->length就行
                        L->data[j+1]=L->data[j];
                L->data[i-1]=e;                           //这个是 i 吧
                L->length++;
                return 0;
        }
}

想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-23 22:16:56 | 显示全部楼层
ba21 发表于 2018-3-23 12:31
你怎么就是顺序表了。你自定义的自认为是,那我也无话可说。喊你再仔细看看教程;对我可没有好处。

你说话语气也用不着这么冲啊 不懂才问的好吧
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 16:01:16 | 显示全部楼层
songqiang 发表于 2018-3-23 17:14
int listinsert(Seqlist *L,int i,int e)
{
        int j;

数组下标不是要减1吗 所以我就都减了
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 17:43:52 | 显示全部楼层
ba21 发表于 2018-3-23 12:31
你怎么就是顺序表了。你自定义的自认为是,那我也无话可说。喊你再仔细看看教程;对我可没有好处。

我依旧觉得我这个帖子的线性表没有太大问题啊。
我今天又编写了一遍 结果都出来了,但我今天只是在这个基础上进行修改而已。
所以 还请你告诉我 我哪里需要改进

# define ERROR -1
# define LISTSIZE 5
# include<stdio.h>
# include<stdlib.h>
typedef struct{
        int *elem;
        int length;
        int listsize;
}Sqlist;

int initlist(Sqlist *L) //初始化顺序表
{
        L->elem=(int*)malloc(LISTSIZE*sizeof(int));
        if(!(L->elem))
                return ERROR;
        L->length=0;
        L->listsize=LISTSIZE;
        return 1;
}

int creatlist(Sqlist *L)  //创建顺序表
{
        int i;
        printf("请输入你要创建的顺序表元素个数:\n");
        scanf("%d",&L->length);
        if(L->length>=L->listsize)
                return ERROR;
        printf("请输入你要创建的顺序表:\n");
        for(i=0;i<L->length;i++)
        {
                printf("第%d个元素是:",i+1);
                scanf("%d",&L->elem);
        }
        return 1;
}



void showlist(Sqlist *L) //遍历顺序表
{
        int i;
        printf("您的顺序表为:");
        for(i=0;i<L->length;i++)
                printf("%d ",L->elem);
        printf("\n");
}




int searchlist(Sqlist *L) //根据元素值查找元素的位置
{
        int e;
        int i;
        printf("请输入你想要查询的元素:");
        scanf("%d",&e);
        for(i=0;i<L->length;i++)
        {
                if(e==L->elem)
                {
                        printf("该元素位置为%d\n",i+1);
                        break;
                }
        }
        if(i>L->length)
                printf("尚未找到您想要的值。\n");
        return 1;
}

int  getelem(Sqlist L)  //根据位置查找元素值
{
        int i;
        printf("请输入您想要查询的位置:");
        scanf("%d",&i);
        if(i<1||i>L.length)
                return ERROR;
        else
                printf("您所查询的位置的值为:%d\n",L.elem[i-1]);
}



void isempty(Sqlist L)  //判断表示法为空
{
        if(L.length==0)
                printf("该表为空\n");
        else
                printf("该表不为空\n");
}


void listlength(Sqlist L)  //求表长
{
        printf("该表长为:%d\n",L.length);
}


void clearlist(Sqlist *L)   //清空线性表
{
        L->length=0;
        printf("该表已被清空哦~\n");
}

void  destroylist(Sqlist *L)//销毁线性表
{
        if(L->elem)
                free (L);
        printf("该表已被销毁哦~\n");
}

void insertlist(Sqlist *L)  //在第i个元素之前插入某元素
{
        int i,e,j;
        printf("请输入插入的元素位置i和元素内容e:");
        scanf("%d,%d",&i,&e);
        if(i<1||i>L->length)
                printf("插入位置不合法。");
        else if(L->length==LISTSIZE)
                printf("该表已满 无法插入");
        else
        {
                for(j=L->length;j>=i-1;j--)
                        L->elem[j+1]=L->elem[j];
                L->elem[i-1]=e;
                ++L->length;
        }

}

void deletelist(Sqlist *L)  //删除第i个元素
{
        int i,j;
        printf("请输入您要删除的元素位置i:");
        scanf("%d",&i);
        if(i<1||i>L->length)
                printf("删除位置不合法");
        else
        {
                for(j=i;j<=L->length-1;j++)
                {
                        L->elem[j-1]=L->elem[j];
                }
                --L->length;
        }
}




int main()
{
        Sqlist L;
        initlist(&L);
        creatlist(&L);
        showlist(&L);
        /*searchlist(&L);
        showlist(&L);
        getelem(L);
        showlist(&L);
        //clearlist(&L);
        //destroylist(&L);
        isempty(L);
        listlength(L);
        showlist(&L);*/
        insertlist(&L);
        showlist(&L);
        deletelist(&L);
        showlist(&L);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

发表于 2018-3-26 19:38:11 | 显示全部楼层    本楼为最佳答案   
本帖最后由 ba21 于 2018-3-26 20:55 编辑
愿你 发表于 2018-3-26 17:43
我依旧觉得我这个帖子的线性表没有太大问题啊。
我今天又编写了一遍 结果都出来了,但我今天只是在这个 ...

  1. #include<stdio.h>
  2. #include<stdlib.h>


  3. #define INCREMENT 5
  4. #define OK 1
  5. #define ERROR -1
  6. #define LISTSIZE 10


  7. typedef struct{
  8.         int *data;
  9.         int length;
  10.         int listsize;
  11. }Seqlist;

  12. int Initial(Seqlist *L)
  13. {
  14.         L->data=(int*)malloc(LISTSIZE*sizeof(int));
  15.         if(!(L->data))
  16.                 return ERROR;
  17.         L->length=0;
  18.         L->listsize=LISTSIZE;

  19.                 return OK;
  20. }

  21. int creat(Seqlist *L)
  22. {
  23.         int a,i;
  24.         printf("请输入要创建的元素个数: ");
  25.         scanf("%d",&a);
  26.                
  27.                 if(a>LISTSIZE)
  28.                 {
  29.                         printf("元素个数不能超过 %d 个。\n", LISTSIZE);
  30.                         return ERROR;
  31.                 }

  32.         for(i=0;i<a;i++)
  33.         {
  34.                 printf("第%d个元素为:",i+1);
  35.                 scanf("%d",&L->data[i]);
  36.                 L->length++;
  37.         }

  38.                 return OK;
  39. }

  40. void show(Seqlist *L)
  41. {
  42.         int i;
  43.         printf("线性表中的元素为:");
  44.         for(i=0;i<L->length;i++)
  45.                 printf("%d ",L->data[i]);
  46.         printf("\n");
  47. }

  48. int getelem(Seqlist *L)
  49. {
  50.         int i;

  51.         printf("请输入你需要查找第几号元素:");
  52.         scanf("%d",&i);
  53.                 if(i<1 || i>L->length) // 当i不在范围内时
  54.                 {
  55.                          printf("%d 号元素不存在!\n", i);
  56.                          return ERROR;
  57.                 }

  58.         printf("您查询的结果是:%d\n",L->data[i-1]);

  59.                 return OK;
  60. }

  61. int listinsert(Seqlist *L,int i,int e)
  62. {
  63.                 int k;

  64.                 if(L->length == LISTSIZE) // 顺序线性表已经满了
  65.                 {
  66.                         return ERROR;
  67.                 }
  68.                 if(i<1 || i>L->length) // 当i不在范围内时
  69.                 {
  70.                         return ERROR;
  71.                 }
  72.                 if(i <= L->length) // 若插入数据位置不在表尾
  73.                 {
  74.                         /* 将要插入位置后数据元素向后移动一位 */
  75.                         for(k=L->length-1; k>=i-1; k--)
  76.                         {
  77.                                 L->data[k+1] = L->data[k];
  78.                         }
  79.                 }

  80.                 L->data[i-1] = e;
  81.                 L->length++;

  82.                 return OK;
  83. }


  84. int listdelete(Seqlist *L,int i,int *e)
  85. {
  86.         int k;

  87.         if(L->length == 0) // 空表
  88.         {
  89.                 return ERROR;
  90.         }
  91.         if(i<1 || i>L->length) // 删除的位置不正确
  92.         {
  93.                 return ERROR;
  94.         }

  95.         *e = L->data[i-1];

  96.         printf("%d, %d\n", L->length, i);
  97.         if(i<=L->length)
  98.         {
  99.                 for(k=i; k<=L->length; k++)
  100.                 {
  101.                         L->data[k-1] = L->data[k];
  102.                 }
  103.         }

  104.         L->length--;

  105.         return OK;
  106. }




  107. int main()
  108. {
  109.         Seqlist L;
  110.         int a, b;

  111.                 // 初始化
  112.         Initial(&L);
  113.         if(creat(&L)==OK)
  114.                 {
  115.                         // 显示
  116.                         show(&L);

  117.                         // 获取元素
  118.                         getelem(&L);

  119.                         // 插入元素
  120.                         printf("请输入要插入的元素:");
  121.                         scanf("%d", &a);
  122.                         listinsert(&L,2,a);
  123.                         show(&L);

  124.                         // 删除元素
  125.                         printf("执行删除操作:\n");
  126.                         listdelete(&L,2,&b);
  127.                         printf("删除的元素是: %d\n", b);
  128.                         show(&L);

  129.                         putchar('\n');
  130.                 }

  131.         return 0;
  132. }
复制代码
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 19:51:35 | 显示全部楼层

甘拜下风!!!!!!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 19:59:40 | 显示全部楼层
你可以在帮我看看我现在的代码吗 还是和昨天的一样的题目合并 但是这次用顺序表完成


我还是打印出来的是第一个表
# include<stdio.h>
# include<stdlib.h>
# define ERROR -1
# define LISTSIZE 5
typedef struct{
        int *elem;
        int length;
        int listsize;
}Sqlist;

int initlist(Sqlist *L) //初始化顺序表
{
        L->elem=(int*)malloc(LISTSIZE*sizeof(int));
        if(!(L->elem))
                return ERROR;
        L->length=0;
        L->listsize=LISTSIZE;
        return 1;
}

int creatlist(Sqlist *L)  //创建顺序表
{
        int i;
        printf("请输入你要创建的顺序表元素个数:\n");
        scanf("%d",&L->length);
        if(L->length>=L->listsize)
                return ERROR;
        printf("请输入你要创建的顺序表:\n");
        for(i=0;i<L->length;i++)
        {
                printf("第%d个元素是:",i+1);
                scanf("%d",&L->elem[i]);
        }
        return 1;
}



void showlist(Sqlist *L) //遍历顺序表
{
        int i;
        printf("您的顺序表为:");
        for(i=0;i<L->length;i++)
                printf("%d ",L->elem[i]);
        printf("\n");
}
void unionlist(Sqlist *L1,Sqlist *L2)  //合并顺序表(判断L2中的每个元素是否在L1中,若不在则插入L1的尾部。)
{
        int a,b,i,j;
        int count;
        for(i=0;i<L2->length;i++)
        {
                a=L2->elem[i];
                for(j=0;j<L1->length;j++)
                {
                        b=L1->elem[j];
                        if(a==b)
                                break;
                }
                if(a!=b)
                {
                        count=L1->length;   //用count变量来标识L1的尾部,方便直接插入
                        L1->elem[count]=a;
                        L1->length++;
                }
        }
}

int main()
{
        Sqlist L1,L2;
        printf("请创建第一个表:");
        initlist(&L1);
        creatlist(&L1);
        showlist(&L1);
        printf("请创建第二个表:");
        initlist(&L2);
        creatlist(&L2);
        showlist(&L2);
        printf("合并之后的表1为:");
        showlist (&L1);
        return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 20:00:35 | 显示全部楼层

你可以在帮我看看我现在的代码吗 还是和昨天的一样的题目合并 但是这次用顺序表完成


我还是打印出来的是第一个表
# include<stdio.h>
# include<stdlib.h>
# define ERROR -1
# define LISTSIZE 5
typedef struct{
&nbsp; &nbsp; &nbsp; &nbsp; int *elem;
&nbsp; &nbsp; &nbsp; &nbsp; int length;
&nbsp; &nbsp; &nbsp; &nbsp; int listsize;
}Sqlist;

int initlist(Sqlist *L) //初始化顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; L->elem=(int*)malloc(LISTSIZE*sizeof(int));
&nbsp; &nbsp; &nbsp; &nbsp; if(!(L->elem))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ERROR;
&nbsp; &nbsp; &nbsp; &nbsp; L->length=0;
&nbsp; &nbsp; &nbsp; &nbsp; L->listsize=LISTSIZE;
&nbsp; &nbsp; &nbsp; &nbsp; return 1;
}

int creatlist(Sqlist *L)&nbsp;&nbsp;//创建顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; int i;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请输入你要创建的顺序表元素个数:\n");
&nbsp; &nbsp; &nbsp; &nbsp; scanf("%d",&L->length);
&nbsp; &nbsp; &nbsp; &nbsp; if(L->length>=L->listsize)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ERROR;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请输入你要创建的顺序表:\n");
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("第%d个元素是:",i+1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d",&L->elem);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return 1;
}



void showlist(Sqlist *L) //遍历顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; int i;
&nbsp; &nbsp; &nbsp; &nbsp; printf("您的顺序表为:");
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d ",L->elem);
&nbsp; &nbsp; &nbsp; &nbsp; printf("\n");
}
void unionlist(Sqlist *L1,Sqlist *L2)&nbsp;&nbsp;//合并顺序表(判断L2中的每个元素是否在L1中,若不在则插入L1的尾部。)
{
&nbsp; &nbsp; &nbsp; &nbsp; int a,b,i,j;
&nbsp; &nbsp; &nbsp; &nbsp; int count;
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L2->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a=L2->elem;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(j=0;j<L1->length;j++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b=L1->elem[j];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a==b)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a!=b)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count=L1->length;&nbsp; &nbsp;//用count变量来标识L1的尾部,方便直接插入
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1->elem[count]=a;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1->length++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
}

int main()
{
&nbsp; &nbsp; &nbsp; &nbsp; Sqlist L1,L2;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请创建第一个表:");
&nbsp; &nbsp; &nbsp; &nbsp; initlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; creatlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; showlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; printf("请创建第二个表:");
&nbsp; &nbsp; &nbsp; &nbsp; initlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; creatlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; showlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; printf("合并之后的表1为:");
&nbsp; &nbsp; &nbsp; &nbsp; showlist (&L1);
&nbsp; &nbsp; &nbsp; &nbsp; return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 20:01:17 | 显示全部楼层
# include<stdio.h>
# include<stdlib.h>
# define ERROR -1
# define LISTSIZE 5
typedef struct{
&nbsp; &nbsp; &nbsp; &nbsp; int *elem;
&nbsp; &nbsp; &nbsp; &nbsp; int length;
&nbsp; &nbsp; &nbsp; &nbsp; int listsize;
}Sqlist;

int initlist(Sqlist *L) //初始化顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; L->elem=(int*)malloc(LISTSIZE*sizeof(int));
&nbsp; &nbsp; &nbsp; &nbsp; if(!(L->elem))
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ERROR;
&nbsp; &nbsp; &nbsp; &nbsp; L->length=0;
&nbsp; &nbsp; &nbsp; &nbsp; L->listsize=LISTSIZE;
&nbsp; &nbsp; &nbsp; &nbsp; return 1;
}

int creatlist(Sqlist *L)&nbsp;&nbsp;//创建顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; int i;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请输入你要创建的顺序表元素个数:\n");
&nbsp; &nbsp; &nbsp; &nbsp; scanf("%d",&L->length);
&nbsp; &nbsp; &nbsp; &nbsp; if(L->length>=L->listsize)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return ERROR;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请输入你要创建的顺序表:\n");
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("第%d个元素是:",i+1);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; scanf("%d",&L->elem[i]);
&nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; return 1;
}



void showlist(Sqlist *L) //遍历顺序表
{
&nbsp; &nbsp; &nbsp; &nbsp; int i;
&nbsp; &nbsp; &nbsp; &nbsp; printf("您的顺序表为:");
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; printf("%d ",L->elem[i]);
&nbsp; &nbsp; &nbsp; &nbsp; printf("\n");
}
void unionlist(Sqlist *L1,Sqlist *L2)&nbsp;&nbsp;//合并顺序表(判断L2中的每个元素是否在L1中,若不在则插入L1的尾部。)
{
&nbsp; &nbsp; &nbsp; &nbsp; int a,b,i,j;
&nbsp; &nbsp; &nbsp; &nbsp; int count;
&nbsp; &nbsp; &nbsp; &nbsp; for(i=0;i<L2->length;i++)
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a=L2->elem[i];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; for(j=0;j<L1->length;j++)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; b=L1->elem[j];
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a==b)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; break;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(a!=b)
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count=L1->length;&nbsp; &nbsp;//用count变量来标识L1的尾部,方便直接插入
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1->elem[count]=a;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; L1->length++;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }
&nbsp; &nbsp; &nbsp; &nbsp; }
}

int main()
{
&nbsp; &nbsp; &nbsp; &nbsp; Sqlist L1,L2;
&nbsp; &nbsp; &nbsp; &nbsp; printf("请创建第一个表:");
&nbsp; &nbsp; &nbsp; &nbsp; initlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; creatlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; showlist(&L1);
&nbsp; &nbsp; &nbsp; &nbsp; printf("请创建第二个表:");
&nbsp; &nbsp; &nbsp; &nbsp; initlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; creatlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; showlist(&L2);
&nbsp; &nbsp; &nbsp; &nbsp; printf("合并之后的表1为:");
&nbsp; &nbsp; &nbsp; &nbsp; showlist (&L1);
&nbsp; &nbsp; &nbsp; &nbsp; return 0;
}
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

 楼主| 发表于 2018-3-26 20:02:45 | 显示全部楼层
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-3-29 08:32

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表