| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 8029 人关注过本帖
标题:已知Q是一个非空队列,S是一个空栈。仅用队列和栈的操作编写一个算法,将队 ...
只看楼主 加入收藏
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
结帖率:100%
收藏
 问题点数:0 回复次数:3 
已知Q是一个非空队列,S是一个空栈。仅用队列和栈的操作编写一个算法,将队列Q中的所有元素逆置。
程序有些问题,帮忙找下
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
//队列的定义
struct QueueRecord;
typedef struct QueueRecord *Queue;
#define MinQueueSize 5
typedef  char ElementType ;
struct QueueRecord
{
     int Capacity;
     int Front;
     int Rear;
     int Size;
     ElementType *Array;
};

void MakeEmpty(Queue Q)
{
Q->Size=0;
Q->Front=1;
Q->Rear=0;
}

int IsEmpty(Queue Q    )
{ return Q->Size==0;
}

int IsFull(Queue Q)
{ return Q->Size==Q->Capacity;
}


static int Succ(int Value, Queue Q)
{
     if(++Value ==Q->Capacity )
       Value=0 ;
     return Value;
}

void Enqueue(ElementType X, Queue Q)
{
       if(IsFull (Q ) )
         printf("Full queue");
       else
{
       Q->Size++                 ;//队列大小加1
  Q->Rear=Succ(Q->Rear,Q);
     Q->Array[Q->Rear]=X                   ;//将元素X插入队列中
}
}



ElementType Dequeue(Queue Q)
{ ElementType c2;
       if(IsEmpty (Q) )
         {printf("Empty");
         exit(0);}
       Q->Size--;//队列大小减一
  if(Q->Front<=Q->Capacity-1)
   {
   c2=Q->Array[Q->Front];
   Q->Array[Q->Front]=Q->Array[Q->Front++];
   }
   else c2=Q->Array[Q->Rear];
return c2;
}

Queue   CreateQueue(int MaxElements )
         {
             Queue Q;

/* 1 */        if(MaxElements<MinQueueSize)
/* 2 */          { printf("Queue size is too small");
                   exit(0);
                 }
/* 3 */           Q=(Queue)malloc(sizeof(struct QueueRecord ) );
/* 4 */           if(Q==NULL)
/* 5 */             { printf("Out of space!!!");
                      exit(0);
                 }
/* 6 */           Q->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(Q->Array==NULL)
/* 8 */                { printf("Out of space!!");
                            exit(0);
                 }
/* 9 */           Q->Capacity=MaxElements;
/*10*/           MakeEmpty(Q);
/*11*/           return Q;
}


//栈定义
struct StackRecord;
typedef struct StackRecord *Stack;
typedef   char ElementType;
#define EmptyTOS -1
#define MinStackSize 5
   
struct StackRecord
{
int Capacity;
int TopOfStack;
ElementType *Array;
};
void MakeEmpty(Stack S)
{
S->TopOfStack=EmptyTOS;
}
int IsFull(Stack S)
 { return S->TopOfStack==S->Capacity;
 }
int IsEmpty(Stack S)
 { return S->TopOfStack==EmptyTOS;
 }
Stack   CreateStack(int MaxElements )
 {
             Stack S;

/* 1 */        if(MaxElements<MinStackSize)
/* 2 */           {printf("Stack size is too small");
                 exit(0);
                 }
/* 3 */           S=(Stack)malloc(sizeof(struct StackRecord ) );
/* 4 */           if(S==NULL)
/* 5 */             { printf("Out of space!!l");
                        exit(0);
                 }
/* 6 */           S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(S->Array==NULL)
/* 8 */               { printf("Out of space!!l");
                           exit(0);
                 }
/* 9 */           S->Capacity=MaxElements;
/*10*/           MakeEmpty(S);
/*11*/           return S;
}
void   DisposeStack(Stack S)
{
      if(S!=NULL)
     {
         free(S->Array);
         free(S);
      }
}
void Push(ElementType X, Stack S)
{
     if( IsFull(S) )
{ printf("Full stack");
exit(0);  }
     else
  S ->Array[++S->TopOfStack]=X;//将元素X放入栈中
}
ElementType Top(Stack S)
{
     if(!IsFull(S))
       return S->Array[S-> TopOfStack];//返回栈顶元素
    /* Return value used to avoid warning */
}
void Pop(Stack S)
{
if(IsEmpty(S) )
  { printf("Empty stack");
exit(0);
                }
else
  S->TopOfStack--;
}
ElementType TopAndPop(Stack S )
{
if(!IsEmpty(S) )
   return S->Array[S->TopOfStack--];
 
}

