Implementation and operation of the stack (C language Implementation)

Source: Internet
Author: User

Definition of stacks
1, stack is a special kind of linear table
2, the stack can only operate at one end of the linear table
3, Top: Agree on one end of the operation
4, bottom of Stack (Bottom):, do not agree with one end of the operation


Here I made the sequence implementation of the stack and chained implementation. Respectively for example the following:

========================================= Gorgeous cutting line ==========================================================

The order of the stacks is implemented:

first of all, we have to make it clear that. A stack is essentially a linear table. is a special kind of linear table. It is only possible to operate at one end. Therefore, very many linear tables are very similar. Therefore, we are able to use the code in the implementation and operation of the sequential linear table previously written (C language implementation) directly to achieve the effect of code reuse (the code does not go back here).

Header file:

#ifndef _seqstack_h_#define _seqstack_h_typedef void seqstack; seqstack* seqstack_create (int capacity), void Seqstack_destroy (seqstack* stack), void Seqstack_clear (seqstack* stack); int Seqstack_push (seqstack* stack, void* item), void* Seqstack_pop (seqstack* stack), void* seqstack_top (seqstack* stack) ; int seqstack_size (seqstack* stack); int seqstack_capacity (seqstack* stack); #endif

Source file:

Stack. CPP: Defines the entry point of the console application. #include "stdafx.h" #include <malloc.h> #include <stdlib.h> #include "SeqStack.h" #include "SeqList.h" int    _tmain (int argc, _tchar* argv[]) {seqstack* stack = seqstack_create (20);    int a[10];        int i = 0;                for (i=0; i<10; i++) {A[i] = i;    Seqstack_push (Stack, a + i);    } printf ("Top:%d\n", * (int*) seqstack_top (stack));    printf ("Capacity:%d\n", seqstack_capacity (stack));        printf ("Length:%d\n", seqstack_size (stack));    while (seqstack_size (stack) > 0) {printf ("Pop:%d\n", * (int*) Seqstack_pop (stack)); } seqstack_destroy (stack); system ("pause"); return 0;} Create stack seqstack* seqstack_create (int capacity) {return seqlist_create (capacity);} destroys void Seqstack_destroy (seqstack* stack) {Seqlist_destroy (stack);} empty void Seqstack_clear (seqstack* stack) {seqlist_clear (stack);} Press Stack int Seqstack_push (seqstack* stack, void* item) {return Seqlist_insert (stack, item, Seqlist_length (stack));} Stack void* seqstack_pop (seqstack* stack) {return seqlist_delete (Stack, seqlist_length (stack)-1);} Get stack top void* seqstack_top (seqstack* stack) {return seqlist_get (Stack, seqlist_length (stack)-1);} stack size int seqstack_size (seqstack* stack) {return seqlist_length (stack);} Total size of the sequential stack int seqstack_capacity (seqstack* stack) {return seqlist_capacity (stack);}

Execution Result:

top:9capacity:20length:10pop:9pop:8pop:7pop:6pop:5pop:4pop:3pop:2pop:1pop:0 Please press the random key to continue ...

===================================== I am the cutting line ========================================================

The chain implementation of the stack:

The same stack of sequential implementation, the chain implementation of the stack is essentially the form of a single-linked list, but also only at the end of the operation, so we also use a code reuse method, detailed code please refer to: implementation and operation of the linked list (C language Implementation) .


Header file:

#ifndef _linkstack_h_#define _linkstack_h_typedef void linkstack; Linkstack* linkstack_create (); void Linkstack_destroy (linkstack* stack); void Linkstack_clear (linkstack* stack); int Linkstack_push (linkstack* stack, void* item); void* Linkstack_pop (linkstack* stack); void* linkstack_top (LinkStack* stack); int linkstack_size (linkstack* stack); #endif

Source file:

#include "stdafx.h" #include "LinkList.h" #include "LinkStack.h" #include <malloc.h> #include <stdlib.h>int    Main (int argc, char *argv[]) {linkstack* stack = linkstack_create ();    int a[10];        int i = 0;                for (i=0; i<10; i++) {A[i] = i;    Linkstack_push (Stack, a + i);    } printf ("Top:%d\n", * (int*) linkstack_top (stack));        printf ("Length:%d\n", linkstack_size (stack));    while (linkstack_size (stack) > 0) {printf ("Pop:%d\n", * (int*) Linkstack_pop (stack)); } linkstack_destroy (stack); system ("pause"); return 0;}    typedef struct _tag_linkstacknode{Linklistnode header; void* item;} tlinkstacknode;//Create linkstack* linkstack_create () {return linklist_create ();}    destroys void Linkstack_destroy (linkstack* stack) {linkstack_clear (stack); Linklist_destroy (stack);}    emptying void Linkstack_clear (linkstack* stack) {while (Linkstack_size (stack) > 0) {linkstack_pop (stack); }}//pressure stack int Linkstack_push (Linkstack* stack, void* item) {tlinkstacknode* node = (tlinkstacknode*) malloc (sizeof (Tlinkstacknode));        int ret = (node! = NULL) && (item! = NULL);                if (ret) {Node->item = Item;    ret = Linklist_insert (Stack, (linklistnode*) node, 0);    } if (!ret) {Free (node); } return ret;}    Stack void* linkstack_pop (linkstack* stack) {tlinkstacknode* node = (tlinkstacknode*) linklist_delete (stack, 0);        void* ret = NULL;                if (node! = NULL) {ret = node->item;    Free (node); } return ret;}    Get stack top void* linkstack_top (linkstack* stack) {tlinkstacknode* node = (tlinkstacknode*) linklist_get (stack, 0);        void* ret = NULL;    if (node! = NULL) {ret = node->item; } return ret;} Get the size of the stack int linkstack_size (linkstack* stack) {return linklist_length (stack);}


Execution Result:

top:9length:10pop:9pop:8pop:7pop:6pop:5pop:4pop:3pop:2pop:1pop:0 Please press the random key to continue ...



If there is a mistake, hope to point out ~



Implementation and operation of the stack (C language Implementation)

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.