链表(链式存储)的基本操作
来源:互联网 发布:买了域名之后怎么用 编辑:程序博客网 时间:2024/05/21 18:31
Clist.h
#ifndef CLIST_H_
#define CLIST_H_typedef struct _tag_CListNode
{
struct _tag_CListNode* next;
}CListNode;
typedef void CList;
CList* CList_Create();
void CList_Destroy(CList* list);
void CList_Clear(CList* list);
int CList_Length(CList* list);
int CList_Insert(CList* list,CListNode* node,int pos);
CListNode* CList_Get(CList* list,int pos);
#endif
Clist.c
#include "Clist.h"
#include <iostream>
typedef struct _tag_CList
{
CListNode head;
int length;
}TCList;
CList* CList_Create()
{
TCList *ret=NULL;
ret=(TCList *)malloc(sizeof(TCList));
memset(ret,0,sizeof(TCList));
return ret;
}
void CList_Destroy(CList* list)
{
if(list!=NULL)
{
free(list);
list=NULL;
}
return;
}
void CList_Clear(CList* list)
{
TCList *tlist=NULL;
if(list==NULL)
{
return;
}
tlist=(TCList*)list;
tlist->length=0;
tlist->head.next=NULL;
return;
}
int CList_Length(CList* list)
{
TCList *tlist=NULL;
if(list==NULL)
{
return -1;
}
tlist=(TCList*)list;
return tlist->length;
}
int CList_Insert(CList* list,CListNode* node,int pos)
{
int ret=0,i=0;
CListNode *current=NULL;
TCList *tlist=NULL;
if(list==NULL||node==NULL||pos<0)
{
ret=-1;
printf("fun CList_Insert() err:%d",ret);
return ret;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&&(current->next!=NULL);i++)
{
current=current->next;
}
node->next=current->next;
current->next=node;
tlist->length++;
return 0;
}
CListNode* CList_Get(CList* list,int pos)
{
int ret=0,i=0;
CListNode *current=NULL;
TCList *tlist=NULL;
if(list==NULL||pos<0)
{
ret=-1;
printf("fun CList_Get() err:%d",ret);
return NULL;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&¤t->next!=NULL;i++)
{
current=current->next;
}
return current->next;
}
CListNode* CList_Delete(CList* list,int pos)
{
int i=0;
CListNode *current=NULL;
CListNode *ret=NULL;
TCList *tlist=NULL;
if(list==NULL||pos<0)
{
printf("fun CList_Get() err:%d",ret);
return NULL;
}
tlist=(TCList*)list;
current=&(tlist->head);
for(i=0;i<pos&¤t->next!=NULL;i++)
{
current=current->next;
}
ret=current->next;
current->next=ret->next;
tlist->length--;
return ret;
}
main.c
#include "Clist.h"
#include <iostream>
typedef struct Teacher
{
CListNode node;
int age;
char name[64];
}Teacher;
void main()
{
int len=0,ret=0,i=0;
Teacher t1,t2,t3,t4,t5;
t1.age=29;
t2.age=35;
t3.age=33;
t4.age=34;
t5.age=28;
CList* list=NULL;
list=CList_Create();
if(list==NULL)
{
return;
}
len=CList_Length(list);
ret=CList_Insert(list,(CListNode*)&t1,0);
ret=CList_Insert(list,(CListNode*)&t2,0);
ret=CList_Insert(list,(CListNode*)&t3,0);
ret=CList_Insert(list,(CListNode*)&t4,0);
ret=CList_Insert(list,(CListNode*)&t5,0);
for(i=0;i<CList_Length(list);i++)
{
Teacher *tmp=(Teacher*)CList_Get(list,i);
if(tmp==NULL)
{
return;
}
printf("tmp->age:%d\n",tmp->age);
}
while(CList_Length(list)>0)
{
Teacher *tmp=(Teacher*)CList_Delete(list,0);
if(tmp==NULL)
{
return;
}
printf("Del tmp->age:%d\n",tmp->age);
}
CList_Destroy(list);
system("pause");
return;
}
- 链表(链式存储)的基本操作
- 数据结构(二)链表1:链式存储的基本操作
- 线性表的链式存储(单链表)的基本操作
- 链式存储的字符串的基本操作(学习历程)
- 串的链式存储的基本操作
- 队列的基本操作(采用链式存储结构)
- 栈的基本操作(链式存储)链栈
- 数据结构 串(链式存储)的基本操作
- 串的链式存储基本操作
- 链式存储无向图的基本操作
- 线性表的链式存储与基本操作
- 二叉树的链式存储结构及基本操作
- 二叉树的链式存储和基本操作
- 链式队列的基本操作(配图)
- 二叉树(二叉链式存储)8种基本操作的实现
- 单链表基本操作的C语言实现(链式存储结构)
- 栈之链式存储基本操作
- 队列的链式存储操作
- gpio-based i2c for eCos
- SpringBoot-01SpringBoot配置FastJson
- redis的基本操作
- git回退版本
- 微信本地开发准备
- 链表(链式存储)的基本操作
- SublimeText在Mac系统上的安装方法
- OrmLite数据库
- MYBATIS 的parameter
- ARM平台嵌入式Linux下使用3G/4G 模块
- HTML5 简介(三):利用 History API 无刷新更改地址栏
- Powerdesigner数据库建模工具教程
- java(优化25) Redis缓存加索引处理数据库百万级并发的方案
- 网络基础---NAT技术和代理服务器