//已知Q是一个非空队列,S是一个空栈。仅用队列和栈的操作编写一个算法,将队列Q中的所有元素逆置。
void Invert(Queue Q,Stack S,int n)//这个子函数好像错在哪里了?
/*借用栈将队列逆置*/
{
    char c;ElementType c1,c2;int i;
    for(i=1;i<=n;i++)
    {c1=Q->Array[Q->Front];
    Push(c1,S);
    Dequeue(Q);}
    for(i=1;i<=n;i++)
    c2=TopAndPop(S);
    Enqueue(c2,Q);

}
void main(void)
{
        char c1,c2;
Queue Q;
Stack S;
printf("请输入队列大小:(n>=5)\n");
int i,MAX;
scanf("%d",&MAX);

getchar();
Q=CreateQueue(MAX);//建立一个大小MAX的队列
printf("\n");
printf("输入\n");
for(i=1;i<=MAX;i++)
{scanf("%c",&c1);getchar();
Enqueue(c1,Q);//将元素c1插入队列中
}
for(i=0;i<MAX;i++)
    {
       c2=Dequeue(Q);
       printf("%c ",c2);
    }
Invert(Q,S,MAX);
for(i=0;i<MAX;i++)
    {
       c2=Dequeue(Q);
       printf("%c ",c2);
    }
}
搜索更多相关主题的帖子: 元素 算法 队列 编写 
2009-12-17 14:33
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
搞定了!
2009-12-17 15:25
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
自己搞定了!呵呵!大家看看
#include<malloc.h> /* malloc()等 */
#include<stdio.h> /* EOF(=^Z或F6),NULL */
#include<stdlib.h> /* atoi() */
#include<process.h> /* exit() */
typedef  char ElementType ;
//队列的定义
struct QueueRecord;
typedef struct QueueRecord *Queue;
#define MinQueueSize 5
struct QueueRecord
{
     int Capacity;
     int Front;
     int Rear;
     int Size;
     ElementType *Array;
};

void MakeEmpty(Queue Q)
{
Q->Size=0;
Q->Front=1;
Q->Rear=0;
}

int IsEmpty(Queue Q    )
{ return Q->Size==0;
}

int IsFull(Queue Q)
{ return Q->Size==Q->Capacity;
}


static int Succ(int Value, Queue Q)
{
     if(++Value ==Q->Capacity )
       Value=0 ;
     return Value;
}

void Enqueue(ElementType X, Queue Q)
{
       if(IsFull (Q ) )
         printf("Full queue");
       else
{
       Q->Size++                 ;//队列大小加1
  Q->Rear=Succ(Q->Rear,Q);
     Q->Array[Q->Rear]=X                   ;//将元素X插入队列中
}
}



ElementType Dequeue(Queue Q)
{
    ElementType c2;
       if(IsEmpty (Q) )
         {printf("Empty");
         exit(0);}
       Q->Size--;//队列大小减一
  if(Q->Front<=Q->Capacity-1)
   {
   c2=Q->Array[Q->Front];
   Q->Array[Q->Front]=Q->Array[Q->Front++];
   }
   else c2=Q->Array[Q->Rear];
   return c2;
}

Queue   CreateQueue(int MaxElements )
{
         Queue Q;

/* 1 */  if(MaxElements<MinQueueSize)
/* 2 */  {
                printf("Queue size is too small");
                exit(0);
}
/* 3 */          Q=(Queue)malloc(sizeof(struct QueueRecord ) );
/* 4 */          if(Q==NULL)
/* 5 */          {
                      printf("Out of space!!!");
                      exit(0);
                 }
/* 6 */           Q->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);
/* 7 */           if(Q->Array==NULL)
/* 8 */                {
                            printf("Out of space!!");
                            exit(0);
}
/* 9 */           Q->Capacity=MaxElements;
/*10*/           MakeEmpty(Q);
/*11*/           return Q;
}

