On the chain stack of data structure (IV.)

Source: Internet
Author: User

Stack of chain storage structure, we generally referred to as "chain stack." Because the single-linked list has a head pointer, and the stack-top pointer is also necessary, so we usually put the top of the stack on the head of a single-linked list, with the top of the stack in the head, the single-linked list of the more common head node lost significance. Usually for the chain stack, there is no need for the head node, there is basically no stack full situation, unless the memory is no longer in use of space. But for the empty stack, the original definition of the list is the head pointer to "empty", then the "Empty" chain stack is actually "top=null" time.

The stack push and stack pop operations are simple, with no loop operation, and the time Complexity is O (1), and the time complexity of the sequential stack is the same as the chain stack, and O (1). For spatial performance, sequential stacks need to determine a fixed length in advance, there may be a waste of memory space, it is convenient to access the location, and the link stack requires that each element has a pointer field, which also adds some memory overhead, but the length of the stack is unlimited, so if the use of the stack element changes unpredictable , sometimes very small, sometimes very large, then it is best to use a chain stack; Conversely, it is preferable to use a sequential stack if the element changes within a controllable range. For the operation of the chain stack, most of the same as the single-linked list, just in the insertion and deletion of a special, let's look at the operation of the chain stack, the actual operation of the source code is as follows:

1#include <stdio.h>2#include <stdlib.h>3 4 #defineOK 15 #defineERROR 06 #defineTRUE 17 #defineFALSE 08 9 #defineMAXSIZE 100/* Storage space Initial allocation */Ten  OnetypedefintStatus; AtypedefintSelemtype;/*the Selemtype type is based on the actual situation and is assumed to be an int*/ -  - /*Link Stack structure*/ thetypedefstructStacknode - { - selemtype data; -     structStacknode *Next; +}stacknode,*linkstackptr; -  +typedefstruct A { at linkstackptr top; -     intcount; - }linkstack; -  - Status Visit (selemtype c) - { inprintf"%d", c); -     returnOK; to } +  - /*constructs an empty stack s*/ theStatus Initstack (Linkstack *S) * {  $S->top = (linkstackptr)malloc(sizeof(Stacknode));Panax Notoginseng     if(! S->top) -         returnERROR; thes->top=NULL; +S->count=0; A      the     returnOK; + } -  $ /*set S to empty stack*/ $Status Clearstack (Linkstack *S) - {  - linkstackptr p,q; theP=s->top; -      while(P)Wuyi     {   theq=p; -P=p->Next; Wu          Free(q); -     }  AboutS->count=0; $      -     returnOK; - } -  A /*returns True if Stack S is an empty stack, otherwise false*/ + Status stackempty (linkstack S) the {  -     if(s.count==0) $         returnTRUE; the     Else the         returnFALSE; the } the  - /*returns the number of elements of S, that is, the length of the stack*/ in intstacklength (linkstack S) the {  the     returnS.count; About } the  the /*Insert Element e as the new top element of the stack*/ theStatus Push (Linkstack *s,selemtype e) + { -Linkstackptr s= (LINKSTACKPTR)malloc(sizeof(Stacknode));  theS->data=e;Bayis->next=s->top;/*assigns the current top element of the stack to the immediate successor of the new node.*/ thes->top=s;/*assigns a new node s value to the top of the stack pointer.*/ thes->count++; -      -     returnOK; the } the  the /*if the stack is not empty, delete the top element of S, return its value with E, and return OK;*/ theStatus Pop (Linkstack *s,selemtype *e) - {  the linkstackptr p; the     if(Stackempty (*S)) the         returnERROR;94*e=s->top->data; thep=s->top;/*assign the top node of the stack to p, see figure ③ .*/ thes->top=s->top->next;/*make the stack top pointer move down one point, referring to the back one node, see figure ④*/ the          Free(p);/*release node P*/        98s->count--; About      -     returnOK;101 }102 103 /*outputs each element of the stack from the top of the stack to the bottom of the stack*/104 Status stacktraverse (linkstack S) the {106 linkstackptr p;107p=S.top;108      while(P)109     { theVisit (p->data);111P=p->Next; the     }113printf"\ n"); the      the     returnOK; the }117 118 intMain ()119 { -     intj,e;121 Linkstack S;122         123     if(Initstack (&s) = =OK)124          for(j=1; j<=Ten; j + +) thePush (&s,j);126printf"1. The elements in the stack are:");127 Stacktraverse (s); -     129Pop (&s,&e); theprintf"2. Pop-up stack top element e=%d\n", e);131printf"3. After ejecting the top element of the stack e=%d, the stack length is%d\n", E,stacklength (s)); the     133Pop (&s,&e);134printf"4. Eject the Next top element of the stack e=%d\n", e);135printf"5. The elements in the stack are:");136 Stacktraverse (s);137     138Push (&s, -);139printf"6. After inserting the new stack top element e=%d, the stack length is%d\n", -, Stacklength (s)); $printf"7. The elements in the stack are:");141 Stacktraverse (s);142     143Clearstack (&s);144printf"8. After emptying the stack, the stack length is%d\n", Stacklength (s));145     146     return 0;147}

On the chain stack of data structure (IV.)

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.