首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C语言 >

写的一个快速排序,但一直提示函数多处定义

2013-11-29 
写的一个快速排序,但一直提示函数多处定义,请指教qsort.c#defineMAXSIZE10typedefstruct{intr[MAXSIZE]in

写的一个快速排序,但一直提示函数多处定义,请指教
qsort.c

#define  MAXSIZE  10

typedef  struct
{
    int  r[MAXSIZE];
    int  length;
}Sqlist;



void  Swap(Sqlist  *L,int  i,int  j)
{
    int  temp =L->r[i];
    L->r[i] =L->r[j];
    L->r[j] =temp;
}



int  Part(Sqlist *L,int low,int high)
 {
     int  pivotkey;

     pivotkey=L->r[low];

     while(low < high)
     {
         while(low < high && pivotkey <= L->r[high])
            high--;
         Swap(L,low,high);

         while(low < high && pivotkey >=L->r[low])
            low++;
         Swap(L,low,high);
     }

     return  low;
 }




void  Qsort(Sqlist  *L,int low,int  high)
{
    int   pivot;

    if(low < high)
    {
        pivot=Part(L,low,high);

        Qsort(L,low,pivot-1);
        Qsort(L,pivot+1,high);
    }
}


void QuickSort(Sqlist *L)
{
QSort(L,0,L->length);
}



main.c
#include <stdio.h>
#include <stdlib.h>
#include "qsort.c"

int main()
{
    int  i;
    int  d[10]={50,30,21,44,65,52,78,42,78,90};
    Sqlist  *L=(Sqlist  *)malloc(sizeof(Sqlist));
    L->length=10;
    for(i=0;i <10;i++)
    {

        L->r[i]=d[i];
    }



    QuickSort(L);

    for(i=0;i <10;i++)
        printf("%d\t",L->r[i]);
    return 0;
}



编译器一直提示:part函数,swap函数多处定义,怎么解决?
[解决办法]
换个名字试试,可能<stdio.h><stdlib.h>里面已经定义了swap
[解决办法]
引用:
Quote: 引用:

加个 头文件

//qsort.h
#ifndef __QSORT_H__
#define __QSORT_H__
#define  MAXSIZE  10
 
typedef  struct
{
    int  r[MAXSIZE];
    int  length;
}Sqlist;

void  Swap(Sqlist  *L,int  i,int  j);
int  Part(Sqlist *L,int low,int high);
void  Qsort(Sqlist  *L,int low,int  high);
void QuickSort(Sqlist *L);

#endif // qsort.h



//然后 qsort.c里面的结构体定义不要,
#include "qsort.h";

//main函数文件
#include "qsort.h"

 


修改以后提示:D:\CODE\codeblock\qsort\qsort.c
[解决办法]
51
[解决办法]
undefined reference to `QSort'
[解决办法]

5那一行的是Qsort,  而不是QSort 注意大小写!

热点排行
Bad Request.