| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 432 人关注过本帖
标题:关于顺序表删除的问题
只看楼主 加入收藏
思小宏
Rank: 1
等 级:新手上路
帖 子:9
专家分:6
注 册:2012-12-1
结帖率:50%
收藏
已结贴  问题点数:5 回复次数:6 
关于顺序表删除的问题
在顺序表操作6那里出了问题,想不通是什么问题,小弟目前是大二软件工程的学生,求大师解答!




#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define  LIST_INIT_SIZE     100  // 线性表存储空间的初始分配量
#define  LISTINCREMENT    10   // 线性表存储空间的分配增量
#define  OK 1
#define OVERFLOW 0
#define ERROR 0
typedef char ElemType;  // 处理字符数据
typedef  struct {     
    ElemType *elem;    // 存储空间基址
    int      length;   // 当前长度
    int      listsize;  // 当前分配的存储容量以sizeof(ElemType)为单位
} SqList;  // 顺序表

int InitList_Sq(SqList& L) { // 顺序表操作1
  // 构造一个空的顺序表
  L.elem=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
  if(!L.elem)exit(OVERFLOW);
  L.length=0;
  L.listsize=LIST_INIT_SIZE;
  return OK;
} // InitList_Sq

void ShowList_Sq(SqList L) { // 顺序表操作2
  // 显示顺序表中的所有元素
int i;
if(!L.elem)exit(OVERFLOW);
 for(i=0;i<L.length;i++)
 {
     printf("%5d",L.elem[i]);
 }
 return;


  
} // ShowList_Sq

int GetLength_Sq(SqList L) { // 顺序表操作3
  // 获取顺序表表长
  if(!L.elem)exit(OVERFLOW);
  return L.length;
} // GetLength_Sq

int LocationList_Sq(SqList L, ElemType e) {  // 顺序表操作4
// 给定元素e在顺序表中找其位序
// 因为只找相等的,可以定义比较函数,也可不定义直接在本函数中比较相等
   int i=1;
   ElemType *p=L.elem;
   while(i<=L.length&&*p++!=e)i++;
   if(i<=L.length) return i;
   else return 0;
} // LocationList_Sq

int ListInsert_Sq(SqList& L, int i, ElemType e) { // 顺序表操作5
// 在顺序表L的第i个元素之前插入新的元素e,
// i 的合法范为 1≤i≤L.length+1
if (i < 1 || i > L.length+1) return ERROR;
if (L.length >= L.listsize) {
                          // 当前存储空间已满,增加分配
    ElemType *newbase;
    newbase = (ElemType *)realloc(L.elem,                                                                 
         (L.listsize+LISTINCREMENT)*sizeof (ElemType));
    if (!newbase) exit(OVERFLOW);  
                           // 存储分配失败
    L.elem = newbase;                // 新基址
    L.listsize += LISTINCREMENT; // 增加存储容量
}


} // ListInsert_Sq
int ListDelete_Sq(SqList& L, int i, ElemType& e) { // 顺序表操作6
// 把顺序表L的第i个元素删除, 并把其值放到e中
// i 的合法范围为 1≤i≤L.length
  if((i<1)||(i>L.length)) return ERROR;
  int p,q;
  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;
  
} // ListDelete_Sq   

[/color]
void DestroyList_Sq(SqList& L) { // 顺序表操作7
// 销毁顺序表
  free(L.elem);
  L.length=0;
}

int main() {
int i;
SqList L;
ElemType e;
ElemType a[]={'a','b','c','d','e'};
    InitList_Sq(L);                                // 实验内容1
    for(i=0;i<5;i++) ListInsert_Sq(L, i+1, a[i]);        // 实验内容2
    printf("当前表中的元素:\n");
    ShowList_Sq(L);
    printf("当前表的长度:%d\n",GetLength_Sq(L));
    i=LocationList_Sq(L, 'e');                         // 实验内容3
    if(i==0) printf("e不在当前表中\n");
    else printf("e在当前表中,在表中的位置:%d\n",i);
    i=LocationList_Sq(L, 'f');
    if(i==0) printf("f不在当前表中\n");
    else printf("f在当前表中,在表中的位置:%d\n",i);
    if(ListInsert_Sq(L, 2, 'f')) {                     // 实验内容4
        printf("插入成功,当前表中的元素:\n");
        ShowList_Sq(L);
        printf("当前表的长度:%d\n",GetLength_Sq(L));
    }
    if(ListDelete_Sq(L, 5, e)) {                     // 实验内容5
        printf("删除成功,删除的第5个元素是:%c\n",e);
        printf("当前表中的元素:\n");
        ShowList_Sq(L);
        printf("当前表的长度:%d\n",GetLength_Sq(L));
    }
DestroyList_Sq(L);                                 // 实验内容6
return 0;
}
搜索更多相关主题的帖子: 存储 include 线性表 
2012-12-01 12:55
yuccn
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:何方
等 级:版主
威 望:167
帖 子:6814
专家分:42393
注 册:2010-12-16
收藏
得分:0 
int ListDelete_Sq(SqList& L, int i, ElemType& e) { // 顺序表操作6
    // 把顺序表L的第i个元素删除, 并把其值放到e中
    // i 的合法范围为 1≤i≤L.length
    if((i<1)||(i>L.length)) return ERROR;
         e = L.elem[i-1];
         L.length--;

    for (int nIndex =i-1;nIndex <  L.length; nIndex++) {
        L.elem[nIndex ] = L.elem[nIndex +1];
   }

  return OK;
 
} // ListDelete_Sq

我行我乐
公众号:逻辑客栈
我的博客:
https://blog.yuccn. net
2012-12-01 21:31
思小宏
Rank: 1
等 级:新手上路
帖 子:9
专家分:6
注 册:2012-12-1
收藏
得分:0 
回复 2楼 yuccn
运行完后不是我想要的结果啊,表中没有数据
2012-12-02 21:52
凌云飞翔
Rank: 5Rank: 5
来 自:河北
等 级:职业侠客
威 望:6
帖 子:98
专家分:388
注 册:2012-4-7
收藏
得分:5 
回复 3楼 思小宏
程序代码:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define  LIST_INIT_SIZE     100  // 线性表存储空间的初始分配量
#define  LISTINCREMENT    10   // 线性表存储空间的分配增量
#define  OK 1
#define OVERFLOW 0
#define ERROR 0
typedef char ElemType; 
typedef  struct {    
    ElemType *elem;   
    int      length;  
    int      listsize; 
} SqList;  

int 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;
} // InitList_Sq

void ShowList_Sq(SqList L)
{

 
int i;
if(!L.elem)exit(OVERFLOW);
for(i=0;i<L.length;i++)
printf("%5c",L.elem[i]);
} // ShowList_Sq

int GetLength_Sq(SqList L) { // 顺序表操作3
  // 获取顺序表表长
  if(!L.elem)exit(OVERFLOW);
  return L.length;
} // GetLength_Sq

int LocationList_Sq(SqList L, ElemType e)
{  // 顺序表操作4
  int i=0;
   ElemType *p=L.elem;
   while(i<L.length&&*p++!=e)i++;
   if(i<L.length) return i+1;
   else return 0;
} // LocationList_Sq

int ListInsert_Sq(SqList &L, int i, ElemType x)
{
      int j;
     if(i<0||i>L.length)
         return 0;
     for(j=L.length-1;j>=i-1;j--)
         L.elem[j+1]=L.elem[j];
         L.elem[i-1]=x;
         L.length++;
         return 1;


} // ListInsert_Sq
int ListDelete_Sq(SqList &L, int i, ElemType &e)

 {
  if((i<1)||(i>L.length)) return ERROR;

 
     int j;
     e=L.elem[i-1] ;
       for(j=i;j<=L.length-1;j++)
         L.elem[j-1]=L.elem[j];
         L.length--;
     return 1;

} // ListDelete_Sq   

void DestroyList_Sq(SqList& L)

 { // 顺序表操作7
// 销毁顺序表
  free(L.elem);
  L.length=0;
} 

int main()
{
int i;
SqList L;
ElemType e;
ElemType a[]={'a','b','c','d','e'};
    InitList_Sq(L);                                // 实验内容1
    L.length=5  ;
    for(i=0;i<5;i++)  L.elem[i]=a[i] ;
    printf("当前表中的元素:\n");
    ShowList_Sq(L);
    printf("\n当前表的长度:%d\n",GetLength_Sq(L));
    i=LocationList_Sq(L, 'e');                         // 实验内容3
    if(i==0) printf("e不在当前表中\n");
    else printf("e在当前表中,在表中的位置:%d\n",i);
    i=LocationList_Sq(L, 'f');
    if(i==0) printf("f不在当前表中\n");
    else printf("f在当前表中,在表中的位置:%d\n",i);
    if(ListInsert_Sq(L, 2, 'f')) {                     // 实验内容4
        printf("插入成功,当前表中的元素:\n");
         ShowList_Sq(L);
        printf("\n当前表的长度:%d\n",GetLength_Sq(L));
    }
    if(ListDelete_Sq(L, 5, e)) {                     // 实验内容5
        printf("删除成功,删除的第5个元素是:%c\n",e);
        printf("当前表中的元素:\n");
        ShowList_Sq(L);
        printf("\n当前表的长度:%d\n",GetLength_Sq(L));
    }
DestroyList_Sq(L);                                 // 实验内容6
return 0;


}
2012-12-03 23:42
凌云飞翔
Rank: 5Rank: 5
来 自:河北
等 级:职业侠客
威 望:6
帖 子:98
专家分:388
注 册:2012-4-7
收藏
得分:0 
图片附件: 游客没有浏览图片的权限,请 登录注册
2012-12-03 23:43
思小宏
Rank: 1
等 级:新手上路
帖 子:9
专家分:6
注 册:2012-12-1
收藏
得分:0 
回复 4楼 凌云飞翔
完全正确~大师你是已经在工作的了么??
2012-12-04 11:01
凌云飞翔
Rank: 5Rank: 5
来 自:河北
等 级:职业侠客
威 望:6
帖 子:98
专家分:388
注 册:2012-4-7
收藏
得分:0 
大师可不敢当呀。。。偶一名学生而已
2012-12-04 22:09
快速回复:关于顺序表删除的问题
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022174 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved