【复习代码】双向链表
来源:互联网 发布:软通动力java试题2016 编辑:程序博客网 时间:2024/05/24 06:40
#include<stdio.h>
#include<stdlib.h>
#define TRUE 1
#define FALSE 0
typedef int data_t;
typedef struct DNode {
data_t data;
struct DNode *next;
struct DNode *prior;
}DNode, *DouList;
DouList CreateList(void)
{
DouList L = (DouList)malloc(sizeof(DNode));
if(NULL == L)
{
return NULL;
}
L->next = L;
return L;
}
void ClearList(DouList L)
{
DouList p;
while(L->next != L)
{
p = L->next;
L->next = p->next;
free(p);
}
p = NULL;
}
int LengthList(DouList L)
{
int count = 0;
DouList p = L;
while((p->next) != L)
{
p = p->next;
count ++;
}
return count;
}
int IsEmpty(DouList L)
{
if(L->next != L)
{
return FALSE;
}
return TRUE;
}
DouList LocateList(DouList L, data_t x)
{
DouList p = L->next;
while(p != L)
{
if(x == p->data)
{
return p;
}
p = p->next;
}
return NULL;
}
DouList FindList(DouList L, int i)
{
if(i<1 || i>LengthList(L))
{
return NULL;
}
DouList p = L;
int j;
for(j = 0; j < i; j ++)
{
p = p->next;
}
return p;
}
int InsertList(DouList L, data_t x, int i)
{
#if 1
if(i<1 || i>(LengthList(L)+1))
{
return FALSE;
}
#endif
DouList p = L;
int j;
for(j = 1; j < i; j ++)
{
p = p->next;
}
DouList Q = (DouList)malloc(sizeof(DNode));
if(Q != NULL)
{
Q->data = x;
Q->next = NULL;
}
if(1 == i)
{
Q->next = L->next;
Q->prior = L;
L->next->prior = Q;
L->next = Q;
}
else{
Q->next = p->next;
Q->prior = p;
p->next->prior = Q;
p->next = Q;
}
return TRUE;
}
int DleList(DouList L, int i)
{
if(i<1 || i>LengthList(L))
{
return FALSE;
}
DouList p = L;
int j;
for(j = 1; j < i; j ++)
{
p = p->next;
}
DouList q = p->next;
p->next = q->next;
q->next->prior = p;
free(q);
q = NULL;
return TRUE;
}
void DisplayList(DouList L)
{
DouList p = L->next;
while(p != L)
{
printf("%-d ", p->data);
p = p->next;
}
printf("\n");
}
int main()
{
DouList L = CreateList();
int i;
for(i = 1; i <= 5; i++)
{
InsertList(L, i+2, i);
}
DisplayList(L);
DleList(L, 3);
DisplayList(L);
printf("%d\n", IsEmpty(L));
printf("find 3: %p\n", FindList(L, 3));
printf("locate 3: %p\n", LocateList(L, 3));
printf("locate 7: %p\n", LocateList(L, 7));
printf("length: %d\n", LengthList(L));
ClearList(L);
DisplayList(L);
printf("%d\n", IsEmpty(L));
return 0;
}
0 0
- 【复习代码】双向链表
- 【复习笔记】双向链表
- 双向链表 代码
- c++ 双向链表代码
- 数据结构代码-双向链表
- 双向链表代码实例
- 【复习代码】循环链表
- 双向链表代码实现-C语言
- 精简双向链表的插入代码
- C-[双向循环带头链表]-代码
- 双向循环链表代码实践
- 双向链表及约瑟夫环代码
- c实现双向链表代码
- 用C++实现双向链表代码
- 数据结构复习——线性表的链式存储实现(双向链表)
- Linux内核-双向循环链表代码分析
- 代码优化之基于双向链表插入的实现
- 双向链表基本操作及代码优化技巧
- Python中try,except,else,finally用法
- 菜单UI点击隐藏与显示(滑动)
- 实现android端的类似二维码的扫描窗口(中间亮,四周暗,扫描线循环)
- HDU Problem 2571 命运【基础dp】
- IDEA 快速入门
- 【复习代码】双向链表
- 16.git使用总结
- 《Asp.Net Web Api 》-----路由机制
- 深度学习BP算法 BackPropagation
- 二叉树插入算法的非递归版本
- Shader学习笔记2
- 关于微信小程序
- 一个新任程序猿的自白
- linux ubuntu12.04初学 ssh/putty配置