⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 stackqueue.cpp

📁 文件中是我编写的两个数据结构
💻 CPP
字号:
/************************************************************************/
/*                        StackQueue.CPP                                */
/************************************************************************/

#ifndef __STACKQUEUE_CPP__
#define __STACKQUEUE_CPP__
#ifndef __STACKQUEUE_H__
   #include "StackQueue.h"
#endif   
#endif
#include <Stdio.h>
//////////////////////////////////////////////////////////////////////////
//Stack Order
//////////////////////////////////////////////////////////////////////////
template<class T>
void InitStack(StackType<T> *S)
{
   S->top=-1;
}

template<class T>
void Push(StackType<T> *S,T x)
{
  if(S->top==MaxSize)  printf("stack top out!\n");
  else
  {
    S->top++;
    S->Stack[S->top]=x;
  }
}

template<class T>
void Pop(StackType<T> *S)
{
   if(S->top==-1)  printf("stack bottom out!\n");
   else S->top--;
}

template<class T>
T GetTop(StackType<T> *S)
{
   if(S->top==-1)
   {
     printf("stack empty!\n"); 
     return T(0);
   }  
   else return (S->Stack[S->top]);
}

template<class T>
int Empty(StackType<T> *S)
{
   if(S->top==-1) return (1);
   else return (0);
}

template<class T>
void Display(StackType<T> *S)
{
  int i;
  printf("Stack Elements:");
  for(i=S->top;i>=0;i--)
     printf("%c ",S->Stack[i]);
  printf("\n");
}
//////////////////////////////////////////////////////////////////////////
//Stack List
//////////////////////////////////////////////////////////////////////////

template<class T>
void InitStack(StackLType<T> **S)
{
   *S=NULL;
}

template<class T>
void Push(StackLType<T> **S,T x)
{
  StackLType<T> *q;
  q=new StackLType<T>;
  q->Data=x;
  q->next=*S;
  *S=q;
}

template<class T>
void Pop(StackLType<T> **S)
{
   StackLType<T> *q=*S; 
   if(q==NULL) printf("stack bottom out!\n");
   else
   {
     q=*S;
     *S=q->next;
     delete q;
   }
      
}

template<class T>
T GetTop(StackLType<T> **S)
{
   if(*S==NULL)
   {
     printf("stack empty!\n"); 
     return T(0); 
   } 
   else return ((*S)->Data);
}

template<class T>
int Empty(StackLType<T> **S)
{
   if(*S==NULL) return (1);
   else return (0);
}

template<class T>
void Display(StackLType<T> **S)
{
  StackLType<T> *q=*S;
  printf("Stack Elements:");
  while(q!=NULL)
  {
    
     printf("%c ",q->Data);
     q=q->next;
  }
  printf("\n");
}
//////////////////////////////////////////////////////////////////////////
//Queue Order
//////////////////////////////////////////////////////////////////////////
template<class T>
void InitQueue(QueueType<T> *Q)
{
   Q->front=Q->rear=-1;
}

template<class T>
void Enter(QueueType<T> *Q , T x)
{
   if(Q->rear==MaxSize) printf("queue top out!\n");
   else
   {
      Q->rear++;
      Q->Queue[Q->rear]=x;
   }
}

template<class T>
void Delete(QueueType<T> *Q)
{
  if(Q->front==Q->rear)
     printf("queue empty!\n");
  else
     Q->front++;
}

template<class T>
T  GetHead(QueueType<T> *Q)
{
  if(Q->front==Q->rear)
  {
     printf("queue empty!\n");
     return T(0);
  }
  else
    return (Q->Queue[Q->front+1]);   
}

template<class T>
int Empty(QueueType<T> *Q)
{
   if(Q->front==Q->rear)  return (1);
   else   return (0);
}

template<class T>
void Display(QueueType<T> *Q)
{
   int i;
   printf("queue elements :");
   for(i=Q->front+1;i<=Q->rear;i++)
      printf("%c ",Q->Queue[i]);
   printf("\n");
}

//////////////////////////////////////////////////////////////////////////
//Queue List
//////////////////////////////////////////////////////////////////////////
template<class T>
void InitQueue(QueueLType<T> **Q)
{
   QueueLType<T> *q=new QueueLType<T>;
   q->front=q->rear=NULL;
   *Q=q;
//   *Q=new QueueLType<T>;
//   (*Q)->front=(*Q)->rear=NULL;
}

template<class T>
void Enter(QueueLType<T> *Q , T x)
{
   Qnode<T> *s=new Qnode<T>;
   s->Data=x;
   s->next=NULL;

   if(Q->rear==NULL) 
     Q->front=Q->rear=s;
   else
   {
      Q->rear->next=s;
      Q->rear=s;
   }
}

template<class T>
void Delete(QueueLType<T> *Q)
{
  Qnode<T> *t;
  if(Q->front==NULL)
     printf("queue empty!\n");
  else if(Q->front==Q->rear)
  {
     t=Q->front;
     Q->front=Q->rear=NULL;
  }
  else
  {
     t=Q->front;
     Q->front=Q->front->next;
  }
  delete t;
}

template<class T>
T  GetHead(QueueLType<T> *Q)
{
  if(Q->front==Q->rear)
  {
     printf("queue empty!\n");
     return T(0);
  }
  else
    return (Q->front->Data);   
}

template<class T>
int Empty(QueueLType<T> *Q)
{
   if(Q->front==Q->rear)  return (1);
   else   return (0);
}

template<class T>
void Display(QueueLType<T> *Q)
{
   Qnode<T> *p=Q->front;
   printf("queue elements :");
   while (p!=NULL)
   {
      printf("%c ",p->Data);
      p=p->next;
   }
      
   printf("\n");
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -