0x00数据结构——C语言实现(双向循环链表)
#ifndef D_C_LIST
#define D_C_LIST
typedef int T;
#define MAXLEN 100
typedef enum {
false = 0,
true
} BOOL;
struct node;
typedef struct node dcnode;
typedef struct node *to_node;
typedef to_node d_c_list;
typedef to_node pos;
dc_list create_list(void);
BOOL set_empty(dc_list l);
int calc_length(dc_list l);
dc_list head_addr(dc_list l);
pos search(dc_list l, T x);
pos locate(dc_list l, int i);
T get_val(dc_list l, int i);
BOOL change_val(dc_list l, int i, const T x);
BOOL insert_val(dc_list l, int i, const T x);
BOOL delete_val(dc_list l, int i, T *x);
BOOL isempty(dc_list l);
void output(dc_list l);
BOOL sort(dc_list l);
#endif
#include <stdio.h>
#include <stdlib.h>
#include "double_linkedc_list.h"
struct node{
T val;
struct node *prev;
struct node *next;
};
dc_list create_list(void)
{
dc_list l = (dc_list)malloc(sizeof(dcnode));
l->next = l;
l->prev = l;
l->val = 0;
return l;
}
BOOL set_empty(dc_list l)
{
pos tmp = l->next, ttemp;
while(tmp!=l){
ttemp = tmp->next;
free(tmp);
tmp = ttemp;
}
l->next = l;
l->prev = l;
l->val = 0;
return true;
}
int calc_length(dc_list l)
{
return l->val;
}
pos head_addr(dc_list l)
{
return l;
}
pos search(dc_list l, T x)
{
int i = 1;
pos tmp;
tmp = l->next;
while(tmp != l && tmp->val != x && i<=l->val){
tmp = tmp->next;
i++;
}
if(i>l->val) return NULL;
else return tmp;
}
pos locate(dc_list l, int i)
{
if(i<=l->val && i>0){
pos tmp;
tmp = l;
while(--i>=0){
tmp = tmp->next;
}
return tmp;
} else {
printf("can not access the %d'th element\n", i);
return NULL;
}
}
T get_val(dc_list l, int i)
{
if(i<=l->val && i>0){
pos tmp;
tmp = l;
while(--i>=0){
tmp = tmp->next;
}
return tmp->val;
} else {
printf("can not access the %d'th element\n", i);
return -1;
}
}
BOOL change_val(dc_list l, int i, const T x)
{
if(i<=l->val && i>0){
pos tmp;
tmp = l;
while(--i>=0){
tmp = tmp->next;
}
tmp->val = x;
return true;
} else {
return false;
}
}
BOOL insert_val(dc_list l, int i, const T x)
{
if(i<=l->val && i>=0){
pos tmp;
tmp = l;
while(--i>=0){
tmp = tmp->next;
}
pos t = (pos)malloc(sizeof(dcnode));
t->next = tmp->next;
(tmp->next)->prev = t;
t->prev = tmp;
tmp->next = t;
t->val = x;
l->val++;
return true;
} else {
return false;
}
}
BOOL delete_val(dc_list l, int i, T *x)
{
if(i<=l->val && i>0){
i--;
pos tmp;
tmp = l;
while(--i>=0 && tmp!=NULL){
tmp = tmp->next;
}
*x = (tmp->next)->val;
tmp->next = (tmp->next)->next;
(tmp->next)->prev = tmp;
l->val--;
return true;
} else {
return false;
}
}
BOOL isempty(const dc_list l)
{
return (l->val == 0 ? true:false);
}
void output(const dc_list l)
{
pos tmp = NULL;
int i = 1;
tmp = l->next;
while(tmp!=NULL){
printf("the %dth element is %d\n", i++, tmp->val);
tmp = tmp->next;
}
}
BOOL sort(const dc_list l)
{
if(l->val < 2)
return true;
pos tmp, tmp1, tmp2;
int i;
tmp = l->prev;
while(tmp->prev != l) {
tmp1 = l->next;
tmp2 = tmp1->next;
while(tmp1 != tmp) {
if(tmp1->val > tmp2->val) {
i = tmp1->val;
tmp1->val = tmp2->val;
tmp2->val = i;
}
tmp1 = tmp1->next;
tmp2 = tmp2->next;
}
tmp = tmp->prev;
}
return true;
}