栈和队列的基本操作实现及其应用

本文通过具体的代码实例介绍了顺序栈、链栈、循环队列和链队列的实现,并提供了一个将十进制数转换为二进制数的算法。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

顺序栈

#include<iostream.h>

class A

{

int top;

int size;

int *Array;

public:

A(int MaxSize);

~A();

bool Push(const int& item);

bool Pop();

bool Peek()const;

int IsEmpty()const{ return top==-1; } 

    int IsFull()const { return top==size-1; } 

};

A::A(int MaxSize)

{

size=MaxSize;

Array=new int[MaxSize];

top=-1;

}

A::~A()

{  

   delete[] Array;

}

bool A::Push(const int& item)

{  

    if(IsFull())

{       

      cout<<"栈已满"<<endl;

      return false;

    }

    top+=1;            

    Array[top]=item;

    return true;

}

 

bool A::Pop()

{   

    if(IsEmpty())

{    

       cout<<"栈已空"<<endl;

       return false;

    }

    int item;         

    item=Array[top];

    top-=1;

    cout<<"弹出数据为:"<<item<<endl;

    return true;

}

 

bool A::Peek()const

{    

    if(IsEmpty())

{          

      cout<<"栈已空"<<endl;

      return false;

    }

    int item;              

    item=Array[top];

    cout<<"栈顶元素为:"<<item<<endl;

    return true;

}

int main()

{

    int size;

    cout<<"请输入要创建的栈的大小:"<<endl;

    cin>>size;

    Shunxu one(size);

    while(true){

      cout<<"请选择:"<<endl;

      cout<<"1.插入 2.删除 3.存取 4.退出"<<endl;

      int i;

      cin>>i;

      if(i==1)

  {

          cout<<"请输入要插入的数据:"<<endl;

          int item;

          cin>>item;

          one.Push(item);

      }

      else if(i==2)

  {   

          one.Pop();

      }

      else if(i==3)

  {            

          one.Peek();

      }

      else               

          break;

    }

  return 0;

}

 

 

链栈

#include<iostream.h>

struct Node

{

int data;

Node *next;

};

class Lian

{

Node *top;

public:

Lian()

{ top=NULL; }

~Lian();

void Push(int x);

int Pop();

int GetTop(){ if(top!=NULL) return top->data;}

int Empty()

{

if(top==NULL)

return 1;

else return 0; } //栈为空?

};

Lian::~Lian()

{  

   Node *p;

   while(top!=NULL)

   {

   p=top;

   top=top->next;

   delete p;

   }

}

 

void Lian::Push(int x)

{

Node *s=new Node;

s->data=x;

s->next=top;

top=s;

}

int Lian::Pop()

{

Node *p;

int x;

if(top==NULL)

cout<<"栈为空"<<endl;

else

{

x=top->data;

p=top;

top=p->next;

delete p;

return x;

}

}

 

int main()

{

Lian s;

s.Push(5);

s.Push(6);

s.Push(1);

cout<<s.GetTop()<<endl;

cout<<s.Pop()<<endl;

cout<<s.Pop()<<endl;

cout<<s.GetTop()<<endl;

return 0;

}

 

 

循环队列

#include<iostream.h>

#include<string.h>

const int QueueSize=100;

class Queue

{

int front, rear;

int data[QueueSize];

public:

Queue(){ front=rear=QueueSize-1; }

~Queue(){}

void EnQueue();

void DeQueue();

int GetQueue();

int Empty()

{

if(front==rear)

return 1;

else return 0; }

};

 

void Queue::EnQueue()

{

int x;

if((rear+1)%QueueSize==front)

throw "上溢";

int s;

do

{

cout<<"请输入一个数:"<<endl;

cin>>x;

rear=(rear+1)%QueueSize;

data[rear]=x;

cout<<"是否要继续:是的话输入1,否则输入0"<<endl;

cin>>s;

}

while(s==1);

}

 

void Queue::DeQueue()

{

int s=1;

do

{

if(rear==front)

throw"空队列";

front=(front+1)%QueueSize;

cout<<data[front]<<endl;

cout<<"是否要继续:是的话输入1,否则输入0"<<endl;

cin>>s;

}

while(s==1);

}

 

int Queue::GetQueue()

{

if(rear==front)

throw"空队列";

int i=(front+1)%QueueSize;

return data[i];

}

 

int main()

