大家看看排序有什么问题?

THEBEST 2003-09-12 11:31:54
/*线性表的插入与删除,排序操作. */

#include <stdio.h>
#include <stdlib.h>
#include <ctype.h> /*为toupper();提供原型*/

#define LIST_INIT_SIZE 100
#define LISTINCREMENT 10

typedef int ElemType;
typedef struct
{
ElemType *elem; /*存储空间基址*/
int length; /*当前长度*/
int listsize; /*当前分配的存储容量*/
}List;

void Show_Choose_Menu ( void); /*显示菜单*/
void Display_List ( List L); /*显示线性表内容*/
List Construct_List ( List L); /*初始化*/
List Insert_List ( List L); /*线性表插入操作*/
List Delete_List ( List L); /*线性表删除操作*/
void Sort_List ( List L); /*线性表排序操作*/
List Sort_Method_A(List L); /*升序排列*/
List Sort_Method_D(List L); /*降序排列*/
List To_Empty_List ( List L); /*清空线性表*/
void swap(ElemType *a,ElemType *b);
int main (void)
{
char ch;
List L;
Show_Choose_Menu();
printf("Please Input The First Letter Of The Word: ");
scanf(" %c",&ch); /*特别注意这里的输入情况*/
while ( (ch = toupper(ch)) != 'Q')
{
switch (ch)
{
case 'C': L= Construct_List(L); break;
case 'I': L= Insert_List(L); break;
case 'D': L= Delete_List(L); break;
case 'S': Sort_List(L); break;
case 'T': L= To_Empty_List (L); break;
default : printf("\nYour Input Has Error!Again!\n"); break;
}
Show_Choose_Menu();
printf("Please Choose The Operation By The First Letter: ");
scanf(" %c",&ch);
}
printf("Thank You For Using My List Program!\n");
printf("BYE!\n");
return 0;
}

void Show_Choose_Menu(void)
{
printf(" \n Construt_List To_Empty_List Quit\n");
printf(" Insert_List Delete_List Sort_List\n");
}

List Construct_List ( List L)
{
L.elem=(ElemType *) malloc (LIST_INIT_SIZE * sizeof(ElemType));
if (!L.elem) exit(1);
L.length = 0;
L.listsize = LIST_INIT_SIZE;
Display_List(L);
return L;
}

void Display_List(List L)
{
int i;
if(L.length == 0) printf("Now. No Value Exist In The List.Please Insert!");
else {
printf("Now After Your Operation.The List Is :\n ");
for(i=0;i<L.length;i++)
printf("%d ",*(L.elem+i));
}
}

List Insert_List(List L)
{
int i;
ElemType value;
ElemType *newbase,*q,*p;
printf("Please Input The Insert Number And Value: ");
while(2 != scanf("%d%d",&i,&value) || i<1 || i>L.length+1)
{
printf("Input Error! Input Again Please: ");
continue;
}
if(L.length>=L.listsize){
newbase=(ElemType *) realloc(L.elem,
(L.listsize+LISTINCREMENT) * sizeof(ElemType));
if (!newbase) exit(1);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
q=&(L.elem[i-1]);
for (p=&(L.elem[L.length-1]);p>=q; --p)
*(p+1) = *p;
*q = value;
++L.length;
printf("The Value %d Had Insert At %d \n",value,i);
Display_List(L);
return L;
}

List Delete_List(List L)
{
int i;
ElemType value;
ElemType *p,*q;
printf("Please Input You Want Delete's Value's Number: ");
while(1 != scanf("%d",&i) ||(i<1) || (i>L.length))
{
printf("Your Input Error. Input Again: ");
continue;
}
p = &(L.elem[i-1]);
value = *p;
q = L.elem+L.length-1;
for(++p;p<=q;++p)
*(p-1) = *p;
--L.length;
printf("The Value %d Had Delete At %d \n",value,i);
Display_List(L);
return L;
}

void Sort_List(List L)
{
char c;
printf("Please Choose Sort Method, A or D: ");
scanf(" %c",&c);
if(c == 'a') L= Sort_Method_A(L);
else L=Sort_Method_D(L);
Display_List(L);
}

List Sort_Method_A(List L)
{
int i,j;
for (i=0;i<L.length;i++)
for(j=0;j<i;j++)
if(*(L.elem+i) > *(L.elem+j))
swap(L.elem+i,L.elem+j);
return L;
}

List Sort_Method_D(List L)
{
int i,j;
for (i=0;i<L.length;i++)
for(j=0;j<i;j++)
if(*(L.elem+i) < *(L.elem+j))
swap(L.elem+i,L.elem+j);
return L;
}

List To_Empty_List (List L)
{
free (L.elem);
L.length=0;
Display_List (L);
return L;
}

void swap(ElemType *a,ElemType *b)
{
ElemType temp;
temp = *b;
*b = *a;
*a = temp;
}

为什么每次排序之后再插入.把原来的表都改掉了???
就是每次排序都改变了原表???
我传的都是按值传递的呀!
...全文
40 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ilovecoding 2003-09-15
  • 打赏
  • 举报
回复
我不是指你的程序结构,而是提供的提示界面,比如输入出错时应给出说明.
还有表的构建应该是程序内部的任务,如果操作不慎可能将原表丢失,还可能内存泄漏.
(这只是我的浅见,不对的还请包含和指教)


不知道TianGuangZao(天光早) 对这个问题的解释能否更深入一些,是所有的编译器对实参(结构类型)的传递都是对其地址的传递吗?
Andy84920 2003-09-14
  • 打赏
  • 举报
回复
特别感谢: TianGuangZao(天光早)
说的太对了,我一下没想到.

不过你觉得应该怎么改好呢?我觉得在排序的时候进行显示复制!有没有更好的呢?

TO: Ilovecoding(电脑蚊子)
我的程序很难懂吗?我这样做是为了让我们现在的一些初学者知道线性表的全过程.

我觉得我的程序比较符合结构化设计呀.而且结构比较好.符合C99.
有什么意见吗?
Andy84920 2003-09-12
  • 打赏
  • 举报
回复
但是我排序的时候根本没有改变地址呀.
我排序的时候只是把原来的表复制到另一个表中然后排序(这个过程中有更改数据)
但这是复制过来的表被改了,应该不会影响原来的表呀.不是吗?

ElemType *elem;
这个改变应该和我排序这个没有关系吧?
liushmh 2003-09-12
  • 打赏
  • 举报
回复
但是你在操作时改变了ElemType *elem; /*存储空间基址*/地址,值传递就是把这个地址也作为值传递进去了,然后改变了,所以...
Ilovecoding 2003-09-12
  • 打赏
  • 举报
回复
你的程序好难懂呀,给出程序的操作流程及输入的格式嘛。
还有线性表的构建应该自己完成嘛,留给用户(每次都得先打“C”)?
TianGuangZao 2003-09-12
  • 打赏
  • 举报
回复
程序好长呀!看了老半天。
为什么原来的表被改变了呢?
原因是从头到尾就一张表。
可能你会以为在 List Sort_Method_A(List L) 中采取传值的方式,就认为复制原来的表。
殊不知 L 只是原来 List 的结构,就是:
typedef struct
{
ElemType *elem; /*存储空间基址*/
int length; /*当前长度*/
int listsize; /*当前分配的存储容量*/
}List;
换句话说,你只是把表头的指针和表的结构给复制了一份。
Andy84920 2003-09-12
  • 打赏
  • 举报
回复
没有一个人回答我?好痛苦啊!

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