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