"Algorithmic Refinement _c Language description" stack _ Stack Interface definition

Source: Internet
Author: User

We can think of the stack as a tennis cone. When the ball is placed in the barrel, the ball is discharged upward from the bottom of the barrel to the mouth, and when the ball is taken from the barrel, the ball is taken out of the mouth until the end of the ball is taken out. And we want to get the ball at the bottom of the barrel, then we have to take out all the balls on the bottom of that ball.

A notable feature of the stack is that it stores and deletes elements in a similar first-in, first-out (LIFO) manner . This means that the last element stored in the stack will be deleted first. In the computer, to store the elements in the stack, "push" the element, to delete the elements in the stack, the "pop-up" element (see Figure 1). Sometimes you can get some information about an element by examining the top element of the stack, rather than actually deleting it.


interface definitions for stacks

Stack_init
void Stack_init (Stack *stack,void (*destroy) (void *data));
return value : none
Description : Initializes the stack specified by the stack.
The initialization function must be invoked before other operations are performed on the stack. The parameter destroy is a function pointer that frees the dynamically allocated memory space by calling Stack_destroy. For example, if a stack contains data that dynamically allocates memory with malloc, then when the stack is destroyed, destroy calls free to release the memory space. When a structured data contains several data members that dynamically allocate memory, destroy should point to a user-defined function to free the memory space of each dynamically assigned data member and structure itself. If you do not need to release the data from the stack, then destroy should point to null.
complexity : O (1)

Stack_destroy
void Stack_destroy (stack *stack);
return value : none
description : Destroys the stack specified by stack.
No other action is allowed after the call to Stack_destroy, unless the stack_init is called again. Stack_destroy deletes all the elements in the stack, and if the Stack_init argument destroy is NOT NULL, the Stack_destroy is called once each time an element is removed.
complexity : O (n), n is the number of elements in the stack.

Stack_push
int Stack_push (stack *stack,const void *data);
return value : returns 0 if the element succeeds in the stack, otherwise returns-1.
Description : Presses an element into the stack specified by the stack.
The new element contains a pointer to data, so the memory referenced by Daga is valid as long as the element still exists on the stack. The data-related storage space will be managed by the caller of the function.
complexity : O (1)

Stack_pop
int Stack_pop (stack *stack,void **data);
return value : returns 0 if the element's stack succeeds, or returns-1.
description : POPs an element from the stack specified by the stack.
When you return, data points to the datastore stored in the pop-up element. The data-related storage space will be managed by the caller of the function.
complexity : O (n), n is the number of elements in the stack.

Stack_peek
void *stack_peek (const stack *stack);
return value : The data stored in the top element of the stack, or null if the stack is empty.
description : Gets the macro that holds the data stored in the top element of the stack specified by stacks.
complexity : O (1)

Stack_size
int stack_size (const stack *stack);
return value : The number of elements in the stack.
description : Gets the macro for the number of elements in the stack specified by the stack.
complexity : O (1)

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.