408 After-school code exercises for kingly data structure (twenty-four)

Continue to create, accelerate growth! This is the second day of my participation in the "Nuggets Daily New Plan · June Update Challenge", click to view the details of the event

foreword

It is planned to update the implementation of all after-school code exercises in the 23 King's Road data structure. Although the exams are generally written in pseudo-code, I have implemented them all because of obsessive-compulsive disorder. The warehouse is here.

The code is written in C++, all of which can be compiled and run, including violent solutions and optimal solutions as much as possible (if the exam time is not enough, you can directly attack a violent one, and you can get more than half of the points).

Continuous update, current update progress:

  • Linear Table 14/14
  • Linked List 25/25
  • Stack 3/3
  • Queue 4/4
  • Application of stacks and queues
  • ...

for reference only! There will be some grammar that the exam is not allowed to write, and there may be some mistakes.

3.2.6, 1

image.png

  • Team empty:Q.front == Q.rear && Q.tag == 0
  • Team full:Q.front == Q.rear && Q.tag == 1
  • Because only entering the team will cause the team to be full, set the tag to 1 when entering the team, and similarly set the tag to 0 when leaving the team.
bool enqueue(Queue &Q, int x) {
  if (Q.front == Q.rear && Q.tag == 1) {
    cout << "队列满" << endl;
    return false;
  }

  Q.data[Q.rear] = x;
  Q.rear = (Q.rear + 1) % maxsize;
  Q.tag = 1;
  return true;
}

bool dequeue(Queue &Q, int &x) {
  if (Q.front == Q.rear && Q.tag == 0) {
    cout << "队列空" << endl;
    return false;
  }

  x = Q.data[Q.front];
  Q.front = (Q.front + 1) % maxsize;
  Q.tag = 0;
  return true;
}
复制代码

2

image.png

  • Elements in the team are dequeued and pushed into the stack one by one, and then they are all pushed into the stack and then pushed out of the stack into the queue one by one.
  • A very simple question is to examine the nature of "first in, first out" and "last in first out"
void reverseQueue(Queue &Q, Stack &S) {
  int x;
  while (!isEmpty(Q)) {
    dequeue(Q, x);
    push(S, x);
  }

  while (!isEmpty(S)) {
    pop(S, x);
    enqueue(Q, x);
  }
}
复制代码

3

image.png

  • The stack of S1 is used to enter the queue. If S1 is full and S2 is empty, the elements of S1 can be inserted into S2.
  • The pop of S2 is used as a queue. If S2 is empty, insert all elements of S1 into S2
  • Empty only needs to determine whether both stacks are empty
bool Enqueue(Stack &S1, Stack &S2, int x) {
  if (!StackOverflow(S1)) {
    Push(S1, x);
    return true;
  }

  if (StackOverflow(S1) && !StackEmpty(S2)) {
    cout << "队列满" << endl;
    return false;
  }

  // S1满且S2空,先将S1中的元素全部入S2,再入S1
  while (!StackEmpty(S1)) {
    int tmp;
    Pop(S1, tmp);
    Push(S2, tmp);
  }
  Push(S1, x);
  return true;
}

bool Dequeue(Stack &S1, Stack &S2, int &x) {
  if (!StackEmpty(S2)) {
    Pop(S2, x);
    return true;
  } 
  
  if (StackEmpty(S1)) {
    cout << "队列空" << endl;
    return false;
  }
  
  // S2为空且S1不为空
  while (!StackEmpty(S1)) {
    int tmp;
    Pop(S1, tmp);
    Push(S2, tmp);
  }
  Pop(S2, x);
  return true;
}

bool QueueEmpty(Stack S1, Stack S2) {
  return StackEmpty(S1) && StackEmpty(S2);
}
复制代码

4

image.png

  • For example, the chain storage structure should be selected, and it is a single circular linked list containing head and tail pointers (head front, tail rear)
  • empty conditionfront == rear
  • full team conditionfront == rear -> next
  • Follow the principle of "full judgment, empty judgment", just write pseudo code

Guess you like

Origin juejin.im/post/7104657708556484616