Stack-C implementation for special linear tables

Source: Internet
Author: User

There are two interesting linear tables: Stack and queue ).

Stack is defined in this way. Its elements follow the principle of first-in-first-out, and there is only one portal, called the top stack. The action to go into the stack is push, and the action to go out of the stack is pop.

It is easy to implement because it is based on a linear table and has such a simple and clear definition.

 // 2013.3.6 // Lincoln // stack sequence storage structure # include 
  
    # define OK 1 # define error-1 # define true 1 # define false 0 # define maxlength 20 struct stack {int data [maxlength]; int top ;}; int push (struct stack * stack, int element) {If (Stack-> Top = MAXLENGTH-1) Return Error; int Index = ++ stack-> top; // top is auto-incrementing first and then assigned to index // int Index = stack-> top ++; // top is assigned to index first and then auto-incrementing. Note This. Stack-> data [Index] = element; Return OK;} int POP (struct stack * stack) {If (Stack-> Top =-1) return error; stack-> top --; Return OK;} void print (struct stack * stack) {If (Stack-> Top =-1) return; int size = stack-> top + 1; for (INT I = 0; I 
   
     data [% d] = % d \ n", I, stack-> data [I]) ;}} int main () {struct stack ={{ 0}, 0}; push (& stack, 30); push (& stack, 12); print (& stack ); pop (& stack); print (& stack); Return 0 ;}
   
  

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.