Sequential storage of stacks-design and implementation-API implementations

Source: Internet
Author: User

Stack Basic Concepts
Stack is a special kind of linear table
Stacks can only operate at one end of a linear table
Top: One end of the allowed operation
Bottom of Stack (Bottom): One end of operation is not allowed

Common operations for Stacks
Creating stacks
Destroying stacks
Empty stack
Into the stack
Out of the stack
Get top of stack element
Get the size of the stack

Analysis of relationship between stack model and linked list model



Sequential storage design and implementation of stacks



seqlist.h//API declarations for sequential storage structure linear tables #ifndef  __my_seqlist_h__ #define __MY_SEQLIST_H__TYPEDEF void Seqlist;typedef void seqlistnode;//Linked list creates seqlist* seqlist_create (int capacity);//list destroys void Seqlist_destroy (seqlist* list);////linked list empty void Seqlist_clear (seqlist* list);//List length int seqlist_length (seqlist* list);//list capacity int seqlist_capacity (seqlist* list);//Linked list Inserts an element int Seqlist_insert (seqlist* list, seqlistnode* node, int pos) at a certain location;//Gets the list node of a location seqlistnode* seqlist_get ( seqlist* list, int pos);//delete node of a location seqlistnode* seqlist_delete (seqlist* list, int pos); #endif  //__my_seqlist_h__

seqlist.cpp//stack API implementation of sequential storage structure # include <iostream> #include <cstdio> #include "seqlist.h" using namespace std ; typedef struct _TAG_SEQLIST{INT capacity;int length;int **node;} tseqlist;//Linked list creates seqlist* seqlist_create (int capacity) {int ret =-1; Tseqlist *tmp = Null;tmp = (tseqlist *) malloc (sizeof (tseqlist)), if (tmp = = NULL) {ret = 1;printf ("Function seqlist_create ( ) err:%d\n ", ret); return NULL;} memset (tmp, 0, sizeof (tseqlist)), tmp->capacity = Capacity;tmp->length = 0;tmp->node = (int * *) malloc (sizeof ( void *) * capacity), if (Tmp->node = = NULL) {ret = 2;printf ("function seqlist_create () err:%d\n", ret); return NULL;} memset (tmp->node, 0, sizeof (void *) * capacity); return TMP; The linked list creates an int seqlist_create2 (int capacity, seqlist**handle) {intret = 0;  Tseqlist*tmp = Null;tmp = (tseqlist *) malloc (sizeof (tseqlist)), if (tmp = = NULL) {ret = 1;printf ("func seqlist_create2 () Err :%d \ n ", ret); return ret;} memset (tmp, 0, sizeof (tseqlist)); tmp->capacity = Capacity;tmp->length = 0;tmP->node = (int * *) malloc (sizeof (void *) * capacity); if (Tmp->node = = NULL) {ret = 2;printf ("func seqlist_create2 () ma Lloc err:%d \ n ", ret); return ret;} *handle = Tmp;return ret;} Linked list destroys void Seqlist_destroy (seqlist* list) {if (list = = NULL) {return;} Tseqlist *tmp = (tseqlist *) list;if (Tmp->node! = NULL) {free (tmp->node);} Free (TMP); return;} List empty void Seqlist_clear (seqlist* list) {if (list = = NULL) {return;} Tseqlist *tmp = (tseqlist *) list;tmp->length = 0;memset (tmp->node, 0, sizeof (tmp->node)); return;} List length int seqlist_length (seqlist* list) {if (list = = NULL) {return-1;} Tseqlist *tmp = (tseqlist *) List;return tmp->length;} List capacity int seqlist_capacity (seqlist* list) {if (list = = NULL) {return-1;} Tseqlist *tmp = (tseqlist *) List;return tmp->capacity;}  The linked list inserts an element int Seqlist_insert (seqlist* list, seqlistnode* node, int pos) in one place {if (list = = NULL | | node = NULL | | POS < 0) {return-1;} Tseqlist *tlist = (tseqlist *) list;//if full if (Tlist->length >= tlist->capacity) {return-2;} If the position of the POS exceeds length, that is, there are some positions in the middle empty if (pos > Tlist->length) {pos = tlist->length;} for (int i = tlist->length; i > pos; i) {tlist->node[i] = tlist->node[i-1];} Tlist->node[pos] = (int *) Node;++tlist->length;return 0;} Gets the list node of a location seqlistnode* seqlist_get (seqlist* list, int pos) {tseqlist *tlist = (tseqlist *) list;if (list = = NULL | | pos < 0 | | POS >= tlist->length) {return NULL;} Seqlistnode *tlistnode = Null;tlistnode = (int *) Tlist->node[pos];return Tlistnode;} Delete a node of a location seqlistnode* seqlist_delete (seqlist* list, int pos) {tseqlist *tlist = (tseqlist *) list; Seqlistnode *tlistnode = null;if (list = = NULL | | pos < 0 | | pos >= tlist->length) {return null;} Tlistnode = tlist->node[pos];for (int i = pos + 1; i < tlist->length; ++i) {tlist->node[i-1] = Tlist->nod E[i];} --tlist->length; Don't forget the length minus one return tlistnode;}

