Stack is a common data structure, although it has the top and bottom of the stack, but it can only be operated from one end (insert or delete), which is an "advanced after-out" mode of operation. The data in the stack is called the stack (push), and the data from the stack is called the Stack (POp). For example, the stacking order is 1, 2, 3, 4, 5, the order of the stack is 5, 4, 3, 2, 1 (only consider the case of a one-off stack).
The stack is divided into sequential stacks and linked list stacks in the same way as storage. The sequential stack is based on an array implementation, so the sequential stack stores the memory of the data is continuous, when the stack is created to specify the size of the stack, so that the use of memory is not very efficient. Chained stacks are implemented with linked lists, where the storage addresses of the elements are discontinuous and are dynamically allocated memory. The sequential stack may appear to be full and empty when used, because the chain stack is based on the list design, so there will be no full-stack (also empty).
Sequential Stacks:
Sequential Stack #include <stdio.h> #define SIZE 10typedef struct Stack{int data[SIZE]; Int top;} stack;//initialization Stack Void init_stack (stack* st) {st->top=-1;} Determines whether the stack is empty int is_stack_empty (stack* st) {if (st->top==-1) return 1;return 0;} Determines whether the stack is full Int is_stack_full (stack* st) {if (st->top==size-1) {return 1;} return 0;} Gets the stack top value Int get_top_value (stack* st) {if (Is_stack_empty (ST)) {printf ("stack is empty!\n"); Return -1;} Return st->data[st->top];} Enter Stack Void push (stack* st,int _data) {if (Is_stack_full (ST)) {printf ("stack is full!\n"); return ;} St->top++;st->data[st->top]=_data;} Out of Stack Int pop (stack* st) {if (Is_stack_empty (ST)) {printf ("stack is empty!\n"); return -1;} return st->data[st->top--];} Int main (int argc, char const *argv[]) {Stack st; init_ Stack (&st); int i; for (i=0;i<10;i++) { push (&st,i); } push (&st,23); push (&st,23); printf ("topvalue:%d\n", Get_top_value (&st)); for (i=0;i<12;i++) { printf ("%d\n", Pop (& ST)); } return 0;}
chained stack:
#include <stdio.h> #include <stdlib.h>//define nodes typedef struct node{int data; Struct node* next;} Node;typedef struct stack{node* top;} stack;//initialization Stack Void init_stack (stack* st) {st->top=null;} Determines whether the stack is empty int is_stack_empty (stack* st) {if (st->top==null) {return 1;} return 0;} Gets the stack top value Int get_topvalue (stack* st) {if (Is_stack_empty (ST)) {printf ("stack is empty! \ n "); return -1;} Return st->top->data;} Stack Void push (stack* st,int _data) {node* newnode= (node*) malloc (1*sizeof (Node)); newnode- >data=_data;newnode->next=st->top;st->top=newnode;} Out of Stack Int pop (stack* st) {if (Is_stack_empty (ST)) {printf ("stack is empty! \ n "); return ;} Node* temp=st->top;int retvalue=st->top->data;st->top=st->top->next;free (temp); Return retvalue;} Int main (int argc, char const *argv[]) {Stack st;init_stack (&st); Int i;for (i=0;i<16;i++) {push (&st,i+1);} printf ("%d\n", Get_topvalue (&st)), for (i=0;i<17;i++) {printf ("%d\t", Pop (&st));} printf ("\ n"); return 0;}
This article is from the "June Feng Eureka" blog, please be sure to keep this source http://10274409.blog.51cto.com/10264409/1745822
Implementation of sequential stack and linked list stack