(C language) data structure --- queue, stack

  1 /*
  2  * FILE: test.c
  3  * ============
  4  */
  5
  6 #include "QUEUE_linkedlist.h"
  7 #include "STACK_linkedlist.h"
  8
  9 #include <stdio.h>
 10
 11 int main(void)
 12 {
 13         int i;
 14
 15         for(i=0; i<10; i++)
 16                 QUEUEput(i);
 17         printf("QUEUE: FIFO\n");
 18         for(i=0; i<10; i++)
 19                 printf("%d ", QUEUEget(i));
 20         printf("\n");
 21
 22         for(i=0; i<10; i++)
 23                 STACKpush(i);
 24         printf("STACK: LIFO\n");
 25         for(i=0; i<10; i++)
 26                 printf("%d ", STACKpop(i));
 27         printf("\n");
 28         return 0;
 29 }

Queue queue: put in the queue, get out of the pair

  1 /*
  2  * FILE: QUEUE_linkedlist.c
  3  * ========================
  4 * queue queue
  5 * Enqueue put: put at the end of the linked list, and update the pointer tail pointing to the end of the linked list
  6 * Dequeue get: return and delete the head node of the linked list, and update the pointer head to the head of the linked list
  7  */
  8
  9 #include "QUEUE_linkedlist.h"
 10
 11 // static limits the scope of global variables
 12 static struct node *head = NULL, *tail = NULL;
 13
 14 // static limits the scope of the function and only affects this file
 15 static struct node *NEW(int data)
 16 {
 17         struct node *new = (struct node *)malloc(sizeof(struct node));
 18         new->data = data;
 19         new->next = NULL;
 20         return new;
 21 }
 22
 23 int QUEUEget()
 24 {
 25         int res = head->data;
 26         struct node *p = head;
 27         head = head->next;
 28         free(p);
 29         return res;
 30 }
 31
 32 void QUEUEput(int data)
 33 {
 34         if(head == NULL)
 35         {
 36                 head = tail = NEW(data);
 37                 return;
 38         }
 39         tail->next = NEW(data);
 40         tail = tail->next;
 41 }
 42
 43 void QUEUEempty()
 44 {
 45         struct node *p =head;
 46         while(p != NULL)
 47         {
 48                 struct node *temp = p;
 49                 p = p->next;
 50                 free(temp);
 51         }
 52         head = tail = NULL;
 53 }
  1 /*
  2  * FILE: QUEUE_linkedlist.h
  3  * ========================
  4  */
  5
  6 #include <stdlib.h>
  7
  8 struct node{
  9         int data;
 10         struct node *next;
 11 };
 12
 13 int QUEUEget();
 14 void QUEUEput(int data);
 15 void QUEUEempty();

Stack stack: push into the stack, pop out of the stack

  1 /*
  2  * FILE: STACK_linkedlist.c
  3  * ========================
  4 * stack stack last in first out
  5 * Push on the stack: put it in the head of the linked list, and update the head pointer of the linked list
  6 * Pop out the stack: return and delete the head node of the linked list, and update the head pointer of the linked list
  7  */
  8
  9 #include "STACK_linkedlist.h"
 10
 11 // static static global variables, limiting the scope of global variables
 12 static struct STACKnode *head = NULL;
 13
 14 // static static function, which limits the scope of the function
 15 static struct STACKnode *NEW(int data, struct STACKnode *head)
 16 {
 17         struct STACKnode *new = (struct STACKnode *)malloc(sizeof(struct STACKnode));
 18         new->data = data;
 19         new->next = head;
 20         return new;
 21 }
 22
 23 void STACKpush(int data)
 24 {
 25         head = NEW(data, head);
 26 }
 27
 28 int STACKpop()
 29 {
 30         int res = head->data;
 31         struct STACKnode *p = head;
 32         head = head->next;
 33         free(p);
 34         return res;
 35 }
 36
 37 void STACKempty()
 38 {
 39         struct STACKnode *p = head;
 40         while(p != NULL)
 41         {
 42                 struct STACKnode *temp = p;
 43                 p = p->next;
 44                 free(temp);
 45         }
 46         head = NULL;
 47 }
/*
 * FILE: STACK_linkedlist.h
 * ========================
 */

#include <stdlib.h>

struct STACKnode{
        int data;
        struct STACKnode *next;
};

void STACKpush(int data);
int STACKpop();
void STACKempty();



Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=326700033&siteId=291194637