Piggy's data structure auxiliary tutorial--3.2 stack and the chain stack in the queue

Source: Internet
Author: User

Piggy's data structure auxiliary tutorial--3.2 stack and the chain stack in the queue

tags (space delimited): Data structure

1. Introduction to this section:

Well, this section does not study the road map ha, because the stack we generally use is the sequential stack, link stack or incidentally mention it,
Stack because it is just the top of the stack to do the insert and delete operations, so a better way is to put the stack top on the head of a single-linked list, the top of the stack
The pointer is combined with the head pointer of the single-linked list so this section is just about the storage structure and basic operations of the chain stack!

2. The storage structure of the chain stack and

Storage Structure :

typedefstruct StackNode{    SElemType data;   //存放的数据    struct StackNode *next;}StackNode,*LinkStackPtr;typedefstruct LinkStack{    LinkStackPtr top;   //Top指针    int count;          //栈元素计数器}LinkStack;

From the point that can be found, the chain stack will not appear full of the situation ...

3. The code implementation of the basic operation of the chain stack

Code Implementation :

#include <Stdio.H>#defineStack_init_sizeTen Initial allocation of//storage space#defineOk1#defineERROR0#define TRUE 1#define FALSE 0typedef int STATUS; typedef int SELEMTYPE;//Storage structuretypedef struct stacknode{SelemtypeData;//Stored datastruct Stacknode*Next;} Stacknode,*linkstackptr;typedef struct linkstack{linkstackptr top;//top Pointerint count;//Stack element counter}linkstack;//1. Building an empty stackStatus Initstack (linkstack*s) {s -Top=(linkstackptr) malloc (sizeof (Stacknode));if(!S -TopreturnERROR; S -Top= NULL; S -Count= 0;returnOK;}//2. Placing the Stack emptyStatus Clearstack (linkstack*S) {linkstackptr p,q; P=S -Top while(p) {Q=P P=P -Next    Free (q); } S -Count= 0;returnOK; }//3. Determine if the stack is emptyStatus stackempty (Linkstack S) {returnS.Count== 0?TRUE:FALSE;}//4. Getting the number of elements in the stackint Stacklength (Linkstack S) {returnS.Count;}//5. Getting the top element of the stackStatus GetTop (linkstack*S,selemtype*e) {linkstackptr p;if(Stackempty (*S))returnERROR;*E=S -Top -Data; P=S -TopreturnOK;}//6. Inserting elements into the chain (stack)Status Pushstack (linkstack*S,selemtype e) {linkstackptr s=(linkstackptr) malloc (sizeof (Stacknode));if(!SreturnERROR; S -Data =E S -Next=S -Top S -Top=S S -Count++;returnOK;}//7. Delete stack top element * out of Stack (Status Popstack (linkstack*S,selemtype*e) {linkstackptr p;if(Stackempty (*S))returnERROR;*E=S -Top -Data; P=S -Top//Get stack top nodeS -Top=S -Top -Next//stack top pointer move down oneFree (p);//Release node PS -Count--;returnOK;}//8. Traversing StacksStatus Stacktraverse (Linkstack S) {linkstackptr p; P=S.Top while(p) {visit (P -Data); P=P -Next } printf ("\ n");returnOK; }//Define a method for printing elementsStatus visit (selemtype c) {printf ("%d", c);returnOK;}    int main () {int i,e;    Linkstack s; Initstack (&s); printf"Initialize the link stack, then insert 10 elements ~\n"); for (i= 1; I<= Ten; I++) {Pushstack (&S,i); } printf ("At this point the elements in the stack are: \ n");    Stacktraverse (s); printf"There are%d elements in this stack \ n", Stacklength (s)); Popstack (&S&e); printf"Out of the stack, the stack node is:%d\n", e); printf"At this point the elements in the stack are: \ n");    Stacktraverse (s); Clearstack (&s); printf"Empty the stack, there are%d elements in the stack!" \ n ", Stacklength (s));return 0;}

Operation result :

The code is relatively simple, it does not explain the ~

4. This section of the code download:

Https://github.com/coder-pig/Data-structure-auxiliary-tutorial/blob/master/Stack/Stack3.c

Piggy's data structure auxiliary tutorial--3.2 stack and the chain stack in the queue

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.