//头文件
#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风格实现的更多相关文章

  1. java实现顺序链表

    C&C++是那么难学,以至于我连指针是什么都不知道.所以只能学习java了. 如今想用java实现N年前学过“数据结构(c语言版)”却又是那么吃力! 慢慢练吧! 写此博客,仅标记自己学过数据结 ...

  2. 顺序链表(C++)

    顺序表结构 struct Sq_list { ]; int length; }; 创建并初始化顺序表 int Init_list(Sq_list *L) { L->length = ; ; } ...

  3. 基于顺序链表的栈的顺序存储的C风格实现

    头文件: #ifndef _SEQSTACK_H_ #define _SEQSTACK_H_ typedef void SeqStack; //创建一个栈 SeqStack* SeqStack_Cre ...

  4. 链式链表的C风格实现

    头文件: #ifndef _LINKLIST_H_ #define _LINKLIST_H_ typedef void LinkList; //将数据的类型分离,相当于句柄 //只是一个小节点 包含着 ...

  5. [leetcode]21Merge Sorted ListNode递归合并顺序链表

    /** * Merge two sorted linked lists and return it as a new list. * The new list should be made by sp ...

  6. 删除顺序链表中重复的数 (一) leecode

    Given a sorted linked list, delete all duplicates such that each element appear only once. For examp ...

  7. java实现顺序表、链表、栈 (x)->{持续更新}

    1.java实现节点 /** * 节点 * @luminous-xin * @param <T> */ public class Node<T> { T data; Node& ...

  8. 转:从《The C Programming Language》中学到的那些编程风格和设计思想

    这儿有一篇写的很好的读后感:http://www.cnblogs.com/xkfz007/articles/2566424.html   读书不是目的,关键在于思考.   很早就在水木上看到有人推荐& ...

  9. 由后序遍历结果构造二叉查找树 && 二叉查找树链表化

    二叉查找树通俗说就是左孩子比父亲小,右孩子比父亲大.构造这么一个树,树嘛,递归即可. 例如一棵树后序遍历是这样(下图的树):2 9 8 16 15 10 25 38 45 42 30 20.最后的20 ...

随机推荐

  1. aliyun maven repository

    <mirrors> <mirror> <id>alimaven</id> <name>aliyun maven</name> & ...

  2. 服务器返回的14种常见HTTP状态码

    当我们从客户端向服务器发送请求时 服务器向我们返回状态码 状态码就是告诉我们服务器响应的状态 通过它,我们就可以知道当前请求是成功了还是出现了什么问题 状态码是由3位数字和原因短语组成的(比如最常见的 ...

  3. R语言基础命令与安装

    1. R的安装过程 1.1.首先附上清华线路的下载链接Windows版3.3.1 1.2. 选择安装路径 1.3. 注意根据自己的计算机位数选择,如我的是64位,便选择64位安装. 1.4. 其他默认 ...

  4. 【起航计划 011】2015 起航计划 Android APIDemo的魔鬼步伐 10 App->Activity->Reorder Activities 后退栈 Intent FLAG

    Reorder Activities 示例有四个相关的Activitives: ReorderOnLaunch, ReorderTwo,ReorderThree, ReorderFour.其中Reor ...

  5. react-native 视频播放器(很不错哦)

    第一步: npm i -S react-native-af-video-player(安装前:先安装: react-native-video.react-native-keep-awake.react ...

  6. nginx的常用命令

    一.nginx的解压安装 #tar xzvf nginx-1.6.0.tar.gz #cd nginx-1.6.0 #./configure    --prefix=/home/weixin/loca ...

  7. C++ Eigen库和Matlab对比

    // A simple quickref for Eigen. Add anything that's missing. // Main author: Keir Mierle #include &l ...

  8. 屏蔽各类弹窗广告(WPS、智能云输入法)

    托盘中的广告“领取双11红包,最高1111元”的罪魁祸首是“智能云输入法” 广告在托盘中闪动: 结束SCSkinInst.exe后,托盘中的广告消失: 智能云输入法的安装路径可参考: C:\Progr ...

  9. JS回调函数(理解篇)

    概述: 回调函数就是一个通过函数指针调用的函数.如果你把函数的指针(地址)作为参数传递给另一个函数,当这个指针被用来调用其所指向的函数时,我们就说这是回调函数.回调函数不是由该函数的实现方直接调用,而 ...

  10. POJ-2151 Check the difficulty of problems---概率DP好题

    题目链接: https://vjudge.net/problem/POJ-2151 题目大意: ACM比赛中,共M道题,T个队,pij表示第i队解出第j题的概率 问 每队至少解出一题且冠军队至少解出N ...