Data Structures-linear tables-stacks

Source: Internet
Author: User

Stack: LIFO (LIFO) last in first out
A stack is a special linear table that operates only at one end of a linear table.
Top of Stack
Stack Bottom Bottom

Implementation method: Sequential structure implementation, linear structure implementation

Chained storage implementations

LinkStack.h

#ifndef _linkstack_h_#define_linkstack_h_typedefvoidLinkstack; Linkstack*linkstack_create ();voidLinkstack_destroy (linkstack*stack);voidLinkstack_clear (linkstack*stack);intLinkstack_push (linkstack* Stack,void*item);void* Linkstack_pop (linkstack*stack);void* Linkstack_top (linkstack*stack);intLinkstack_size (linkstack*stack);#endif

Linkstack.c

#include <malloc.h>#include"LinkStack.h"#include"LinkList.h"typedefstruct_tag_linkstacknode{Linklistnode Header; void*item;} Tlinkstacknode; Linkstack*linkstack_create () {returnlinklist_create ();}voidLinkstack_destroy (linkstack*stack)    {linkstack_clear (stack); Linklist_destroy (stack);}voidLinkstack_clear (linkstack*stack) {     while(Linkstack_size (Stack) >0) {linkstack_pop (stack); }}intLinkstack_push (linkstack* Stack,void*Item) {Tlinkstacknode* node = (tlinkstacknode*)malloc(sizeof(Tlinkstacknode)); intRET = (node! = NULL) && (Item! =NULL); if(ret) {node->item =item; RET= Linklist_insert (Stack, (linklistnode*) node,0); }        if( !ret) {         Free(node); }        returnret;}void* Linkstack_pop (linkstack*stack) {Tlinkstacknode* node = (tlinkstacknode*) linklist_delete (Stack,0); void* ret =NULL; if(Node! =NULL) {ret= node->item;  Free(node); }        returnret;}void* Linkstack_top (linkstack*stack) {Tlinkstacknode* node = (tlinkstacknode*) linklist_get (Stack,0); void* ret =NULL; if(Node! =NULL) {ret= node->item; }        returnret;}intLinkstack_size (linkstack*stack) {    returnlinklist_length (stack);}

Main.c

#include <stdio.h>#include<stdlib.h>#include"LinkStack.h"/*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/intMainintargcChar*argv[]) {Linkstack* Stack =linkstack_create (); inta[Ten]; inti =0;  for(i=0; i<Ten; 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); return 0;}

Sequential Storage implementations:

SeqStack.h

#ifndef _seqstack_h_#define_seqstack_h_typedefvoidSeqstack; Seqstack* Seqstack_create (intcapacity);voidSeqstack_destroy (seqstack*stack);voidSeqstack_clear (seqstack*stack);intSeqstack_push (seqstack* Stack,void*item);void* Seqstack_pop (seqstack*stack);void* Seqstack_top (seqstack*stack);intSeqstack_size (seqstack*stack);intSeqstack_capacity (seqstack*stack);#endif

Seqstack.c

#include"SeqStack.h"#include"SeqList.h"Seqstack* Seqstack_create (intcapacity) {    returnseqlist_create (capacity);}voidSeqstack_destroy (seqstack*stack) {Seqlist_destroy (stack);}voidSeqstack_clear (seqstack*stack) {seqlist_clear (stack);}intSeqstack_push (seqstack* Stack,void*Item) {    returnSeqlist_insert (Stack, item, seqlist_length (stack));}void* Seqstack_pop (seqstack*stack) {    returnSeqlist_delete (Stack, seqlist_length (stack)-1);}void* Seqstack_top (seqstack*stack) {    returnSeqlist_get (Stack, seqlist_length (stack)-1);}intSeqstack_size (seqstack*stack) {    returnseqlist_length (stack);}intSeqstack_capacity (seqstack*stack) {    returnseqlist_capacity (stack);}

Main.c

#include <stdio.h>#include<stdlib.h>#include"SeqStack.h"/*Run this program using the console Pauser or add your own getch, System ("pause") or input loop*/intMainintargcChar*argv[]) {Seqstack* Stack = seqstack_create ( -); inta[Ten]; inti =0;  for(i=0; i<Ten; 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); return 0;}

Data Structures-linear tables-stacks

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.