双链表基本操作
2014-06-17 16:29
302 查看
#include <stdio.h> #include <string.h> #include <stdlib.h> #define DNODE_SIZE sizeof(DNode) typedef struct _tagDNode { int value; struct _tagDNode *prov; struct _tagDNode *next; }DNode; DNode* create_double_link(DNode *node) { DNode *head = NULL; if (node->value == 0) { return head; } head = (DNode*)malloc(DNODE_SIZE); head->value = node->value; head->prov = NULL; head->next = NULL; return head; } DNode* insert2tail(DNode *head,DNode *node) { DNode *cur = head; DNode *tn = NULL; tn = (DNode*)malloc(DNODE_SIZE); tn->value = node->value; while (cur->next != NULL) { cur = cur->next; } cur->next = tn; tn->prov = cur; tn->next = NULL; return head; } DNode* insert2head(DNode *head,DNode *node) { DNode *cur = head; DNode *tn = NULL; tn = (DNode*)malloc(DNODE_SIZE); tn->value = node->value; tn->prov = NULL; tn->next = cur; cur->prov = tn; return tn; } DNode* insert(DNode *head,DNode *node) { DNode *cur = head; DNode *tn = NULL; tn = (DNode*)malloc(DNODE_SIZE); tn->value = node->value; if (cur->value >= node->value) { head = insert2head(head,node); return head; } while (cur->next != NULL) { if (cur->value < node->value) { cur = cur->next; continue; } break; } if ((cur ->next == NULL) && (cur->value <= node->value)) { head = insert2tail(head,node); return head; } tn->prov = cur->prov; cur->prov->next = tn; tn->next = cur; cur->prov = tn; return head; } void print_link_by_head(DNode *head) { DNode *tp = head; while (tp->next != NULL) { printf("%d ",tp->value); tp = tp->next; } printf("%d ",tp->value); } void print_link_by_tail(DNode *tail) { DNode *tp = tail; while (tp->prov != NULL) { printf("%d ",tp->value); tp = tp->prov; } printf("%d ",tp->value); } void delete_double_link(DNode *head) { DNode *cur = head; DNode *prov= head; while (cur->next != NULL) { prov = cur; cur = cur->next; free(prov); } free(cur); cur = NULL; prov = NULL; } DNode *gettail(DNode *head) { DNode *cur = head; while (cur->next != NULL) { cur = cur->next; } return cur; } int main(void) { DNode *head = NULL; DNode *tail = NULL; DNode node = {0}; int i = 0; node.value = 1; head = create_double_link(&node); node.value = 65; head = insert(head,&node); node.value = 3; head = insert(head,&node); node.value = 343; head = insert(head,&node); node.value = 33; head = insert(head,&node); node.value = 2; head = insert(head,&node); node.value = 99; head = insert(head,&node); print_link_by_head(head); putchar('\n'); tail = gettail(head); print_link_by_tail(tail); putchar('\n'); return 0; }
相关文章推荐
- 解析从源码分析常见的基于Array的数据结构动态扩容机制的详解
- C#数据结构揭秘一
- 关于C语言中参数的传值问题
- 深入C++中API的问题详解
- 基于C语言string函数的详解
- 探讨C语言的那些小秘密之断言
- 深入探讨C语言中局部变量与全局变量在内存中的存放位置
- 基于C语言fflush()函数的使用详解
- 关于C语言除0引发的思考
- 深入分析C中不安全的sprintf与strcpy
- 探讨register关键字在c语言和c++中的差异
- C中实现矩阵乘法的一种高效的方法
- c语言printf函数的使用详解
- c语言标准库中字符转换函数和数字转换函数
- C语言学籍管理系统源代码
- 浅析C语言中的sizeof
- c语言内存泄露示例解析
- 浅析C语言位域和位段
- 解析C语言中位字段内存分配的问题
- 学生成绩管理系统C语言代码实现