Loading...

堆栈操作复习

2024-12-03
0
-
- 分钟
|

堆栈操作复习

#include<iostream>using namespace std; #define max 20 // 顺序栈struct seqStack{    int data[max];    int top;}; // 置空栈seqStack* init_seqStack(){    seqStack *s = new seqStack;    s->top = -1;    return s;}  // 判空bool empty_seqStack(seqStack *s){    return s->top == -1;  // 如果栈空,返回 true}  // 入栈bool push_seqStack(seqStack *s, int x){    if(s->top == max - 1) return false;  // 栈满    s->data[++s->top] = x;    return true;  // 入栈成功} // 出栈bool pop_seqStack(seqStack *s, int &x){    if(empty_seqStack(s)) return false;  // 栈空    x = s->data[s->top--];  // 返回栈顶元素并更新栈顶指针    return true;} // 取栈顶元素bool top_seqStack(seqStack *s, int &x){    if(empty_seqStack(s)) return false;    x = s->data[s->top];    return true;} // 链栈typedef struct Node{    int data;    Node* next;}*stackNode; class linkStack{    stackNode H;  // 栈头指针public:    linkStack(){      // 初始化一个空节点作为头节点        H = NULL;    }     // 入栈    void push(int x){        Node* p = new Node();        p->data = x;        p->next = H;  // 插入栈顶        H = p;  // 更新栈头指针    }     // 出栈    bool pop(int &x){        if(this->H == NULL) return false;  // 栈空        Node* p = H;        H = H->next;        x = p->data;        delete p;  // 释放内存        return true;    }     // 判空    bool isEmpty() const {        return H == NULL;  // 如果栈头节点的下一个节点为空,栈为空    } }; // 数制转换void conversion(int N, int r){    int x;    linkStack S;    while(N != 0){        S.push(N % r);        N = N / r;    }    while(!S.isEmpty()){        S.pop(x);        cout << x;    }}// 队列class queue{   int data[max];  int rear,front; int num; // 循环队 public: void init_que(){        this->front = max-1;        this->rear = max-1; }   // 入队   bool in_queue(int x){       if(this->num == max){           return -1;      }       else{           this->rear = (this->rear+1) % max;          this->data[this->rear] = x;         this->num++;            return 1;       }   }   // 出队   bool out_queue(int &x){     if(this->num == 0){         return 0;       }else{          this->front = (this->front+1)%max;          x = this->data[this->front];            this->num--;            return 1;       }   }   // 判断队空 bool is_empty(){        return this->num == 0;  } }; // 链队列struct Qnode{    int data;   Qnode *next;};class lqueue{ Qnode *rear;    Qnode *front;public:    lqueue(){       Qnode* node = new Qnode();      this->front = node;     this->rear = node;      node->next = NULL;  }   // 入队   void in_lqueue(int x){      Qnode *p = new Qnode();     p->data = x;        p->next = NULL;     this->rear->next = p;       this->rear = p; }   // 判断队空 bool emp_queue(){       return this->front == this->rear;   }    // 出队操作    bool out_lqueue(int &x) {        if (this->emp_queue()) {            return false;  // 如果队列为空,返回false        } else {            Qnode *p = this->front->next; // 获取队列头部的第一个元素            x = p->data;                   // 将队头元素的值返回            this->front->next = p->next;   // 更新队头指针            if (this->front->next == nullptr) {  // 如果队列为空,更新队尾指针                this->rear = this->front;            }            delete p;  // 释放队头节点的内存            return true;        }    }}; int main(){    int a = 10;    cout << "10 转换为二进制是:";    conversion(a, 2);    cout << endl;    return 0;}

文章目录