顺序链表的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 ...
随机推荐
- dos命令执行mysql的sql文件
1.cmd进入dos窗口 2.输入cd+ mysql的bin目录后进入bin文件下 3.进入控制台:mysql -uroot -proot 回车 4.选择数据库:use mydata go 回车 5 ...
- 洛谷 P1195 口袋的天空(最小生成树)
嗯... 题目链接:https://www.luogu.org/problemnew/show/P1195 思路: 首先可以判断这道题是用最小生成树来做的,然后在将其合并时用ans记录一下它的总消耗, ...
- 构建跨平台APP开发的两本书,这里重点推荐下
第一本是<构建跨平台:jquery Mobile移动应用实战> 是目前jqm开发写的比较入门的一本书,上手很快,但是高手我觉得就没有必要学习了,因为写的比较浅显. 第二本是<构建跨平 ...
- python 不同目录间的模块调用
有时候调用的模块不再同一个目录.直接import 是加载不进来的.默认的加载路径是sys.path中指定的路径.如果要指定加载的目录得需要把这个目录加到sys.path里面. 比如要加载父目录的同级目 ...
- Android程序员不容错过的10款在线实用工具
Android十款在线工具,在做Android开发过程中,会遇到一些小的问题,虽然自己动手也能解决,但是有了一些小工具,解决这些问题就得心应手了.Android在线工具,包括在线测试工具,及其他较为重 ...
- PDO链式操作——针对关键字出现问题的解决方案
例如: 1.执行一条SQL语句:查询user表中的所有数据,并通过name字段进行降序,通过age进行升序 2. 案例1: 正确的执行语句为:SELECT * FROM user ORDER BY ...
- C++ new new[]详解
精髓: operator new()完成的操作一般只是分配内存:而构造函数的调用(如果需要)是在new运算符中完成的. operator new和new 运算符是不同的,operator new只分配 ...
- 整个trick
数据输入方面:1.image pyramid 图像金字塔.目前代码里是先选取一个scale,然后在每个GPU上按照scale读图片,相应的gt也更改."scales":[440, ...
- Python 2.x 和 3.x的区别
Python有两个版本,2.x 和 3.x ,两个版本不兼容,3.x 不不考虑对2.x代码的向后兼容. 在3.x中,一些语法,内建函数和对象的行为都有所调整. 大部分的python库都支持 pytho ...
- orderBy 过滤器
orderBy 过滤器根据表达式排列数组: <!DOCTYPE html><html><head><meta http-equiv="Content ...