Stack API declaration for seqstack.h//sequential storage structure #ifndef __seqstack_h__#define __seqstack_h__typedef void seqstack;//Create Stack seqstack* Seqstack_create (int capacity);//Destroy Stack void* Seqstack_destroy (seqstack* stack);//Empty Stack void* seqstack_clear (seqstack* stack);//element into the stack int seqstack_push (seqstack* stack, void* item);//popup stack top element void* seqstack_pop (seqstack* stack);//get stack top element void * Seqstack_top (seqstack* stack);//Gets the size of the stack int seqstack_size (seqstack* stack);//Gets the stack's capacity int seqstack_capacity (seqstack* stack); #endif

API implementation of seqstack.cpp//sequential storage structure stack//call up previously written order list Api#include <cstdio> #include "seqlist.h" #include "seqstack.h"// Creating a stack is equivalent to creating a linear table seqstack* seqstack_create (int capacity) {return seqlist_create (capacity);} Destroys the stack, which is equivalent to destroying the linked list void* Seqstack_destroy (seqstack* stack) {Seqlist_destroy (stack); return NULL;} Emptying the stack is equivalent to emptying the list void* seqstack_clear (seqstack* stack) {seqlist_clear (stack); return NULL;} element into the stack, equivalent to adding an element int Seqstack_push (seqstack* stack, void* item) at the tail of a linear table (array) {return Seqlist_insert (stack, item, Seqlist_ Length (Stack));} POPs the top element of the stack, which is equivalent to removing the element from the tail of the linear table void* Seqstack_pop (seqstack* stack) {return seqlist_delete (Stack, seqlist_length (stack)-1);} Gets the top element of the stack, which is equivalent to getting the trailing element of the list void* seqstack_top (seqstack* stack) {return seqlist_get (Stack, seqlist_length (stack)-1);} Gets the size of the stack, which is equivalent to the length of the linked list int seqstack_size (seqstack* stack) {return seqlist_length (stack);} Gets the capacity of the stack int seqstack_capacity (seqstack* stack) {return seqlist_capacity (stack);}

Test procedure for main.cpp//sequential structure stack # include <stdio.h> #include "seqstack.h" void Play () {int i = 0; Seqstack *stack = Null;int a[10];for (i = 0; i <; ++i) {A[i] = i + 1;} stack = seqstack_create (20);//enter stack for (int i = 0; i < 5; ++i) {Seqstack_push (stack, &a[i]);} printf ("len:%d \ n", seqstack_size (Stack));p rintf ("capacity:%d \ n", seqstack_capacity (Stack));p rintf ("top:%d \ n", * ( int *) seqstack_top (stack));//element out of Stack while (Seqstack_size (stack)) {printf ("%d", * (int *) Seqstack_pop (stack));} Seqstack_destroy (stack); return;} int main () {play (); return 0;}

Project Code Details: Github

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Sequential storage of stacks-design and implementation-API implementations

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.