(C语言)数据结构 --- 队列、栈

  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:入队 put、出对get

  1 /*
  2  * FILE: QUEUE_linkedlist.c
  3  * ========================
  4  * 队列 queue
  5  * 入队 put:放在链表尾部,并更新指向链表尾部的指针tail
  6  * 出队 get: 返回并删除链表头部节点,并更新指向链表头部的指针head
  7  */
  8 
  9 #include "QUEUE_linkedlist.h"
 10 
 11 // static 限制全局变量的作用域
 12 static struct node *head = NULL, *tail = NULL;
 13 
 14 // static 限制函数作用域,只作用于本文件
 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:入栈push、出栈pop

  1 /*
  2  * FILE: STACK_linkedlist.c
  3  * ========================
  4  * 栈 stack 后进先出
  5  * 入栈 push:放在链表头部,并更新链表头指针head
  6  * 出栈 pop:返回并删除链表头部节点,并更新链表头指针head
  7  */
  8 
  9 #include "STACK_linkedlist.h"
 10 
 11 // static 静态全局变量,限制了全局变量的作用域
 12 static struct STACKnode *head = NULL;
 13 
 14 // static 静态函数,限制了函数的作用域
 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();



猜你喜欢

转载自blog.csdn.net/trb331617/article/details/79462986