Introduction to algorithms Chapter 1 10th stack and queue

Source: Internet
Author: User
Tags stack pop
I. Concept 1. Stack (1) stack implements the first-in-first-out operation. In the Array Implementation of the stack, the top pointer of the stack points to the top element of the stack. During the insertion, the pointer is modified before insertion, and the top element of the stack is taken before the pointer is modified. When top [s] is 0, the stack is hollow. (2) array stack structure: int top; // stack top pointer int * s]; // point to Stack Array (3) stack-empty (s) implemented on the stack // judge whether the stack is empty push (S, x) // Press X to the top of the stack POP (s) // extract and return the top element of the stack 2. queue (1) queues implement first-in-first-out operations. In the Array Implementation of the queue, the header pointer of the queue points to the first element of the queue. When the queue is deleted, the first element of the queue is taken and then the pointer is modified. The tail pointer of the queue points to the next element of the last element, insert the pointer before modifying it. When top [s] is 0, the stack is empty. (2) Structure of the array queue: int tail; // the end of the queue, pointing to the latest incoming element int head; // The queue header, pointing to the first-out element int length; // The queue length int * s; // point to the array Queue (3) the operation enqueue (Q, x) implemented on the queue) // insert X to the end of the queue dequeue (q) // retrieve the first element of the queue and return code 2.
// 10.1 stack and queue # include <iostream> # include <stdio. h ># include <string> using namespace STD; ********************* * ********************************/struct stack {int top; // stack top pointer int * s; // array stack (INT size): Top (0) {S = new int [size + 1];} // ~ Stack () {Delete [] s ;}}; void print (stack s) {int I; // sequential output from the bottom of the stack to the top of the stack for (I = 1; I <= S. top; I ++) cout <S. s [I] <''; cout <Endl;} // check whether a stack is empty bool stack_empty (stack & S) {If (S. top = 0) return true; elsereturn false;} // inbound stack void push (stack & S, int X) {S. top ++; S. s [S. top] = x;} // The output stack int POP (stack & S) {If (stack_empty (s) {cout <"underflow" <Endl; exit (0);} else {S. top --; return S. s [S. top + 1] ;}} ********************* * *********************************/struct queue {int tail; // queue head pointer int head; // queue end pointer int length; // queue length int * s; // array Queue (INT size): tail (1 ), head (1), length (size) {S = new int [size + 1] ;}}; // output void print (queue q) from the queue header to the end of the queue) {int I; If (Q. tail> = Q. head) {for (I = Q. head; I <q. tail; I ++) cout <q. s [I] <''; cout <Endl;} // the end of the queue may be in front of the queue header else {for (I = Q. head; I <= Q. length; I ++) cout <q. s [I] <''; for (I = 1; I <q. tail; I ++) cout <q. s [I] <''; cout <Endl ;}// determine whether the queue is empty bool queue_empty (queue q) {If (Q. tail = Q. head) return 1; return 0;} // enter the queue void enqueue (queue & Q, int X) {q. s [q. tail] = x; If (Q. tail = Q. length) Q. tail = 1; else Q. tail ++;} // output queue int dequeue (queue & Q) {int x = Q. s [q. head]; If (Q. head = Q. length) Q. head = 1; else Q. head ++; return X ;}
Iii. Exercises

10.1-1

4 1

10.1-2

Use the two ends of the array as the starting point of the two stacks to expand to the middle. If the sum of elements in the two stacks does not exceed n, the two stacks will not meet each other.

See introduction to Algorithms
10.1-2 use an array to implement two stacks

10.1-3

3 8

10.1-4

// Code: can handle overflow and underflow queues // Input Queue void enqueue2 (queue & Q, int X) {int t; // overflow if (Q. tail = Q. length) t = 1; else t = Q. tail + 1; if (t = Q. head) {cout <"error: overflow" <Endl; return;} else {q. s [q. tail] = x; q. tail = T ;}/// output queue int dequeue2 (queue & Q) {// underflow if (Q. head = Q. tail) {cout <"error: underflow" <Endl; Return-1;} int x = Q. s [q. head]; If (Q. head = Q. length) Q. head = 1; else Q. head ++; return X ;}

10.1-5

See introduction to Algorithms
10.1-5 dual-end queue

10.1-6

Inbound queue:

If Stack B is not empty, the values in stack B are popped up and pushed to stack. Then, press the value of the queue to stack a O (n)

Outbound queue:

If Stack A is not empty, the values in stack a are popped up and pushed to stack B. Then, the values at the top position of stack B are output to the stack and the value O (n) is returned)

// Use two stacks to simulate a queue // output queue. Stack 1 is from the bottom of the stack to the top of the stack, and stack 2 is from the top of the stack to the bottom of the stack void print (stack S1, stack S2) {int I; for (I = 1; I <= s1.top; I ++) cout <s1.s [I] <''; for (I = s2.top; I >= 1; I --) cout <s2.s [I] <''; cout <Endl ;}// determine whether the queue is empty bool queue_empty (stack & S1, stack & S2) {If (stack_empty (S1) & stack_empty (S2) return 1; return 0 ;}// enter the queue void enqueue (stack & S1, stack & S2, int X) {// if Stack B is not empty, the values in stack B are popped up and pushed to stack a while (! Stack_empty (S2) Push (S1, pop (S2); // the value to be pushed into the queue is pushed to stack a (S1, x );} // output queue int dequeue (stack & S1, stack & S2) {// if Stack A is not empty, the values in stack a are popped up and pushed to stack B while (! Stack_empty (S1) Push (S2, pop (S1); // extract the value at the top of stack B from Stack B and return the value return POP (S2 );}

 

10.1-7

Stack entry:

One of the two queues is an empty queue. The value is added to this empty queue, and the values of the other non-empty queue are retrieved and added to this queue in sequence. O (N)

Out Stack: directly extract O (1) from non-empty queues)

// Simulate the stack with two queues // determine whether the stack is empty bool stack3_empty (queue Q1, queue q2) {If (queue_empty (Q1) & queue_empty (Q2) return 1; return 0;} // output stack void print (queue Q1, queue q2) {If (! Queue_empty (Q1) print (Q1); If (! Queue_empty (Q2) print (Q2);} // void push (queue & Q1, queue & Q2, int X) {// two queues, one of them is an empty queue if (queue_empty (Q1) {// enter the value into this empty queue enqueue (Q1, X ); // extract the values of another non-empty queue and add them to this queue in sequence while (! Queue_empty (Q2) enqueue (Q1, dequeue (Q2);} else // likewise {enqueue (Q2, x); While (! Queue_empty (Q1) enqueue (Q2, dequeue (Q1) ;}// output stack int POP (queue & Q1, queue & q2) {// retrieve if (! Queue_empty (Q1) return dequeue (Q1); If (! Queue_empty (Q2) return dequeue (Q2); cout <"error: underflow" <Endl; Return-1 ;}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.