算法导论C语言实现: 基本数据结构

来源:互联网 发布:等保中关于数据库审计 编辑:程序博客网 时间:2024/05/04 15:36

1 stack


头文件

#ifndef__IA_STACK_H__#define __IA_STACK_H__#include <common.h>typedef struct _iastack_t {int top;int size;int *data;} iastack_t;//return 0 for success// -1 for errorint iastack_init(iastack_t *s, int size);//free memoryvoid iastack_free(iastack_t *s);//return TRUE -- the stack is empty// FALSE -- the stack is overflowint iastack_empty(iastack_t *s);//if the stack overflow, the routine will exit immediately//set data first, then increase the stack pointer(top)void iastack_push(iastack_t *s, int x);//if the stack underflow, the routine will exit immediately//decrease the stack pointer(top), the read the dataint iastack_pop(iastack_t *s);#endif/* __IA_STACK_H__ */

C文件

#include <ia_stack.h>//return 0 for success// -1 for errorint iastack_init(iastack_t *s, int size){s->data = (int *)malloc(size * sizeof(int));if (s->data == NULL) {return -1;}s->top = 0;s->size = size;return 0;}//free memoryvoid iastack_free(iastack_t *s){if (s->data != NULL) {free(s->data);}}//return TRUE -- the stack is empty// FALSE -- the stack is overflowint iastack_empty(iastack_t *s){if (s->top == 0) {return TRUE;}return FALSE;}//if the stack overflow, the routine will exit immediately//set data first, then increase the stack pointer(top)void iastack_push(iastack_t *s, int x){if (s->top >= s->size) {ia_error("stack overflow", s->size);}s->data[s->top] = x;s->top++;}//if the stack underflow, the routine will exit immediately//decrease the stack pointer(top), the read the dataint iastack_pop(iastack_t *s){if (iastack_empty(s) == TRUE) {ia_error("stack underflow", 0);//impossiblereturn 0;} else {s->top--;return s->data[s->top];}}


2. queue


头文件

#ifndef __IA_QUEUE_H__#define __IA_QUEUE_H__#include <common.h>typedef struct _iaqueue_t {int tail;int head;int length;int *data;} iaqueue_t;//return 0 for success// -1 for errorint iaqueue_init(iaqueue_t *q, int size);void iaqueue_free(iaqueue_t *q);//if the queue overflow, the routine will exit immediatelyvoid iaqueue_enqueue(iaqueue_t *q, int x);//if the queue underflow, the routine will exit immediatelyint iaqueue_dequeue(iaqueue_t *q);#endif/* __IA_QUEUE_H__ */

C文件

#include <ia_queue.h>//return 0 for success// -1 for errorint iaqueue_init(iaqueue_t *q, int size){if (q == NULL) {return -1;}q->data = (int *) malloc((size + 1) * sizeof(int));if (q->data == NULL) {return -1;}q->length = size;q->head = 0;q->tail = 0;return 0;}void iaqueue_free(iaqueue_t *q){if (q->data != NULL) {free(q->data);}}//if the queue overflow, the routine will exit immediatelyvoid iaqueue_enqueue(iaqueue_t *q, int x){if (q->tail == q->length && q->head == 0) {ia_error("iaqueue overflow", 0);} else if (q->tail == q->head - 1) {ia_error("iaqueue overflow", 0);}q->data[q->tail] = x;if (q->tail == q->length) {q->tail = 0;} else {q->tail = q->tail + 1;}}//if the queue underflow, the routine will exit immediatelyint iaqueue_dequeue(iaqueue_t *q){int x;if (q->head == q->tail) {ia_error("iaqueue underflow", 0);}x = q->data[q->head];if (q->head == q->length) {q->head = 0;} else {q->head = q->head + 1;}return x;}


3. dlist

头文件

#ifndef __IA_DLIST_H__#define __IA_DLIST_H__#include <common.h>typedef struct _dlist_node_t {int key;struct _dlist_node_t *prev;struct _dlist_node_t *next;} dlist_node_t;typedef struct _dlist_t {dlist_node_t *head;dlist_node_t *tail;//TODO} dlist_t;void iadlist_init(dlist_t *l);//return NULL for not founddlist_node_t *iadlist_search(dlist_t *l, int key);void iadlist_init(dlist_t *l);//add x to headvoid iadlist_insert(dlist_t *l, dlist_node_t *x);void iadlist_delete(dlist_t *l, dlist_node_t *x);#endif/* __IA_DLIST_H__ */

C文件

#include <ia_dlist.h>void iadlist_init(dlist_t *l){l->head = NULL;l->tail = NULL;}//return NULL for not founddlist_node_t *iadlist_search(dlist_t *l, int key){dlist_node_t *x = l->head;while(x != NULL && x->key != key) {x = x->next;}return x;}//add x to headvoid iadlist_insert(dlist_t *l, dlist_node_t *x){x->next = l->head;if (l->head != NULL) {l->head->prev = x;}l->head = x;x->prev = NULL;}void iadlist_delete(dlist_t *l, dlist_node_t *x){if (x->prev != NULL) {x->prev->next = x->next;} else {l->head = x->next;}if (x->next != NULL) {x->next = x->prev;}}


4. dlist_cs

头文件

#ifndef__IA_DLIST_CS_H__#define __IA_DLIST_CS_H__#include <common.h>typedef struct _dlist_cs_node_t {int key;struct _dlist_cs_node_t *prev;struct _dlist_cs_node_t *next;} dlist_cs_node_t;typedef struct _dlist_cs_t {dlist_cs_node_t *nil;dlist_cs_node_t nil_node;} dlist_cs_t;void iadlist_cs_init(dlist_cs_t *l);//return NULL for not founddlist_cs_node_t *iadlist_cs_search(dlist_cs_t *l, int key);//add x to headvoid iadlist_cs_insert(dlist_cs_t *l, dlist_cs_node_t *x);void iadlist_cs_delete(dlist_cs_t *l, dlist_cs_node_t *x);#endif/* __IA_DLIST_CS_H__ */

C文件

#include <ia_dlist_cs.h>void iadlist_cs_init(dlist_cs_t *l){l->nil = &l->nil_node;l->nil_node.next = l->nil;l->nil_node.prev = l->nil;}//return NULL for not founddlist_cs_node_t *iadlist_cs_search(dlist_cs_t *l, int key){dlist_cs_node_t *x = l->nil->next;while (x != l->nil && x->key != key) {x = x->next;}if (x != l->nil) {return x;}return NULL;}//add x to headvoid iadlist_cs_insert(dlist_cs_t *l, dlist_cs_node_t *x){x->next = l->nil->next;l->nil->next->prev = x;l->nil->next = x;x->prev = l->nil;}void iadlist_cs_delete(dlist_cs_t *l, dlist_cs_node_t *x){x->prev->next = x->next;x->next->prev = x->prev;}


0 0