Note: The main purpose of this article is to record your learning process and facilitate your communication. For reprint, please indicate from:
Http://blog.csdn.net/ab198604
In general, data storage methods need to be considered during program design. Different Data Storage Methods Determine their corresponding data retrieval methods. Generally, the storage structure of data retrieval is stack and queue. Next we will briefly introduce some basic knowledge about the storage structure of the stack.
I. Stack
Stack is a kind of data storage structure (LIFO) based on the advanced and later stages. It retrieves elements in the opposite order of storage elements. The so-called advanced post-release operation stores and deletes data elements, which means that the last stored element will be deleted first. For example, we want to store ", 2, 1" into the "stack", from the bottom of the stack to the top, which are 5, 4, 3, 2, 1. When deleted, the order is 1, 2, 3, 4, 5 ".
Let's not talk about the theory. How to implement the "stack" data structure? In fact, there are many methods to implement stack, and sequential linear storage structures and chained non-linear storage structures can be used. The sequential storage structure is like an array. The chain storage structure, as its name implies, uses the linked list storage method. Using the linked list method to implement stack is not only simple, but also can make the stack have the characteristics of polymorphism. This is because, in addition to some operations of the stack itself, it is a chain table, which has the same attributes as the chain table. Therefore, you can also use linked list operations. Therefore, this article uses the linked list method to implement the stack.
Ii. stack structure and Interface Definition
Stack is another form of linked list, so the stack structure definition can be encapsulated on the basis of the linked list. As follows:
/** Filename: Stack. H * Author: zhm * Date: 2012-01-05 */# ifndef stack_h # define stack_h # include <stdlib. h> # include "list. H "typedef list stack; // redefinition of the list type
The structure definition and interface of the linked list are described in "one-way linked list structure of Data Structure Learning". We will not describe it here. If you need it, you can log on to this address to access the relevant content:
Http://blog.csdn.net/ab198604/article/details/8253405
The preceding typedef list stack statement defines the stack structure. The following are Stack-related operations:
/* public interface */#define stack_init list_init#define stack_destroy list_destroyint stack_push(Stack *stack, const void *data);int stack_pop(Stack *stack, void **data);#define stack_peek(stack) ((stack)->head == NULL ? NULL : (stack)->head->data)#define stack_size list_size
It mainly includes stack initialization, stack destruction, pressure stack operations, and inbound stack operations. The initialization, destruction, and acquisition of stack elements are further encapsulated by the # define statement on the basis of the original linked list interface.
Iii. Implementation Details of stack Interfaces
The real implementation is the operation of the Pressure stack and the inbound stack, which can be achieved by calling the linked list interface. See the following code:
/* * filename: stack.c * author: zhm * date: 2012-01-05 */#include <stdlib.h>#include "list.h"#include "stack.h"/* stack_push */int stack_push(Stack *stack, const void *data){ /* push the data onto the stack */ return list_ins_next(stack, NULL, data);}/* stack_pop */int stack_pop(Stack *stack, void **data){ /* pop the data off the stack */ return list_rem_next(stack, NULL, data);}
Iv. Examples of stack applications
See the following code:
/* * filename: main.c * author:zhm * date: 2013-01-05 */#include <string.h>#include <stdlib.h>#include <stdio.h>#include "list.h"#include "stack.h"/* destroy */void destroy(void *data){ printf("in destroy\n"); free(data); return;}/* main */int main(int argc, char **argv){ Stack stack; int *int_ptr = NULL; int ret = 0; int i; stack_init(&stack, destroy); for(i = 0; i < 5; i++ ) { int_ptr = (int *)malloc(sizeof(int)); if( int_ptr == NULL ) return -1; *int_ptr = i; printf("push the data: %d\n",i); ret = stack_push(&stack, (void *)int_ptr); if( ret != 0 ) return -1; } printf("size of the stack is : %d\n", stack_size(&stack)); //pop the data from top to the bottom for(i = stack_size(&stack); i > 0; i-- ) { int_ptr = NULL; stack_pop(&stack, (void **)&int_ptr); printf("i = %d, pop the data = %d\n",i,*int_ptr); free(int_ptr); } printf("after pop size of the stack is :%d\n", stack_size(&stack)); return 0;}
The functions implemented by the preceding main () function are to push data: 0, 1, 2, 3, and 4 into the stack respectively, and then bring one of them up from the top of the stack. After the program is compiled and run, the results are as follows: