C Language------data structure (stack operation, array implementation) __ data structure

Source: Internet
Author: User
/***************************************************** Copyright (C), 2014-2015, lighting Studio. 
Co., Ltd. File name:Author:Jerey_Jobs version:0.1 Date:Description:Funcion List: ****************************************
	/#include <stdio.h> #include <stdlib.h> #define MAX struct stack{int Stack_text[max];

int top;//array element (stack element) subscript};

typedef struct stack stack;

Enum Return_result{push_ok,push_no,full_ok,full_no,empty_ok,empty_no};
		void Is_malloc_ok (Stack *stack)//Whether to assign success {if (stack = NULL) {printf ("malloc error!\n"); Exit (-1);//End process} void Creat_stack (Stack **stack)/Allocate space {(*stack) = (stack*) malloc (sizeof (stack);/Allocate Space is_ma
LLOC_OK (*stack); } void Init_stack (Stack *stack)//Initialize {stack->top =-1;//top assignment is-1, position on stack top} int is_full (stack *stack)/check stack full {if (s)
	Tack->top = = Max) return FULL_OK;
else return full_no; int push_stack (stack *stack,int num)/into stack operation {if (is_full (stack) = = FULL_OK)//Stack full {printf ("thE stack is full!\n ");
	return push_no; }//Stack not full stack->top++; Top is-1 finished top is 0 stack->stack_text[stack->top] = num;
Value is written to return PUSH_OK;
	The int is_empty (stack* stack)//Determines whether the stack is empty {if (Stack->top = 1) {return EMPTY_OK;
return empty_no;
	   } int pop_stack (stack *stack)/out stack operation {if (is_empty (stack) = = EMPTY_OK)/stack Empty {printf ("stack is empty\n");
	return EMPTY_OK;
else//Stack not empty stack content return empty_no;
	int main () {stack* Stack;

	int i; Creat_stack (&stack);//Create Stack init_stack (stack)//stack initialization for (i = 0;i < max;i++)//Stack count {if (Push_stack + 1)
        = = PUSH_OK)//into the stack printf ("Successful!\n into the stack");
	else printf ("Stack failed!\n"); for (i = 0;i < max;i++)//out stack count {if (pop_stack (stack) = = empty_no)/out stack to determine whether stack is empty {printf ("num =%d\n",stack->
			Stack_text[stack->top]);
		stack->top--;//forward} else {printf ("Stack empty!\n");
} 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.