藍森林首頁 | 返回主頁 | 本站地圖 | 站內搜索 | 聯繫信箱 |
 您目前的位置:首頁 > 自由軟件 > 技術交流 > 應用編程


    

藍森林 http://www.lslnet.com 2006年6月6日 10:18


在排序時出了錯,另外給些這個程序在思想上的指導吧?

*線性表的插入與刪除,排序操作. */

#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;
}

為什麼每次排序之後再插入.把原來的表都改掉了???
就是每次排序都改變了原表???
我傳的都是按值傳遞的呀!

在排序時出了錯,另外給些這個程序在思想上的指導吧?

你是想學數據結構嗎?如果不是,你用stl的list和sort吧,很方便的。



Copyright © 1999-2000 LSLNET.COM. All rights reserved. 藍森林網站 版權所有。 E-mail : webmaster@lslnet.com