顺序链表的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 ...
随机推荐
- sass-基础
导入: sass的导入(@import)规则和CSS的有所不同,编译时会将@import的scss文件合并进来只生成一个CSS文件. 但是如果你在sass文件中导入css文件如@import 'res ...
- <Android 基础(十)> FloatingActionButton
介绍 Source Code中的介绍如下: Floating action buttons are used for a special type of promoted action. They a ...
- 如何解决ArcGIS Runtime SDK for Android中文标注无法显示的问题
自10.2版本开始,我就一直被ArcGIS Runtime SDK for Android的中文标注无限困扰.无论是驻留于内存中的Graphic 的文本符号TextSymbol,还是新增的离线geod ...
- [COM Interop学习小结]实现一个C#调用C++的示例
最近在研究产品的架构代码,发现其中涉及到Com组件技术,即项目中的C# Project会通过Com接口来调用C++ Project中的方法,研究一下,实现一个小的例子,供自己学习. 一. 什么是COM ...
- java之Socket传递图片
客户端: package client; import java.io.BufferedInputStream; import java.io.BufferedOutputStream; import ...
- java读取指定package下的所有class
JAVA如何扫描一个包下面的所有类,并加载到内存中去? spring中有一个<context:component-scan base-package="com.controller& ...
- hive数据仓库建设
hive数据仓库建设 1.设计原生日志表 原生日志表用来存放上报的原始日志,数据经过清洗加工后会进入到各个日志表中. 1.1 创建数据库 #创建数据库 $hive>create database ...
- do..while(false)的用法总结
首先要注意: do..while(0) 代表do里面的东西至少被执行一次,在这里仅仅执行一次. 此种用法有三个用处: 代替{}代码块,实现局部作用域.在某些宏定义时非常有用: #define f(x) ...
- Last_SQL_Errno: 1050
主库上create table,从库上存在. 报错信息如下所示: Last_SQL_Errno: 1050 Last_SQL_Error: ...
- 【[TJOI2017]DNA】
[题目][https://www.lydsy.com/JudgeOnline/problem.php?id=4892] 好像用\(SAM\)做的都是\(dfs\)啊 其实这里也是搜索 如果用\(SAM ...