| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112 |
- #include <stdio.h>
- #include <stdlib.h>
- #include "linkstack.h"
-
- linkstack_t pkt_creat_linkstack(void)
- {
- linkstack_t s ;
- if ((s = (linkstack_t)malloc(sizeof(struct linkstack))) == NULL)
- {
- printf("malloc failed!\n");
- return NULL;
- }
-
- s->next = NULL;
- return s;
- }
-
- int pkt_empty_linkstack(linkstack_t s)
- {
- return s->next == NULL;
- }
-
- stack_frame_t pkt_get_linkstack_top(linkstack_t s)
- {
- if (pkt_empty_linkstack(s))
- {
- printf("linkstack is empty!\n");
- return NULL;
- }
-
- return &s->next->frame;
- }
-
- int pkt_linkstack_push(linkstack_t s, stack_frame_t frame)
- {
- PKT_ASSERT(s);
- PKT_ASSERT(frame);
- linkstack_t f;
- if ((f = (linkstack_t)malloc(sizeof(struct linkstack))) == NULL)
- {
- printf("malloc failed!\n");
- return -1;
- }
- f->frame = *frame;
- f->next = s->next;
- s->next = f;
- return 0;
- }
-
- struct stack_frame pkt_linkstack_pop(linkstack_t s)
- {
- struct stack_frame f = {0};
-
- if (pkt_empty_linkstack(s))
- {
- printf("linkstack is empty!\n");
- return f;
- }
-
- linkstack_t d = s->next;
- s->next = d->next;
- f = d->frame;
- free(d);
- return f;
- }
-
- int pkt_clear_linkstack(linkstack_t s)
- {
- linkstack_t d = s->next;
- while (d)
- {
- s->next = d->next;
- free(d);
- d = s->next;
- }
- return 0;
- }
-
- int pkt_free_linkstack(linkstack_t s) /* include free head */
- {
- linkstack_t d = s;
-
- while (d)
- {
- s = d->next;
- free(d);
- d = s;
- }
- return 0;
- }
-
-
- int main(void)
- {
- linkstack_t s = pkt_creat_linkstack();
-
- struct stack_frame frame;
-
- for (int i = 0; i < 10; i++)
- {
- frame.data = i;
- pkt_linkstack_push(s, &frame);
- }
-
- while (!pkt_empty_linkstack(s))
- {
- printf("%d\n", pkt_linkstack_pop(s).data);
- }
- pkt_free_linkstack(s);
- return 0;
- }
|