In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)05/31 Report--
This article mainly explains "how to implement sequential stack in C language". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Let's let the editor take you to learn "how to realize the sequential stack in C language".
Definition of sequential stack
First of all, let's take a brief look at the sequential stack. We know that according to the sequential storage or linked storage, it is divided into sequential table and single linked list. Similarly, according to the different storage methods, we divide the stack into sequential storage stack called sequential stack, chain storage stack called chain stack. What we're talking about is the sequential stack. In fact, with some knowledge of the previous linear table, it is easier for us to understand the operation of the stack.
Understanding of sequential stack
What's the problem? How do we define it? Usually we can use an array and variables that record the location of the elements at the top of the stack, which uses the integer variable Top to record the subscript value of the elements at the top of the stack. When Top==- 1, it indicates an empty stack. When top==MAXSIZE- 1, it indicates a full stack. All right, let's start implementing the sequential stack.
Preparatory work
1. Macro definition and its renaming
# define OK 1#define ERROR 0#define TRUE 1#define FALSE 0#define MAXSIZE 20 / * initial allocation of storage space * / typedef int Status; typedef int SElemType; / * the type of SElemType depends on the actual situation, and it is assumed to be int * /
two。 Structure (representation of the sequential stack)
/ * Sequential stack structure * / typedef struct {SElemType data [MAXSIZE]; int top; / * is used for top stack pointer * /} SqStack; implementation
1. Initialization
/ * construct an empty stack S * / Status InitStack (SqStack * S) {/ * S.data = (SElemType *) malloc (MAXSIZE*sizeof (SElemType)); * / S-> top=-1; return OK;}
two。 Empty
/ * set S to empty stack * / Status ClearStack (SqStack * S) {S-> top=-1; return OK;}
3. Determine whether it is empty or not
/ * return TRUE if stack S is empty, otherwise return FALSE * / Status StackEmpty (SqStack S) {if (S.top==-1) return TRUE; else return FALSE;}
4. Find the length
/ * returns the number of elements of S, that is, the length of the stack * / int StackLength (SqStack S) {return S.topical 1;}
5. Find the elements at the top of the stack
/ * if the stack is not empty, return the top element of S with e and return OK; otherwise return ERROR * / Status GetTop (SqStack S, SElemType* e) {if (S.top = =-1) {return ERROR;} else {* e = S.data [S.top]; return OK;}}
6. Enter the stack (to see if it is full)
/ * insert element e to the new stack top element * / Status Push (SqStack* S, SElemType e) {if (S-> top = = MAXSIZE-1) / * Stack full * / {return ERROR;} S-> top++; / * add a * / S-> data [S-> top] = e to the top pointer of the stack / * assign the newly inserted element to the top space of the stack * / return OK;}
7. Out of the stack (to determine whether it is empty)
/ * if the stack is not empty, delete the top element of S, return its value with e, and return OK Otherwise, return ERROR * / Status Pop (SqStack* S, SElemType* e) {if (S-> top = =-1) return ERROR; * e = S-> data [S-> top]; / * assign the stack top element to be deleted to e* / S-> top--; / * stack top pointer minus one * / return OK;}
8. Ergodic
/ * display / Status StackTraverse (SqStack S) {int i; i = 0; while (I) for each element in the stack from the bottom to the top of the stack
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.