顺序链表的C风格实现
//头文件
#ifndef _SEQLIST_H_
#define _SEQLIST_H_
//定义数据类型
typedef void SeqList;
typedef void SeqListNode; //顺序链表的生成
SeqList* SeqList_Create(int capacity);
//顺序链表的删除
void SeqList_Destory(SeqList* list);
//顺序链表的清空
void SeqList_Clear(SeqList* list);
//返回链表长度
int SeqList_Length(SeqList* list);
//返回链表容量
int SeqList_Capacity(SeqList* list);
//在POS位置插入节点
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos);
//获取POS位置的节点信息
SeqListNode* SeqList_Get(SeqList* list, int pos);
//删除POS位置的节点信息
SeqListNode* SeqList_Delete(SeqList* list, int pos); #endif
//Cpp #include "seqList.h"
#include <iostream>
using namespace std;
//定义一个链表头
typedef struct _tag_SeqList
{
int length;
int capacity;
unsigned int **node; // 使用**防止数据丢失
}TSeqList; //创建一个线性表
SeqList* SeqList_Create(int capacity)
{
int ret = ;
TSeqList *temp = NULL; //创建临时指针变量remp temp = (TSeqList *)malloc((sizeof(TSeqList))); //分配内存
//异常处理
if (temp == NULL)
{
ret = -;
cout << "SeqList_Create Err!" << endl;
return NULL;
}
memset(temp, , sizeof(TSeqList)); // 快速填充为0 //为所有节点分配内存空间 (*容量)
temp->node = (unsigned int **)malloc(sizeof(unsigned int *) * capacity); if (temp->node == NULL)
{
ret = -;
cout << "SeqList_Create Err! (malloc..)" << endl;
return NULL;
} temp->capacity = capacity;
temp->length = ;
return temp;
} //删除(释放)一个线性表
void SeqList_Destory(SeqList* list)
{
TSeqList *tlist = NULL;
//判断是否为空
if(list == NULL)
{
cout << "Destory Err" << endl;
return ;
}
tlist = (TSeqList *)list;
//先释放node内存 再释放List内存
if (tlist->node != NULL)
{
free(tlist->node);
}
free(tlist); return;
} //清空一个线性表
void SeqList_Clear(SeqList* list)
{
TSeqList *tlist = NULL;
if(list == NULL)
{
cout << "Clear Err " << endl;
return ;
}
//直接将链表的长度重置为0
tlist = (TSeqList *)list;
tlist->length = ;
return;
} //返回一个线性表的长度
int SeqList_Length(SeqList* list)
{
TSeqList *tlist = NULL;
//异常处理
if(list == NULL)
{
cout << "Length Err, list == NULL" << endl;
return -;
} tlist = (TSeqList *)list;
return tlist->length;
} //返回线性表的容量
int SeqList_Capacity(SeqList* list)
{
TSeqList *tlist = NULL;
//异常处理
if(list == NULL)
{
cout << "Capacity Err, list == NULL" << endl;
return -;
}
tlist = (TSeqList *)list; return tlist->capacity;
} //在POS位置插入一个节点
int SeqList_Insert(SeqList* list, SeqListNode* node, int pos)
{
int i = ;
int ret = ;
TSeqList *tlist = NULL; //异常处理
if(list == NULL || node == NULL || pos<)
{
cout << "Insert Err, (list == NULL || node == NULL || pos<0)" << endl;
ret = -;
return ret;
}
tlist = (TSeqList *)list; //判断是否满了
if (tlist->length >= tlist->capacity)
{
cout << "满了" << endl;
ret = -;
return ret;
}
//容错修正 如果插入的pos位置大于链表长度 并且此时容量未满
if (pos >= tlist->length) /////////
{
pos = tlist->length;
} //位置后移并储存
for(i = tlist->length; i > pos; i--)
{
tlist->node[i] = tlist->node[i-];
}
//将node[i]改为node
tlist->node[i] = (unsigned int*) node;
tlist->length++;
return ; } //获取节点信息
SeqListNode* SeqList_Get(SeqList* list, int pos)
{
TSeqList *tlist = NULL;
//异常处理
if(list == NULL || pos < )
{
cout << "SeqList_Get Err, list == NULL" << endl;
return NULL ;
}
tlist = (TSeqList *)list;
//强制类型转换
return (SeqListNode*)tlist->node[pos];
} //删除一个节点
SeqListNode* SeqList_Delete(SeqList* list, int pos)
{
TSeqList *tlist = NULL;
SeqListNode* ret = ;
//异常处理
if(list == NULL || pos < )
{
cout << "Delete Err" << endl;
return NULL ;
}
tlist = (TSeqList *)list;
//将POS位置的node地址给临时ret指针变量
ret = (SeqListNode*)tlist->node[pos];
//pos 位置后的元素往前移动
for(int i = pos + ; i < tlist->length; i++)
{
tlist->node[i - ] = tlist->node[i];
}
tlist->length--;
//返回删除的元素
return ret;
}
测试代码:
#include "User.h"
#include "seqList.h"
#include <iostream> using namespace std;
//定义一个结构体
typedef struct Teacher
{
int age;
char name[]; }teacher; int main0()
{
int ret = ;
int i = ;
//创建顺序链表
SeqList* list = NULL;
list = SeqList_Create(); teacher t1,t2,t3,t4,t5; t1.age = ;
t2.age = ;
t3.age = ;
t4.age = ;
t5.age = ;
//头插法插入元素
ret = SeqList_Insert(list, (SeqListNode*) &t1, ); //测试容错
ret = SeqList_Insert(list, (SeqListNode*) &t3, );
ret = SeqList_Insert(list, (SeqListNode*) &t4, );
ret = SeqList_Insert(list, (SeqListNode*) &t5, );
//遍历元素并显示age
for (;i < SeqList_Length(list); i++)
{
teacher* temp = (teacher*) SeqList_Get(list, i);
if (temp == NULL)
{
return ;
}
//cout << "temp->age: " << ((teacher* )SeqList_Get(list, i))->age << endl;
cout << "temp->age: " << temp->age << endl;
}
cout << endl;
//删除 并显示删除结果
int m = ;
while (SeqList_Length(list) > )
{
teacher *t = NULL;
t = (teacher*)SeqList_Delete(list, );
cout << "第" << m << "次头删的数据" << endl;
cout << "t->age" << t->age << endl;
m++;
cout << endl;
} //每次遍历删除头部后的链表
/*int m = 1;
while (SeqList_Length(list) > 0)
{
SeqList_Delete(list, 0);
cout << "第" << m << "次头删的结果" << endl;
for (i = 0;i < SeqList_Length(list); i++)
{
teacher* temp = (teacher*) SeqList_Get(list, i);
//cout << "temp->age: " << ((teacher* )SeqList_Get(list, i))->age << endl;
cout << "temp->age: " << temp->age << endl;
}
m++;
cout << endl;
}
*/
system("pause");
return ;
}
顺序链表的C风格实现的更多相关文章
- java实现顺序链表
C&C++是那么难学,以至于我连指针是什么都不知道.所以只能学习java了. 如今想用java实现N年前学过“数据结构(c语言版)”却又是那么吃力! 慢慢练吧! 写此博客,仅标记自己学过数据结 ...
- 顺序链表(C++)
顺序表结构 struct Sq_list { ]; int length; }; 创建并初始化顺序表 int Init_list(Sq_list *L) { L->length = ; ; } ...
- 基于顺序链表的栈的顺序存储的C风格实现
头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...
- 链式链表的C风格实现
头文件: #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef void LinkList; //将数据的类型分离,相当于句柄 //只是一个小节点 包含着 ...
- [leetcode]21Merge Sorted ListNode递归合并顺序链表
/** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...
- 删除顺序链表中重复的数 (一) leecode
Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...
- java实现顺序表、链表、栈 (x)->{持续更新}
1.java实现节点 /** * 节点 * @luminous-xin * @param <T> */ public class Node<T> { T data; Node& ...
- 转:从《The C Programming Language》中学到的那些编程风格和设计思想
这儿有一篇写的很好的读后感:http://www.cnblogs.com/xkfz007/articles/2566424.html 读书不是目的,关键在于思考. 很早就在水木上看到有人推荐& ...
- 由后序遍历结果构造二叉查找树 && 二叉查找树链表化
二叉查找树通俗说就是左孩子比父亲小,右孩子比父亲大.构造这么一个树,树嘛,递归即可. 例如一棵树后序遍历是这样(下图的树):2 9 8 16 15 10 25 38 45 42 30 20.最后的20 ...
随机推荐
- Unity3d开发集成Google Admob广告增加收入
在Unity游戏中植入广告是Unity 游戏产品增加收入的一种重要方式,常用的广告有谷歌Admob,百度ssp,腾讯广点通,unity公司的unityads等等,而使用的最多的应该属于谷歌Admob, ...
- 【起航计划 021】2015 起航计划 Android APIDemo的魔鬼步伐 20 App->Intents createChooser
Intents 这个例子的代码非常简单: public void onGetMusic(View view) { Intent intent = new Intent(Intent.ACTION_GE ...
- X-Cart-5.3.1.4 (Ubuntu 16.04)
平台: Ubuntu 类型: 虚拟机镜像 软件包: x-cart-5.3.1.4 commercial ecommerce open-source x-cart 服务优惠价: 按服务商许可协议 云服务 ...
- redis笔记(一)
redis安装 ubuntu: apt-get install redis-server redis服务 :redis-server 启动客户端 :redis-cli redis简单知识 ...
- webpack踩坑
1.当你用webpack2实现css文件单独成一个文件的时候: 可能遇到这种错误Error: Breaking change: extract now only takes a single argu ...
- IOS transform的使用(移动,放大,旋转)
@interface ViewController () - (IBAction)up; - (IBAction)big ; - (IBAction)leftRotate ; @property (n ...
- IOS xcode 离线帮助文档安装和安装路径
将想要安装的xcode 帮助文档 版本,将 ‘帮助文档包’ 放入‘xcode 帮助文档安装路径’.再将Xcode软件重启. xcode 帮助文档安装路径: 在‘应用程序’->Xcode软件 右击 ...
- 一款带有CSS的单选框以及选中事件
html <div class="radio radio-success"> <input type=" name="radioSingle1 ...
- 课程设计__继承与派生,重载<<
///继承与派生 #include <iostream> using namespace std; class Point { public: Point (,):x(a),y(b) {} ...
- 2017.9.24 JSP动态页面
1.1 JSP(Java Server Page)是一种运行在服务器端的脚本语言,用来开发动态网页的开发技术. 1.2 JSP页面的结构 JSP页面主要由HTML和JSP代码构成,JSP代码是通过&q ...