双向循环链表的实现
来源:互联网 发布:《网络黑白》txt下载 编辑:程序博客网 时间:2024/06/05 10:13
/************************ file: linkhead.h ****************************/
#ifndef _LINKHEAD_H_
#define _LINKHEAD_H_typedef void LinkHead;
typedef struct LinkNode LinkNode;
struct LinkNode{
LinkNode* next;
LinkNode* prev;
};
LinkHead* Link_Create(void); //创建链表
int Link_Insert(LinkHead* head, LinkNode* node, int pos); //插入节点
LinkNode* Link_Get(LinkHead* head, int pos); //获取指定节点
LinkNode* Link_Delete(LinkHead* head, int pos); // 删除节点
int Link_Length(LinkHead* head); //获取链表长度
void Link_Destroy(LinkHead* head); //销毁链表
#endif // _LINKHEAD_H_
/************************ file: linkhead.h end *************************/
/************************ file: linkhead.c ****************************/
#include <malloc.h>
#include "linkhead.h"
typedef struct _tag_LinkHead{
LinkNode headnode;
int length;
}TLinkHead;
LinkHead* Link_Create(void)
{
TLinkHead* Thead = NULL;
Thead = (TLinkHead*)malloc(sizeof(TLinkHead));
if(Thead != NULL)
{
Thead->headnode.next = NULL;
Thead->headnode.prev = NULL;
Thead->length = 0;
}
return Thead;
}
int Link_Length(LinkHead* head)
{
TLinkHead* Thead = (TLinkHead*)head;
int ret = -1;
if(Thead != NULL)
ret = Thead->length;
return ret;
}
int Link_Insert(LinkHead* head, LinkNode* node, int pos)
{
TLinkHead* Thead = (TLinkHead*)head;
LinkNode* temp = NULL;
int ret = -1;
int i;
if(Thead != NULL && pos >= 0 && pos <= Thead->length && node != NULL)
{
temp = &Thead->headnode;
for(i=0; i<pos; i++)
{
temp = temp->next;
}
node->next = temp->next;
temp->next = node;
Thead->length++;
ret = 0;
}
return ret;
}
LinkNode* Link_Get(LinkHead* head, int pos)
{
TLinkHead* Thead = (TLinkHead*)head;
LinkNode* ret = NULL;
int i;
if(Thead != NULL && pos >=0 && pos < Thead->length)
{
ret = Thead->headnode.next;
for(i=0; i<pos; i++)
{
ret = ret->next;
}
}
return ret;
}
LinkNode* Link_Delete(LinkHead* head, int pos)
{
TLinkHead* Thead = (TLinkHead*)head;
LinkNode* ret = NULL;
int i;
if(Thead != NULL && pos >= 0 && pos < Thead->length)
{
ret = &Thead->headnode;
for(i=0; i<pos; i++)
{
ret = ret->next;
}
ret = ret->next;
Thead->headnode.next = ret->next;
Thead->length--;
}
return ret;
}
void Link_Destroy(LinkHead* head)
{
if(head != NULL)
free(head);
}
/************************ file: linkhead.c end *************************/
/************************ file: main.c ****************************/
#include <stdio.h>
#include "linkhead.h"
struct node{
LinkNode* nodelink;
int data;
};
int main(int argc, char **argv)
{
int i;
LinkHead* header = Link_Create();
struct node v1;
struct node v2;
struct node v3;
struct node v4;
struct node v5;
v1.data = 1;
v2.data = 2;
v3.data = 3;
v4.data = 4;
v5.data = 5;
Link_Insert(header, (LinkNode*)&v1, 0);
Link_Insert(header, (LinkNode*)&v2, 0);
Link_Insert(header, (LinkNode*)&v3, 0);
Link_Insert(header, (LinkNode*)&v4, 0);
Link_Insert(header, (LinkNode*)&v5, 0);
for(i=0; i<Link_Length(header); i++)
{
struct node* tmp = (struct node*)Link_Get(header, i);
printf("%d\n", tmp->data);
}
while(Link_Length(header) > 0)
{
struct node* tmp = (struct node*)Link_Delete(header, 0);
printf("%d\n", tmp->data);
}
Link_Destroy(header);
printf("world!\n");
return 0;
}
/************************ file: main.c end *************************/
- 双向循环链表的实现
- 双向循环链表的c++ 实现
- Linux双向循环链表的实现
- 双向循环链表的实现
- 双向循环链表的C++实现
- Linux双向循环链表的实现
- 双向循环链表操作的实现
- Linux双向循环链表的实现
- Linux双向循环链表的实现
- 双向循环链表操作的实现
- Linux双向循环链表的实现
- 双向循环链表的实现
- 双向循环链表 函数的实现
- 双向循环链表的实现
- 循环双向链表的实现
- 双向循环链表的实现
- 双向循环链表的简单实现
- Linux双向循环链表的实现
- Java内部类
- 无法连接到数据库实例: ORA-12519: TNS: 没有找到适用的服务处理程序 (DBD ERROR: OCIServerAttach)。
- 测试类运行报错
- 密码参数引起的DataGuard状态异常.docx
- [IOS]UIPickerView选择时间段
- 双向循环链表的实现
- Ubuntu14.10 64 安装VMware_workstation
- Nutch关于robot.txt的处理
- c++ 有四个常用的预定义名字,分别为 :__FILE__,__LINE__,__DATE__,__TIME__
- java.lang.RuntimeException: org.apache.thrift7.transport.TTransportException: java.net.ConnectExcept
- JavaScript 对this的理解
- 线程句柄作WaitForSingleObject的参数是什么意思?
- OpenGL 矩阵变换(讲的太好了~!)
- HDU2037 活动安排 贪心