数据结构双向链表c语言实现(linux下多文件实现)
来源:互联网 发布:网络信息发布有关规定 编辑:程序博客网 时间:2024/03/29 02:13
一.dlist头文件
#ifndef __DLIST_H__
#define __DLIST_H__
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *prior;
struct node *next;
}dlistnode;
extern dlistnode *dlist_creat();
extern void dlist_show(dlistnode *H);
extern dlistnode * dlist_get(dlistnode * H,int pos);
extern int dlist_insert(dlistnode *H,int value,int pos);
extern int dlist_delete(dlistnode *H,int pos);
#endif
二.dlist.c文件函数实现
#include"dlist.h"
dlistnode *dlist_creat()
{
dlistnode *H, *r,*p;
int n;
if((H = (dlistnode *)malloc(sizeof(dlistnode))) ==NULL)
{
printf("malloc failed\n");
return NULL;
}
H->prior = H;
H->next = H;
r = H;
while(1)
{
printf("please input(-1 exit):");
scanf("%d",&n);
if(n == -1)
{
break;
}
if((p = (dlistnode *)malloc(sizeof(dlistnode))) ==NULL)
{
printf("malloc failed\n");
return NULL;
}
p->data = n;
p->prior = r;
p->next = r->next;
r->next = p;
H->prior = p;
r = p;
}
return H;
}
void dlist_show(dlistnode *H)
{
dlistnode *p;
p = H->next;
while(p!=H)
{
printf("%d ",p->data);
p = p->next;
}
puts("");
}
dlistnode * dlist_get(dlistnode * H,int pos)
{
int i = -1;
dlistnode *p = H;
if(pos < 0)
{
printf("pos < 0,invalid\n");
return NULL;
}
while(i < pos)
{
p = p->next;
i++;
if(p == H)
{
printf("pos is invalid\n");
return NULL;
}
}
return p;
}
int dlist_insert(dlistnode *H,int value,int pos)
{
dlistnode *p,*q;
p = dlist_get(H,pos);
if(p ==NULL)
{
return -1;
}
if((q = (dlistnode *)malloc(sizeof(dlistnode))) == NULL)
{
printf("malloc failed\n");
return -1;
}
q -> data = value;
q -> prior = p -> prior;
q ->next = p;
p ->prior->next = q;
p -> prior = q;
return 0;
}
int dlist_delete(dlistnode *H,int pos)
{
dlistnode *p;
p = dlist_get(H,pos);
if(p == NULL)
{
return -1;
}
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
p = NULL;
return 0;
}
三.test.c里测试包含main函数
#include"dlist.h"
int main()
{
dlistnode *H , *p;
int pos;
H = dlist_creat();
dlist_show(H);
while(1)
{
printf("input pos:");
scanf("%d",&pos);
dlist_delete(H,pos);
dlist_show(H);
}
#if 0
while(1)
{
printf("input pos:");
scanf("%d",&pos);
dlist_insert(H,100,pos);
dlist_show(H);
}
#endif
#if 0
while(1)
{
printf("input pos:");
scanf("%d",&pos);
p = dlist_get(H,pos);
if(p)
{
printf("%d\n",p->data);
}
}
#endif
return 0;
}
四.makefile文件的实现
CFLAG= -c -Wall -g
test:dlist.o test.o
.PHONY:clean
clean:
rm *.o test
- 数据结构双向链表c语言实现(linux下多文件实现)
- 数据结构之双向链表(C语言实现)
- 数据结构(C实现)------- 双向链表
- 数据结构C语言实现系列——双向链表
- (C语言)双向链表实现案例(数据结构六)
- 数据结构--双向循环链表c语言实现
- 数据结构之---c语言实现双向链表操作
- 数据结构——双向链表(C语言实现)
- C语言实现双向循环链表[下]
- 双向循环链表实现文件C语言
- Linux下的C语言编程——双向循环链表的简单实现
- C实现通用数据结构--双向链表
- 数据结构--双向循环链表C实现
- linux中C语言实现双向循环链表
- 双向链表代码实现-C语言
- 双向链表C语言实现
- C语言实现双向链表[上]
- C语言实现双向链表
- Maven环境搭建
- Photoshop CS6
- Jquery DIV滚动至浏览器顶部位置固定
- 字符集编码详解
- Log4j配置详解
- 数据结构双向链表c语言实现(linux下多文件实现)
- Java标识符、数据类型、运算符
- CameraMotionBlurEditor 在uinty5.x 报错
- Android 实现与Eclipse数据交互
- pandas 中的Axis含义
- @Controller和@RestController的区别
- Azure .Net应用架构原型
- VPN加速修改host文件攻略
- JQuery基础入门介绍