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

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 error
int iastack_init(iastack_t *s, int size);

//free memory
void iastack_free(iastack_t *s);

//return TRUE -- the stack is empty
//	 FALSE -- the stack is overflow
int 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 data
int iastack_pop(iastack_t *s);

#endif	/* __IA_STACK_H__ */

C文件

#include <ia_stack.h>

//return 0 for success
//	 -1 for error
int 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 memory
void iastack_free(iastack_t *s)
{
	if (s->data != NULL) {
		free(s->data);
	}
}

//return TRUE -- the stack is empty
//	 FALSE -- the stack is overflow
int 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 data
int iastack_pop(iastack_t *s)
{
	if (iastack_empty(s) == TRUE) {
		ia_error("stack underflow", 0);
		//impossible
		return 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 error
int iaqueue_init(iaqueue_t *q, int size);

void iaqueue_free(iaqueue_t *q);

//if the queue overflow, the routine will exit immediately
void iaqueue_enqueue(iaqueue_t *q, int x);

//if the queue underflow, the routine will exit immediately
int iaqueue_dequeue(iaqueue_t *q);

#endif	/* __IA_QUEUE_H__ */

C文件

#include <ia_queue.h>

//return 0 for success
//	 -1 for error
int 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 immediately
void 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 immediately
int 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 found
dlist_node_t *iadlist_search(dlist_t *l, int key);

void iadlist_init(dlist_t *l);

//add x to head
void 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 found
dlist_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 head
void 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 found
dlist_cs_node_t *iadlist_cs_search(dlist_cs_t *l, int key);

//add x to head
void 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 found
dlist_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 head
void 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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值