//栈定义
struct StackRecord;
typedef struct StackRecord *Stack;
#define EmptyTOS -1
#define MinStackSize 5
   
struct StackRecord
{
   int Capacity;
   int TopOfStack;
   ElementType *Array;
};

void MakeEmpty(Stack S)
{
   S->TopOfStack=EmptyTOS;
}

int IsFull(Stack S)
{
    S->TopOfStack==S->Capacity;return 0;
}

int IsEmpty(Stack S)
{
    return S->TopOfStack==EmptyTOS;
}

Stack   CreateStack(int MaxElements )
 {
             Stack S;

/* 1 */        if(MaxElements<MinStackSize)
/* 2 */           {printf("Stack size is too small");
                 exit(0);
                 }
/* 3 */           S=(Stack)malloc(sizeof(struct StackRecord ) );//为栈分配空间
/* 4 */           if(S==NULL)
/* 5 */             { printf("Out of space!!l");
                        exit(0);
                 }
/* 6 */           S->Array=(ElementType *)malloc(sizeof(ElementType)*MaxElements);//为数组分配空间
/* 7 */           if(S->Array==NULL)
/* 8 */               { printf("Out of space!!l");
                           exit(0);
                 }
/* 9 */           S->Capacity=MaxElements;
/*10*/           MakeEmpty(S);
/*11*/           return S;
}

void Push(ElementType X, Stack S)
{
     if( IsFull(S) )
     {
         printf("Full stack");
         exit(0);  
     }
     else
         S ->Array[++S->TopOfStack]=X;//将元素X放入栈中
}

ElementType TopAndPop(Stack S )
{
   if(!IsEmpty(S) )
   return S->Array[S->TopOfStack--];
 
}

//已知Q是一个非空队列,S是一个空栈。仅用队列和栈的操作编写一个算法,将队列Q中的所有元素逆置。
void Invert(Queue Q,Stack S,int n)
/*借用栈将队列逆置*/
{
    char c;ElementType c1,c2,c3;int i,j;
    S=CreateStack(n);
    for(i=1;i<=n;i++)
    {
        c1=Dequeue(Q);
        Push(c1,S);
    }//将队列中的数据入栈
    printf("队列逆置顺序:\n");
    for(j=1;j<=n;j++)
    {
        c2=TopAndPop(S);
        Enqueue(c2,Q);
        c3=Dequeue(Q);
        printf("%c ",c3);
    }//数据出栈进入队列输出
    printf("\n");

}
void main(void)
{
        char c1,c2;
        Queue Q1,Q2;
        Stack S;
        printf("请输入队列大小:(n>=5)\n");
        int i,MAX;
        scanf("%d",&MAX);
        getchar();
        Q1=CreateQueue(MAX);
        Q2=CreateQueue(MAX);//建立一个大小MAX的队列
        printf("\n");
        printf("输入\n");
        for(i=1;i<=MAX;i++)
        {
            scanf("%c",&c1);getchar();
            Enqueue(c1,Q1);Enqueue(c1,Q2);//将元素c1插入队列中
        }
        printf("队列原始顺序:\n");
        for(i=0;i<MAX;i++)
        {
            c2=Dequeue(Q1);
            printf("%c ",c2);
        }
        printf("\n");
        Invert(Q2,S,MAX);//利用栈逆置队列
}
2009-12-17 15:26
crazywen2009
Rank: 1
等 级:新手上路
帖 子:16
专家分:5
注 册:2009-10-16
收藏
得分:0 
一开始忘了为栈开辟空间在Invert函数那里,以及没有考虑到输出队列是把队列都清空了,所以出现了那么多问题,不过现在完成了!
2009-12-17 15:28
快速回复:已知Q是一个非空队列,S是一个空栈。仅用队列和栈的操作编写一个算法, ...
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.015923 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved