谁那有双向链表实现的队列,c语言的

jieao111 2012-10-25 03:36:09
求个代码,谢谢
...全文
448 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
AnYidan 2012-10-26
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 的回复:]
自己动手,丰衣足食。
[/Quote]

网上 google
proorck6 2012-10-26
  • 打赏
  • 举报
回复
自己动手,丰衣足食。
kmesg 2012-10-26
  • 打赏
  • 举报
回复
楼主真帅
newtee 2012-10-25
  • 打赏
  • 举报
回复
楼上 GOOD!
smart900613 2012-10-25
  • 打赏
  • 举报
回复
封装队列链表
llist.h

#ifndef __LLIST_H__
#define __LLIST_H__

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

//有头循环双向链表
struct node_t {
void *data;
struct node_t *next;
struct node_t *prev;
};

typedef struct llist_t {
int size;
int num;
struct node_t head;
}LLIST;


#define PREPEND 0
#define APPEND -1
#define SORT -2

#define DEBUG0(...)
#define DEBUG1(...) fprintf(stderr, __VA_ARGS__);

#define ERRP(con, ret, flag, ...) do \
{ \
if (con) \
{ \
DEBUG##flag(__VA_ARGS__) \
ret; \
} \
} while (0)

typedef void (llist_op_t)(const void *);
typedef int (llist_cmp_t)(const void *, const void *);

LLIST *llist_create(int size);
int llist_append(void *data, LLIST *handle);
int llist_prepend(void *data, LLIST *handle);
int llist_insert_sort(void *data, llist_cmp_t *cmp, LLIST *handle);
int llist_insert(void *data, int index, LLIST *handle);
int llist_num(LLIST *handle);
int llist_size(LLIST *handle);
void *llist_ind(int index, LLIST *handle);
int llist_delete_ind(int index, LLIST *handle);
void *llist_find(void *key, llist_cmp_t *cmp, LLIST *handle);
int llist_delete(void *key, llist_cmp_t *cmp, LLIST *handle);
void llist_sort(llist_cmp_t *cmp, LLIST *handle);
int llist_store(const char *path, LLIST *handle);
LLIST *llist_load(const char *path);
void llist_travel(llist_op_t *op, LLIST *handle);
void llist_destroy(LLIST *handle);

#endif /* __LLIST_H__ */

llist.c 具体实现
#include <llist.h>

LLIST *llist_create(int size)
{
LLIST *handle = NULL;

handle = (LLIST *)malloc(sizeof(LLIST));
ERRP(NULL == handle, return NULL, 0);

handle->size = size;
handle->num = 0;
handle->head.next = &handle->head;
handle->head.prev = &handle->head;

return handle;
}

int insert(void *data, int index, llist_cmp_t *cmp, LLIST *handle)
{
struct node_t *new = NULL, *tail = NULL;

new = (struct node_t *)malloc(sizeof(struct node_t));
ERRP(NULL == new, goto ERR1, 0);

new->data = (void *)malloc(handle->size);
ERRP(NULL == new->data, goto ERR2, 0);

memcpy(new->data, data, handle->size);

if (index == PREPEND || index < SORT)
tail = &handle->head;
else if (index == APPEND || index > handle->num)
tail = handle->head.prev;
else if (index == SORT)
{
for (tail = &handle->head;
tail->next != &handle->head &&
cmp(tail->next->data, new->data) < 0;
tail = tail->next)
;
}
else
{
for (tail = &handle->head;
--index && tail->next != &handle->head;
tail = tail->next)
;
}

new->next = tail->next;
new->prev = tail;
tail->next->prev = new;
tail->next = new;

handle->num++;

return 0;
ERR2:
free(new);
ERR1:
return -1;
}

int llist_append(void *data, LLIST *handle)
{
return insert(data, APPEND, NULL, handle);
}

int llist_prepend(void *data, LLIST *handle)
{
return insert(data, PREPEND, NULL, handle);
}

int llist_insert_sort(void *data, llist_cmp_t *cmp, LLIST *handle)
{
return insert(data, SORT, cmp, handle);
}

int llist_insert(void *data, int index, LLIST *handle)
{
if (index == SORT)
return -1;
return insert(data, index, NULL, handle);
}

int llist_num(LLIST *handle)
{
return handle->num;
}

int llist_size(LLIST *handle)
{
return handle->size;
}

void *llist_ind(int index, LLIST *handle)
{
int i;
struct node_t *tail = NULL;

ERRP(index < 1 || index > handle->num, return NULL, 0);

for (tail = handle->head.next, i = 1;
tail != &handle->head && i != index;
tail = tail->next, i++)
;
return tail->data;
}

int llist_delete_ind(int index, LLIST *handle)
{
int i;
struct node_t *tail = NULL;

ERRP(index < 1 || index > handle->num, return -1, 0);

for (tail = handle->head.next, i = 1;
tail != &handle->head && i != index;
tail = tail->next, i++)
;
tail->next->prev = tail->prev;
tail->prev->next = tail->next;

free(tail->data);
free(tail);

handle->num--;

return 0;
}

void *llist_find(void *key, llist_cmp_t *cmp, LLIST *handle)
{
struct node_t *tail = NULL;

for (tail = handle->head.next; tail != &handle->head; tail = tail->next)
{
if (!cmp(key, tail->data))
return tail->data;
}

return NULL;
}

int llist_delete(void *key, llist_cmp_t *cmp, LLIST *handle)
{
struct node_t *tail = NULL, *save = NULL;

for (tail = handle->head.next; tail != &handle->head; tail = save)
{
save = tail->next;
if (!cmp(key, tail->data))
{
tail->next->prev = tail->prev;
tail->prev->next = tail->next;
free(tail->data);
free(tail);
return 0;
}
}

return -1;

}

void llist_sort(llist_cmp_t *cmp, LLIST *handle)
{
struct node_t *tail = NULL, *val = NULL;
void *t = NULL;

for (tail = handle->head.next; tail->next != &handle->head; tail = tail->next)
{
for (val = tail->next; val != &handle->head; val = val->next)
{
if (cmp(tail->data, val->data) > 0)
{
t = tail->data;
tail->data = val->data;
val->data = t;
}
}
}
}

int llist_store(const char *path, LLIST *handle)
{
FILE *fp = NULL;
struct node_t *tail = NULL;

fp = fopen(path, "w");
ERRP(NULL == fp, return -1, 0);

fwrite(&handle->size, sizeof(handle->size), 1, fp);
fwrite(&handle->num, sizeof(handle->num), 1, fp);

for (tail = handle->head.next; tail != &handle->head; tail = tail->next)
{
fwrite(tail->data, handle->size, 1, fp);
}

fclose(fp);

return 0;
}

LLIST *llist_load(const char *path)
{
FILE *fp = NULL;
struct node_t *tail = NULL;
int size, num, ret, i;
LLIST *handle = NULL;
void *t = NULL;

fp = fopen(path, "r");
ERRP(NULL == fp, goto ERR1, 0);

ret = fread(&size, sizeof(size), 1, fp);
ERRP(ret != 1, goto ERR2, 0);
ret = fread(&num, sizeof(num), 1, fp);
ERRP(ret != 1, goto ERR2, 0);

handle = llist_create(size);
ERRP(NULL == handle, goto ERR2, 0);

t = (void *)malloc(size);
ERRP(NULL == t, goto ERR3, 0);

for (i = 0; i < num; i++)
{
ret = fread(t, size, 1, fp);
ERRP(ret != 1, goto ERR4, 0);

ret = llist_append(t, handle);
ERRP(ret == -1, goto ERR4, 0);
}

free(t);
fclose(fp);

return handle;
ERR4:
free(t);
llist_destroy(handle);
ERR3:
free(handle);
ERR2:
fclose(fp);
ERR1:
return NULL;
}

void llist_travel(llist_op_t *op, LLIST *handle)
{
struct node_t *tail = NULL;

for (tail = handle->head.next; tail != &handle->head; tail = tail->next)
{
op(tail->data);
}
}

void llist_destroy(LLIST *handle)
{
struct node_t *tail = handle->head.next, *save = NULL;

while (tail != &handle->head)
{
save = tail->next;
free(tail->data);
free(tail);
tail = save;
}

free(handle);
}




newtee 2012-10-25
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 的回复:]

麻痹的,害我自己写了一个,现在论坛堕落了啊
[/Quote]自己写的不是很好吗?
jieao111 2012-10-25
  • 打赏
  • 举报
回复
麻痹的,害我自己写了一个,现在论坛堕落了啊
newtee 2012-10-25
  • 打赏
  • 举报
回复
自己去找找吧 要不买本数据结构书看看 上面都有的
jieao111 2012-10-25
  • 打赏
  • 举报
回复
1楼是c++,2楼的代码太烂
newtee 2012-10-25
  • 打赏
  • 举报
回复
网上好多
队列的双向链表实现
时间 : 2009-11-14 作者:匿名 编辑:小张 点击: 67 [ 评论 ]
-
-
/*queue.h*/

#ifndef QUEUE_H_
#define QUEUE_H_

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int tcreate_queue(int elem_size, int *queue);
int tadd_queue_head(int queue, void *elem);
int tadd_queue_tail(int queue, void *elem) ;
int tget_queue_head(int queue, void *elem);
int tget_queue_tail(int queue, void *elem);
int tdestory_queue(int queue);

#endif


#include "queue.h"

typedef struct Node
{
void *elem;
struct Node *prev;
struct Node *next;
}QNode;

typedef struct QLink
{
int data_size;
QNode *head;
QNode *tail;
}QLink;



/************************************************
*函数功能: 创建一个队列
*参数 @elem_size 表示数据大小,传去参数
*参数 @queue表示接口的handle ,传出参数
**************************************************/
int tcreate_queue(int elem_size, int *queue)
{
QLink *Qu = NULL;
Qu = (QLink *)malloc(sizeof(QLink));
if (Qu == NULL)
{
printf("fail!\n");
return -1;
}

Qu->data_size = elem_size;
Qu->head = NULL;
Qu->tail = NULL;
*queue = (int)Qu;

return 0;
}

/*******************************************
* 函数功能: 增加结点到 队列队头
*参数@queue 接口handle,传入参数
*参数@elem,数据,传入参数
********************************************/
int tadd_queue_head(int queue, void *elem)
{
QLink *Qu = NULL;
QNode *node = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;

node = (QNode *)malloc(sizeof(QNode));
if (node == NULL)
{
printf("fail!\n");
return -1;
}
node->elem = (void *)malloc(Qu->data_size);
if(node->elem == NULL)
{
printf("fail!\n");
free(node);
return -1;
}

memcpy(node->elem, elem, Qu->data_size);
if (Qu->head == NULL && Qu->tail == NULL)
{
Qu->head = node;
Qu->tail = node;
node->next = NULL;
node->prev = NULL;
}
else
{
pt = Qu->head;
pt->prev = node;
node->next = pt;
Qu->head = node;
node->prev = NULL;
}

return 0;

}


/**********************************************
* 函数功能: 添加结点到队列的队尾
*参数:@queue ,接口handle ,传入参数
*参数:@elem 数据,传入参数
************************************************/
int tadd_queue_tail(int queue, void *elem)
{
QLink *Qu = NULL;
QNode *node = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;
node = (QNode *)malloc(sizeof(QNode));
if (node == NULL)
{
printf("fail\n");
return -1;
}

node->elem = (void *)malloc(Qu->data_size);
if (node->elem == NULL)
{
printf("fail!\n");
free(node);
return -1;
}
memcpy (node->elem, elem, Qu->data_size);

pt = Qu->tail;
/*查看是否是第一个接点*/
if (Qu->head == NULL && Qu->tail == NULL)
{
Qu->head = node;
Qu->tail = node;
node->next = NULL;
node->prev = NULL;
}
else
{
pt->next = node;
node->next = NULL;
node->prev = pt;
Qu->tail = node;
}
return 0;

}

/*************************************************
*函数功能: 删除队列的队尾元素
*参数@ queue 接口handel,传入参数
*参数@elem 返回删除的数据,传出参数
**************************************************/
int tget_queue_tail(int queue, void *elem )
{
QLink *Qu = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;
pt = Qu->tail;
if (Qu->head == NULL && Qu->tail == NULL)
{
printf("queue is empty!\n");
return -1;
}

memcpy(elem, pt->elem, Qu->data_size);
if (Qu->head == Qu->tail)
{
Qu->head = NULL;
Qu->tail = NULL;
free(pt);
}
else
{
Qu->tail = pt->prev;
pt->prev->next = NULL;
free(pt);
}
return 0;
}

/***************************************************
*函数功能 : 删除队列的 头结点
*参数@queue 接口handle ,传入参数
*参数@elem,返回删除的数据,传出参数
****************************************************/
int tget_queue_head(int queue, void *elem)
{
QLink *Qu = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;

pt = Qu->head;

if (Qu->head == NULL && Qu->tail == NULL)
{
printf("queue is empty !\n");
return -1;
}

memcpy(elem, pt->elem, Qu->data_size);

if (Qu->head == Qu->tail)
{
Qu->head = NULL;
Qu->tail = NULL;
free(pt);

}
else
{
Qu->head = pt->next;
pt->next->prev = NULL;
free(pt);
}
return 0;
}

/*****************************************
*函数功能: 销毁队列
*参数@queue 接口的handle,传入参数
******************************************/
int tdestory_queue(int queue)
{
QLink *Qu = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;

if (Qu->head == NULL && Qu->tail == NULL)
{
printf("queue is empty!\n");
free(Qu);
return -1;
}

pt = Qu->head;

while(pt != NULL)
{
Qu->head = pt->next;
free(pt);
pt = Qu->head;
}
Qu->head = NULL;
Qu->tail = NULL;

#if 0
while(Qu->head != NULL)
{

Qu->head = pt->next;
pt->next->prev = NULL;
free(pt);
pt = Qu->head;
if (Qu->head == Qu->tail)
{
Qu->head =NULL;
Qu->tail = NULL;
free(pt);
}
}
#endif
free(Qu);
return 0;
}


/***************************************
*函数功能: 打印队列
*参数@queue, 接口handle,传入参数
*****************************************/
static int print(int queue)
{
QLink *Qu = NULL;
QNode *pt = NULL;

Qu = (QLink *)queue;

pt = Qu->head;

if (Qu->head == NULL && Qu->tail == NULL)
{
printf("queue is empty !\n");
return 0;
}

while(pt != NULL)
{
printf("%s\n", pt->elem);
pt = pt->next;
}
return 0;
}

void menu()
{
printf("******************************\n");
printf("选择你的操作:\n");
printf("1 创建一个队列\n");
printf("2 添加结点到队头\n");
printf("3 添加结点到队尾\n");
printf("4 删除队列头结点\n");
printf("5 删除队列尾结点\n");
printf("6 销毁队列\n");
printf("7 打印队列\n");
printf("8 退出\n");
printf("******************************\n");
}

int main()
{
int *queue = NULL;
int size = 0;
char *elem= NULL;
int choice = 0;

queue = (int *)malloc(sizeof (int));
if (queue == NULL)
{
printf("fail!\n");
goto fail;
}
elem = (char *)malloc(sizeof(char));
if (elem == NULL)
{
printf("fail!\n");
goto fail;
}


while(1)
{
menu();
printf ("选择你的操作:\n");
scanf("%d", &choice);

switch(choice)
{
case 1:
printf("input data_size:\n");
scanf("%d", &size);
tcreate_queue(size, queue );
break;

case 2:
printf("Add node head ,input elem:\n");
scanf("%s", elem);
tadd_queue_head(*queue, elem);
printf("Add head success!\n");
break;

case 3:
printf("Add node tail ,input elem:\n");
scanf("%s", elem);
tadd_queue_tail(*queue, elem);
printf("Add tail success!\n");
break;

case 4:
printf("delete node head!\n");
tget_queue_head(*queue, elem);
printf("delete head success!\n");
break;

case 5:
printf("delete node tail!\n");
tget_queue_tail(*queue, elem);
printf("delete tail success!\n");
break;

case 6:
printf("destory queue!\n");
tdestory_queue(*queue);
printf("destory success!\n");
break;

case 7:
printf("print queue:\n");
print(*queue);
break;

case 8:
return 0;

default :
printf ("Input error !\n");
break;
}
}


fail:
if(elem)
{
free(elem);
elem = NULL;
}
if (queue)
{
free(queue);
queue = NULL;
}
return 0;

}

图灵狗 2012-10-25
  • 打赏
  • 举报
回复
参考(http://soft.chinabyte.com/database/473/12311473.shtml)这篇文章及其代码。

69,371

社区成员

发帖
与我相关
我的任务
社区描述
C语言相关问题讨论
社区管理员
  • C语言
  • 花神庙码农
  • 架构师李肯
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