![]() |
#2
寒风中的细雨2010-11-18 22:18
原始的问题是数组越界 和 代码copy遗漏
//.cpp文件 #include <stdio.h> #include <malloc.h> #include <stdlib.h> #define OK 1 #define NULL 0 #define LIST_INTT_SIZE 100000 #define LISTINCREMENT 10 #define OVERFLOW 0 typedef int Status; typedef int ElemType; typedef struct { ElemType *elem; int Length; int MaxSize; }SqList; Status InitList_Sq(SqList &L) { L.elem=(ElemType*)malloc(1000*sizeof(ElemType)); if(!L.elem) exit(OVERFLOW); L.Length=0; L.MaxSize = LIST_INTT_SIZE; return OK; } void DataIn_Sq(SqList &L) { int x; int i; for(i=1;i<=5;i++) { scanf("%d",&x); L.elem[i-1]=x; L.Length++; } } void DataOut_Sq(SqList &L) { int i; for(i=0;i<L.Length;i++) printf("%d ",L.elem[i]); printf("\n"); } Status Partition(SqList &L, int low, int high) { int temp; int pivotkey; pivotkey = L.elem[low]; while(low<high) { while(low<high && L.elem[high]>=pivotkey) --high; temp=L.elem[low]; L.elem[low]=L.elem[high]; L.elem[high]=temp; while(low<high && L.elem[low]<=pivotkey) ++low; temp=L.elem[low]; L.elem[low]=L.elem[high]; L.elem[high]=temp; } return low; } void QuickSort(SqList &L, int low, int high) { int pivotkey; if(low<high) { pivotkey=Partition(L,low,high); QuickSort(L,low,pivotkey-1); QuickSort(L,pivotkey+1,high); } } void DestroyList(SqList &L) { free(L.elem); L.elem = NULL; L.Length = 0; L.MaxSize = 0; } void main() { SqList MyList; InitList_Sq(MyList); DataIn_Sq(MyList); DataOut_Sq(MyList); QuickSort(MyList,0,MyList.Length-1); DataOut_Sq(MyList); DestroyList(MyList); } |
#include <stdio.h>
#include <malloc.h>
#include <stdlib.h>
#define OK 1
#define NULL 0
#define LIST_INTT_SIZE 100000
#define LISTINCREMENT 10
#define OVERFLOW 0
typedef int Status;
typedef int ElemType;
typedef struct
{
ElemType *elem;
int Length;
int MaxSize;
}SqList;
Status InitList_Sq(SqList &L)
{
L.elem=(ElemType*)malloc(1000*sizeof(ElemType));
if(!L.elem)
exit(OVERFLOW);
L.Length=0;
L.MaxSize=LIST_INTT_SIZE;
return OK;
}
void DataIn_Sq(SqList &L)
{
int x;
int i;
for(i=1;i<10;i++)
{
scanf("%d",&x);
L.elem[i-1]=x;
L.Length++;
}
}
void DataOut_Sq(SqList &L)
{
int i;
for(i=0;i<L.Length;i++)
printf("%d ",L.elem[i]);
}
Status Partition(SqList &L, int low, int high)
{
int temp;
int pivotkey;
pivotkey=L.elem[low];
while(low<high)
{
while(low<high && L.elem[high]>=pivotkey)
--high;
temp=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=temp;
while(low<high && L.elem[low]<=pivotkey)
++low;
temp=L.elem[low];
L.elem[low]=L.elem[high];
L.elem[high]=temp;
}
return low;
}
void QuickSort(SqList &L, int low, int high)
{
int pivotkey;
if(low<high)
pivotkey=Partition(L,low,high);
QuickSort(L,low,pivotkey-1);
QuickSort(L,pivotkey+1,high);
}
void DestroyList(SqList &L)
{
free(L.elem);
L.elem=NULL;
L.Length=0;
L.MaxSize=0;
}
void main()
{
SqList MyList;
InitList_Sq(MyList);
DataIn_Sq(MyList);
DataOut_Sq(MyList);
QuickSort(MyList,Partition(MyList,0,1000),MyList.MaxSize);
DataOut_Sq(MyList);
DestroyList(MyList);
}