LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 730|回复: 8

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

[复制链接]
发表于 2003-9-12 12:03:34 | 显示全部楼层 |阅读模式
/*线性表的插入与删除,排序操作. */
#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("lease 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("lease 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("lease 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("lease 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("lease 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;
}

为什么每次排序之后再插入.把原来的表都改掉了???
就是每次排序都改变了原表???
我传的都是按值传递的呀!
 楼主| 发表于 2003-9-12 12:04:30 | 显示全部楼层
再来说说我写的这个程序有没有价值?或者提供一点意见?
发表于 2003-9-12 13:11:29 | 显示全部楼层
如果是C++
那么STL中已经有几种sort算法了
只要重载
operator <就可以实现生意元素排序

另外写算法也可以学习而不是希望很有用
可以自己加深理解吧

还有就是代码太乱了
看不清写什么
最好像他们那样对齐
 楼主| 发表于 2003-9-12 13:34:11 | 显示全部楼层
你把代码复制一下到你的编辑器看好吗?
我是按值传的.为什么会改变原LIST呢?搞不懂!
学C的数据结构当然用C,老师还不懂C++呢!
发表于 2003-9-12 13:43:21 | 显示全部楼层
\  [ code]
你的代码
[\ code]
发表于 2003-9-12 21:59:47 | 显示全部楼层
因为List里的elem是地址,排序的时候elem里的内容都变了。
发表于 2003-9-13 23:34:56 | 显示全部楼层

  1. /*线性表的插入与删除,排序操作. */
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <ctype.h> /*为toupper();提供原型*/

  5. #define LIST_INIT_SIZE 100
  6. #define LISTINCREMENT 10

  7. typedef int ElemType;
  8. typedef struct
  9. {
  10.         ElemType *elem; /*存储空间基址*/
  11.         int length; /*当前长度*/
  12.         int listsize; /*当前分配的存储容量*/
  13. }List;

  14. void Show_Choose_Menu ( void); /*显示菜单*/
  15. void Display_List ( List L); /*显示线性表内容*/
  16. List Construct_List ( List L); /*初始化*/
  17. List Insert_List ( List L); /*线性表插入操作*/
  18. List Delete_List ( List L); /*线性表删除操作*/
  19. void Sort_List ( List L); /*线性表排序操作*/
  20. List Sort_Method_A(List L); /*升序排列*/
  21. List Sort_Method_D(List L); /*降序排列*/
  22. List To_Empty_List ( List L); /*清空线性表*/
  23. void swap(ElemType *a,ElemType *b);
  24. int main (void)
  25. {
  26.         char ch;
  27.         List L;
  28.         Show_Choose_Menu();
  29.         printf("Please Input The First Letter Of The Word: ");
  30.         scanf(" %c",&ch); /*特别注意这里的输入情况*/
  31.         while ( (ch = toupper(ch)) != 'Q')
  32.         {
  33.                 switch (ch)
  34.                 {
  35.                         case 'C': L= Construct_List(L); break;
  36.                         case 'I': L= Insert_List(L); break;
  37.                         case 'D': L= Delete_List(L); break;
  38.                         case 'S': Sort_List(L); break;
  39.                         case 'T': L= To_Empty_List (L); break;
  40.                         default : printf("\nYour Input Has Error!Again!\n"); break;
  41.                 }
  42.                 Show_Choose_Menu();
  43.                 printf("Please Choose The Operation By The First Letter: ");
  44.                 scanf(" %c",&ch);
  45.         }
  46.         printf("Thank You For Using My List Program!\n");
  47.         printf("BYE!\n");
  48.         return 0;
  49. }

  50. void Show_Choose_Menu(void)
  51. {
  52.         printf(" \n Construt_List To_Empty_List Quit\n");
  53.         printf(" Insert_List Delete_List Sort_List\n");
  54. }

  55. List Construct_List ( List L)
  56. {
  57.         L.elem=(ElemType *) malloc (LIST_INIT_SIZE * sizeof(ElemType));
  58.         if (!L.elem) exit(1);
  59.         L.length = 0;
  60.         L.listsize = LIST_INIT_SIZE;
  61.         Display_List(L);
  62.         return L;
  63. }

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

  74. List Insert_List(List L)
  75. {
  76.         int i;
  77.         ElemType value;
  78.         ElemType *newbase,*q,*p;
  79.         printf("Please Input The Insert Number And Value: ");
  80.         while(2 != scanf("%d%d",&i,&value) || i<1 || i>L.length+1)
  81.         {
  82.                 printf("Input Error! Input Again Please: ");
  83.                 continue;
  84.         }
  85.         if(L.length>=L.listsize){
  86.                 newbase=(ElemType *) realloc(L.elem,
  87.                                 (L.listsize+LISTINCREMENT) * sizeof(ElemType));
  88.                 if (!newbase) exit(1);
  89.                 L.elem=newbase;
  90.                 L.listsize+=LISTINCREMENT;
  91.         }
  92.         q=&(L.elem[i-1]);
  93.         for (p=&(L.elem[L.length-1]);p>=q; --p)
  94.                 *(p+1) = *p;
  95.         *q = value;
  96.         ++L.length;
  97.         printf("The Value %d Had Insert At %d \n",value,i);
  98.         Display_List(L);
  99.         return L;
  100. }

  101. List Delete_List(List L)
  102. {
  103.         int i;
  104.         ElemType value;
  105.         ElemType *p,*q;
  106.         printf("Please Input You Want Delete's Value's Number: ");
  107.         while(1 != scanf("%d",&i) ||(i<1) || (i>L.length))
  108.         {
  109.                 printf("Your Input Error. Input Again: ");
  110.                 continue;
  111.         }
  112.         p = &(L.elem[i-1]);
  113.         value = *p;
  114.         q = L.elem+L.length-1;
  115.         for(++p;p<=q;++p)
  116.                 *(p-1) = *p;
  117.         --L.length;
  118.         printf("The Value %d Had Delete At %d \n",value,i);
  119.         Display_List(L);
  120.         return L;
  121. }

  122. void Sort_List(List L)
  123. {
  124.         char c;
  125.         printf("Please Choose Sort Method, A or D: ");
  126.         scanf(" %c",&c);
  127.         if(c == 'a') L= Sort_Method_A(L);
  128.         else L=Sort_Method_D(L);
  129.         Display_List(L);
  130. }

  131. List Sort_Method_A(List L)
  132. {
  133.         int i,j;
  134.         for (i=0;i<L.length;i++)
  135.                 for(j=0;j<i;j++)
  136.                         if(*(L.elem+i) > *(L.elem+j))
  137.                                 swap(L.elem+i,L.elem+j);
  138.         return L;
  139. }

  140. List Sort_Method_D(List L)
  141. {
  142.         int i,j;
  143.         for (i=0;i<L.length;i++)
  144.                 for(j=0;j<i;j++)
  145.                         if(*(L.elem+i) < *(L.elem+j))
  146.                                 swap(L.elem+i,L.elem+j);
  147.         return L;
  148. }

  149. List To_Empty_List (List L)
  150. {
  151.         free (L.elem);
  152.         L.length=0;
  153.         Display_List (L);
  154.         return L;
  155. }

  156. void swap(ElemType *a,ElemType *b)
  157. {
  158.         ElemType temp;
  159.         temp = *b;
  160.         *b = *a;
  161.         *a = temp;
  162. }
复制代码


This is indented automatically by vim
发表于 2003-9-14 13:03:01 | 显示全部楼层
vim中编码相关的操作有

set tabstop=4 (如果在windows下要和其它的编译用的话 可以用这个统一对齐)
set softtabstop=4 (tabstop不变 但按一次tab前进4个byte)
set shiftwidth=4 (缩进数目 8 太长了点)

对方
范围内对齐
=a{

全文对齐
gg=G
 楼主| 发表于 2003-9-14 19:36:00 | 显示全部楼层
大家能不能帮我解决一下问题啊?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

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