Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct list_t list_t;
- struct list_t {
- int data;
- list_t *link;
- };
- list_t *new_list(int data) {
- list_t *new = (list_t *) malloc(sizeof(list_t));
- new->data = data;
- new->link = NULL;
- return new;
- }
- list_t *top = NULL;
- void push(int data) {
- list_t *new = new_list(data);
- if (!top) {
- top = new;
- return;
- }
- new->link = top;
- top = new;
- }
- int pop(void) {
- int data;
- if (!top) {
- fprintf(stderr, "can't pop\n");
- return -1;
- }
- data = top->data;
- top = top->link;
- return data;
- }
- list_t *head = NULL, *tail = NULL;
- void enqueue(int data) {
- list_t *new = new_list(data);
- if (!head) head = new;
- if (!tail) tail = new;
- head->link = new;
- head = new;
- }
- int dequeue(void) {
- int data;
- if (!tail) {
- fprintf(stderr, "can't dequeue\n");
- return -1;
- }
- data = tail->data;
- tail = tail->link;
- return data;
- }
- int main(void) {
- int i;
- for (i = 0; i < 5; i++) push(i);
- for (i = 0; i < 6; i++) printf("%d, ", pop());
- puts("");
- for (i = 0; i < 5; i++) enqueue(i);
- for (i = 0; i < 6; i++) printf("%d, ", dequeue());
- puts("");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement