链栈(link stack)

/* Author: ACb0y Date: 2010-6-14 Description: link stack */ #include <stdio.h> #include <malloc.h> typedef int elemtype; typedef struct node { elemtype data; node * pnext; }link_stack; /* FUNCTION link_stack_create DESCRIPTION create the link stack and return the pointer of it PARAMETERS void RETURNS p_lik_s: the pointer of generator link stack */ link_stack * link_stack_create() { link_stack * p_lik_s = (node *)malloc(sizeof(node)); p_lik_s->pnext = NULL; return p_lik_s; } /* FUNCTION link_stack_empty DESCRIPTION judge the link stack if empty PARAMETERS p_lik_s: the pointef link stack RETURNS 1: link stack is empty 0: link stack isn't empty */ int link_stack_empty(link_stack * p_lik_s) { if (p_lik_s->pnext == NULL) { return 1; } else { return 0; } } /* FUNCTION link_stack_push DESCRIPTION push the element in the link stack PARAMETERS p_lik_s: the pointer of link stack value: the element to push into the stack RETURNS flag: the flag whether the push operation successful or not */ int link_stack_push(link_stack * p_lik_s, elemtype value) { int flag; node * pnode = (node *)malloc(sizeof(node)); if (pnode == NULL) { flag = 0; } else { pnode->data = value; pnode->pnext = p_lik_s->pnext; p_lik_s->pnext = pnode; flag = 1; } return flag; } /* FUNCTION link_stack_pop DESCRIPTION pop a element in the link stack PARAMETERS p_lik_s: the pointer of link_stack RETURNS void */ void link_stack_pop(link_stack * p_lik_s) { node * ptemp = p_lik_s->pnext; p_lik_s->pnext = p_lik_s->pnext->pnext; free(ptemp); } /* FUNCTION link_stack_top DESCRIPTION get the top element of link stack PARAMETERS p_lik_s: the pointer of link_stack RETURNS p_lik_s->pnext->data: the top element of link stack */ elemtype link_stack_top(link_stack * p_lik_s) { return p_lik_s->pnext->data; } int main() { link_stack * p_lik_s = link_stack_create(); int i; for (i = 0; i < 10; i++) { link_stack_push(p_lik_s, i + 1); } while (!link_stack_empty(p_lik_s)) { printf("%d ", link_stack_top(p_lik_s)); link_stack_pop(p_lik_s); } printf("/n"); return 0; }

你可能感兴趣的:(链栈(link stack))