Introduction to algorithm------------stack simple array implementation

Source: Internet
Author: User

stacks and queues are dynamic collections, and the entry and exit of elements is well-defined. The stack specifies that the element is advanced-out (FILO), and the queue specifies that the element is FIFO. Implementations of stacks and queues can be implemented using arrays and linked lists. In the standard module library STL has the specific application, may refer to http://www.cplusplus.com/reference/.

The basic operation of the stack consists of a stack push and a stack pop, a stack top pointer top, a pointer to the latest stack of elements, both the stack and the stack operation are from the top of the stack.

The basic operations of the queue include the queued enqueue and the dequeue, with the queue head head and the tail tail pointer. Elements are always out of the team, from the end of the team. In order to make reasonable use of space, we can use the array to realize the efficient utilization of the queue space.


Simple array implementation of the stack:

#include <iostream>using namespace std;struct stack{int *s;int stacksize;int top;};/ /init stackvoid init_stack (Stack *s) {s->stacksize = 100;s->s = (int*) malloc (sizeof (int) *s->stacksize);//s- >s = new int (); s->top =-1;} int Stack_empty (stack s) {     return ((0 = = s.stacksize)? 1:0);} void Push_stack (Stack *s, int x) {if (s->top = = s->stacksize) cout << "Up to Overflow" << endl;else{s-> Top++;s->s[s->top] = x;s->stacksize++;}} void Pop_stack (Stack *s) {if (0 = = s->stacksize) cout << "down to Overflow" << endl;else{s->top--;s->s tacksize--;}} int Top_stack (stack s) {return s.s[s.top];} int main () {stack s;init_stack (&s); for (int i = 0; i <; i++) {Push_stack (&s, i);} for (int i = 0; i < 5; i++) {Cout<<top_stack (s) << "";p op_stack (&s);}}

The simple implementation of the queue is next written. Study for a night, very tired very tired.

Introduction to algorithm------------stack simple array implementation

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.