{

Queue cir;

cout<<"入队列操作:"<<endl;

try{ cir.EnQueue();}

catch(char* s)

{ cout<<s<<endl; }

cout<<"出队列操作:"<<endl;

try

{ cir.DeQueue(); }

catch(char* b)

{ cout<<b<<endl; }

cout<<"获取队头元素数据:"<<endl;

try

{ cout<<cir.GetQueue()<<endl; }

catch(char* s)

{ cout<<s<<endl; }

cout<<"判断队列是否为空"<<endl;

if(cir.Empty())

{ cout<<"该队列为空!"<<endl; }

else

cout<<"该队列不为空"<<endl;

cout<<"测试结束"<<endl;

return 0;

}

 

 

链队列

#ifndef LinkQueue_H

#define LinkQueue_H

template<class DataType>

struct Node

{

DataType data;

Node<DataType> *next;

};

 

template<class DataType>

class LinkQueue

{

public:

LinkQueue();                

~LinkQueue();

void EnQueue(DataType x);     

DataType DeQueue();            

DataType GetQueue();        

int Empty();

private:

Node<DataType> *front, *rear;

};

#endif

 

#include"LinkQueue.h"

template<class DataType>

LinkQueue<DataType>::LinkQueue()

{

Node<DataType> *s=NULL;

s=new Node<DataType>;

s->next=NULL;

front=rear=s;

}

 

template< class DataType >

LinkQueue<DataType>::~LinkQueue()

{

Node<DataType> *p=NULL;

while(front!=NULL)

{

p=front->next;

delete front;

front=p;

}

}

 

template< class DataType >

void LinkQueue<DataType>::EnQueue(DataType x)

{

Node<DataType> *s=NULL;

s=new Node<DataType>;

s->data=x;

s->next=NULL;

rear->next=s; rear=s;

}

 

template< class DataType >

DataType LinkQueue<DataType>::DeQueue()

{

Node<DataType> *p=NULL;

int x;

if(rear==front)throw"下溢";

p=front->next;

x=p->data;

front->next=p->next;

if(p->next==NULL) rear=front;

delete p;

return x;

}

 

template< class DataType >

DataType LinkQueue<DataType>::GetQueue()

{

if(front!=rear)

return front->next->data;

}

 

template< class DataType >

int LinkQueue<DataType>::Empty()

{

if(front==rear)

return 1;

else

return 0;

}

 

#include<iostream.h>

#include "LinkQueue.cpp"

 

void main()

{

LinkQueue<int>Q;

if(Q.Empty())

cout<<"队列为空"<<endl;

else

cout<<"队列非空"<<endl;

cout<<"元素10和15执行入队操作:"<<endl;

try

{

Q.EnQueue(10);

Q.EnQueue(15);

}

catch(char* wrong)

{

cout<<wrong<<endl;

}

cout<<"查看队头元素:"<<endl;

cout<<Q.GetQueue()<<endl;

cout<<"执行出队操作:"<<endl;

try

{

Q.DeQueue();

}

catch(char* wrong)

{

cout<<wrong<<endl;

}

cout<<"查看队头元素:"<<endl;

cout<<Q.GetQueue()<<endl;

}

 

 

设计算法并写出代码,实现将十进制数转换成2进制数。

#include<stdio.h>

#include<malloc.h>

#define ERROR 0

#define OK 1

#define STACK_INT_SIZE 10  /*存储空间初始分配量*/

#define STACKINCREMENT 5   /*存储空间分配增量*/

typedef int ElemType;      /*定义元素的类型*/

typedef struct

{

ElemType *base;

ElemType *top;

int stacksize;         /*当前已分配的存储空间*/

}SqStack;

 

int push(SqStack *S,ElemType e);  /*入栈*/

int Pop(SqStack *S,ElemType *e);  /*出栈*/

void PrintStack(SqStack *S);      /*出栈并输出栈中元素*/

 

int Push(SqStack *S,ElemType e)

{

if(S->top-S->base==STACK_INT_SIZE)

return 0;

    *(++S->top)=e;

    return 1;

}

 

int Pop(SqStack *S,ElemType *e)

{

if(S->top==S->base)

return 0;

*e=*S->top;

S->top--;

return 1;

}

 

void Conversion(SqStack *S,ElemType e)

{

while(e/2)

{

Push(S,e%2);

e/=2;

}

Push(S,e);

}

 

void PrintStack(SqStack *S)

{

ElemType e;

while(Pop(S,&e))

printf("%d",e);

printf("\n");

}

 

int main()

{

int num;

SqStack *S;

S=(SqStack *)malloc(sizeof(SqStack));

S->base=(ElemType *)malloc(STACK_INT_SIZE *sizeof(ElemType));

if(!S->base) return ERROR;

S->top=S->base;

S->stacksize=STACK_INT_SIZE;

printf("请输入要转换的数字:\n");

scanf("%d",&num);

Conversion(S,num);

PrintStack(S);

return 0;

}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值