#include<stdio.h>
#include<string.h>
#include<stdlib.h>
typedef struct _node
{
struct _node *pre;
struct _node *next;
int data;
}dbnode_t;
typedef struct _list
{
dbnode_t *head;
dbnode_t *tail;
int len;
}dblist_t;
dbnode_t* creat_dbnode(int pdata)
{
dbnode_t* db_node = (dbnode_t*)malloc(sizeof(dbnode_t));
if (NULL == db_node)
return NULL;
db_node->pre = NULL;
db_node->next= NULL;
db_node->data= pdata;
return db_node;
}
void destroy_dbnode(dbnode_t* dbnode)
{
if (dbnode)
{
free(dbnode);
}
return;
}
dblist_t* creat_dblist()
{
dblist_t* pnlist = (dblist_t*)malloc(sizeof(dblist_t));
if (NULL == pnlist)
return NULL;
pnlist->len = 0;
pnlist->head= NULL;
pnlist->tail= NULL;
return pnlist;
}
void destroy_dblist(dblist_t* pnlist)
{
dbnode_t *cur_node = NULL;
if (pnlist)
{
if (NULL == pnlist->tail || NULL == pnlist->head)
{
free(pnlist);
return;
}
cur_node = pnlist->tail;
while(cur_node->pre)
{
cur_node = cur_node->pre;
destroy_dbnode(cur_node->next);
}
free(pnlist);
}
return;
}
// 获取指定位置上的节点
dbnode_t * getnode(dblist_t list, int pos)
{
int i;
dbnode_t *cur_node = list.head;
if (NULL == cur_node || 0 >= pos)
return NULL;
for (i=1;i<pos;i++)
{
if (cur_node)
cur_node= cur_node->next;
else
return NULL;
}
return cur_node;
}
void printf_alldata(dblist_t* db_list)
{
dbnode_t* cur_node = NULL;
if (NULL == db_list || NULL == db_list->head)
return;
cur_node = db_list->head;
printf("***********************\n");
while (cur_node)
{
printf("cur_node data = %d\n", cur_node->data);
cur_node = cur_node->next;
}
printf("db_list len = %d\n", db_list->len);
return;
}
void lpush(dblist_t* pnlist, dbnode_t* dbnode)
{
dbnode_t* head = NULL;
if (NULL == dbnode || NULL == pnlist)
return;
if (NULL == pnlist->head)
{
pnlist->head = dbnode;
pnlist->tail = dbnode;
pnlist->len++;
return;
}
head = pnlist->head;
dbnode->next = head;
head->pre = dbnode;
pnlist->head = dbnode;
pnlist->len++;
return;
}
void rpush(dblist_t* pnlist, dbnode_t* dbnode)
{
dbnode_t* tail = NULL;
if (NULL == dbnode || NULL == pnlist)
return;
if (NULL == pnlist->head || NULL == pnlist->tail)
{
pnlist->head = dbnode;
pnlist->tail = dbnode;
pnlist->len++;
return;
}
tail = pnlist->tail;
dbnode->pre = tail;
tail->next = dbnode;
pnlist->tail = dbnode;
pnlist->len++;
return;
}
void lpop(dblist_t* db_list)
{
if (NULL == db_list || NULL == db_list->head)
return;
if (db_list->head->next)
{
db_list->head = db_list->head->next;
free(db_list->head->pre);
db_list->head->pre = NULL;
}
else
{
free(db_list->head);
db_list->head = NULL;
}
db_list->len--;
if (db_list->len == 0)
{
db_list->tail = NULL;
}
return;
}
void rpop(dblist_t* db_list)
{
if (NULL == db_list || NULL == db_list->tail)
return;
if (db_list->tail->pre)
{
db_list->tail = db_list->tail->pre;
free(db_list->tail->next);
db_list->tail->next = NULL;
}
else
{
free(db_list->tail);
db_list->tail = NULL;
}
db_list->len--;
if (db_list->len == 0)
{
db_list->head = NULL;
}
return;
}
void main()
{
dblist_t* db_list = NULL;
dbnode_t* db_node = NULL;
dbnode_t* cur_node = NULL;
int i;
db_list = creat_dblist();
if (NULL == db_list)
return;
for (i=20;i<25;i++)
{
db_node = creat_dbnode(11-i);
if (!db_node)
return;
rpush(db_list, db_node);
if (db_list)
printf("db_list len = %d\n", db_list->len);
}
for (i=0;i<9;i++)
{
db_node = creat_dbnode(11-i);
if (!db_node)
return;
lpush(db_list, db_node);
if (db_list)
printf("db_list len = %d\n", db_list->len);
}
if (db_list)
printf_alldata(db_list);
cur_node = getnode(*db_list, 0);
if (cur_node)
printf("cur_node data = %d\n", cur_node->data);
for (i=0;i<7;i++)
{
lpop(db_list);
if (db_list)
printf_alldata(db_list);
rpop(db_list);
if (db_list)
printf_alldata(db_list);
}
rpop(db_list);
if (db_list)
printf_alldata(db_list);
if (db_list)
destroy_dblist(db_list);
return;
}