注册 登录
编程论坛 数据结构与算法

关于数据结构的几个问题,求大神

五游士道 发布于 2015-04-12 10:56, 2035 次点击
我的问题会在二楼,这里是程序而已




#define MAXSIZE 100    /*宏定义*/
#define OK 1
#define OVERFLOW -2

#include "stdio.h"    /*包含输入输出文件*/

typedef int elemtype;
typedef struct          /*定义顺序表的结构*/
{elemtype vec[MAXSIZE]; /*顺序表数据成员所占据的存储空间*/
 int last;              /*顺序表中最后一个元素在数组中的下标(或向量中的位置)从0开始*/
}sequenlist;

int insert(L,i,x)  /*在顺序表的第i个元素之前插入一个新元素x*/
sequenlist *L;
int i;
elemtype x;
{ int j;
 if(((*L).last)>=MAXSIZE-1)
   {printf("the list is overflow!\n");
    return(0); /*溢出判断*/
    }
 else
    if((i<1)||(i>(*L).last+1))
     {printf("position is not correct!\n");
      return(0); /*插入位置不正确*/
     }
    else
     {for(j=(*L).last;j>=i-1;j--)  /*后移元素*/
      (*L).vec[j+1]=(*L).vec[j];
      (*L).vec[i-1]=x;     /*插入新元素*/
      (*L).last=(*L).last+1;/*修改last的值*/
      }
      return(1);
 }
 void delete(L,i)  /*删除顺序表的第i个元素*/
 sequenlist *L;
 int i;
 { int j;
  if((i<1)||(i>(*L).last+2))
      printf("delete fail\n");
  else
      {for(j=i;j<=(*L).last;j++)
          (*L).vec[j-1]=(*L).vec[j];/*前移元素,覆盖掉要删除元素的值*/
       (*L).last--;  /*修改last的值*/
       }
 }

 void listprint(sequenlist *L) /*输出线性表*/
 {int i;
  for(i=0;i<=(*L).last;i++)
  printf("i,e=%d,%d\n",i,L->vec[i]);
 }

  main()
 {
  sequenlist sl={{1,2,3,4,5,6,7,8,9,10},9};//直接给顺序表赋初值
  sequenlist *L;/*定义一个指向顺序表类型的指针变量*/
  int i,j,x;
  elemtype e;
  L=&sl; /*给指针变量赋值*/
  printf("please input the insert position and insert value\n");
  scanf("%d,%d",&i,&x);
  printf("the insert position: %d \ninsert value:%d\n",i,x);
  insert(L,i,x);
  listprint(L);
  printf("please intput the delete position:");
  scanf("%d",&j);
  delete(L,j);
  listprint(L);
}
2 回复
#2
五游士道2015-04-12 10:59
问题是程序中的插入操作有没有错?写的是insert(L,i,x),但是不应该是insert(L,x,i)吗?
#3
五游士道2015-04-12 11:03
第二个疑问就是宏定义中写OK 1,而OVERFLOW -2,然后问题来了,过程中返回操作return应该怎样写,是写return(OK),还是写return(0)
1