完美姜增华老婆的照片:用C 语言写栈,队,或者快排,插入排序(急用!)

来源:百度文库 编辑:查人人中国名人网 时间:2024/05/12 05:05:01
要能够在机子上实现的,栈 要进栈 出栈,对也是

都写出来,在追加100分,全赔上了!
最好有注释

调试通过....c++6.0
#include <iostream.h>
#define Max 50
typedef struct QuNode
{
int before,tail;
int QuArray[Max];
}Queue;
typedef struct StNode
{
int top,base;
int StArray[Max];
}Stata;
int QuEmpty(Queue * Q);
int QuFull(Queue *q);
void init_Qu(Queue * Q);
int EnterQu(Queue * Q, int i);
int OutQu(Queue * Q, int *i);
int StEmpty(Stata * S);
int StFull(Stata * S);
void init_St(Stata * S);
int push(Stata * S, int i);
int pop(Stata * S, int *i);

void InsertSort(int *a,int n);
void QSort(int *a,int left ,int right);
void main()
{
int i,a[10]={11,2,3,4,5,6,7,8,9,0};
Queue Q;
Stata S;
//初始化
init_Qu(&Q);
init_St(&S);

EnterQu(&Q,9);
OutQu(&Q,&i);
cout<<i<<endl;

push(&S,8);
pop(&S,&i);
cout<<i<<endl;
//插入排序
InsertSort(a,10);
//快排
QSort(a,0,9);
for (i=0;i<10;i++)
{
cout<<" "<<a[i];
}

}
int QuEmpty(Queue * Q)
{
if (Q->before==Q->tail)
{
return 1;
}
else
return 0;
}
int QuFull(Queue * Q)
{
if (Q->before==((Q->tail+1)%Max))
{
return 1;
}
return 0;
}
void init_Qu(Queue * Q)
{
Q->before=Q->tail=0;
}
int EnterQu(Queue * Q, int i)
{
if (!QuFull(Q))
{
Q->QuArray[Q->tail]=i;
Q->tail=(Q->tail+1)%Max;
return 1;
}
else
return 0;
}
int OutQu(Queue *Q,int *i)
{
if (!QuEmpty(Q))
{
*i=Q->QuArray[Q->before];
Q->before=(Q->before-1+Max)%Max;
return 1;
}
else
return 0;
}
void init_St(Stata * S)
{
S->top=S->base=0;
}
int StEmpty(Stata * S)
{
if (S->top==S->base)
{
return 1;
}
else
return 0;
}
int StFull(Stata *S)
{
if (S->top>=Max-1)
{
return 1;
}
else
return 0;
}
int push(Stata * S ,int i)
{
if (!StFull(S))
{
S->StArray[S->top]=i;
S->top++;
return 1;

}
else
return 0;
}
int pop(Stata * S,int *i)
{
if (!StEmpty(S))
{
S->top--;
*i=S->StArray[S->top];
return 1;
}
else
return 0;
}
void InsertSort(int *a,int n)//插入排序
{
int j,temp;
if (n<1)
{
return ;
}
for (int i=1;i<n;i++)
{
temp=a[i];
j=i;
while(temp<a[j-1] && j>0){
a[j]=a[j-1];
j--;
}
a[j]=temp;
}
}
void QSort(int a[],int left ,int right)
{
int pivot ,l ,r ,temp ;
l=left;
r=right;
pivot=a[(left+right)/2];
while (l<r)
{
while (a[l]<pivot)++l;
while(a[r]>pivot)--r;

if (l>=r)break;

temp=a[l];
a[l]=a[r];
a[r]=temp;

if(l!=pivot) --r;
if(r!=pivot) ++l;
}
if(l==r) l++;
if(left<r)QSort(a,left,l-1);
if(l<right)QSort(a,r+1,right);
}

typedef struct _Element_{
struct _Element_ * next;
void * data;
}Element;

int Push(Element ** stack,void * data)
{
Element * element;
element = (Element *)malloc(sizeof(Element));
if(!element)
return 0;
element->data = data;
element->next = *stack;
*stack = element;
return 1;
}

int Pop(Element ** stack,void ** data)
{
Element * element;
if(!(element = *stack))
return 0;
*data = element->data;
*stack = element->next;
free(element);
return 1;
}

int createStack(Element ** stack);
{
*stack = NULL;
return 1;
}

int deleteStack(Element ** stack);
{
Element * next;
while( *stack ){
next = (*stack)->next;
free(*stack);
*stack = next;
}
return 1;
}