顺序链表的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 ...
随机推荐
- System Center Configuration Manager 2016 配置安装篇(Part3)
SCCM 2016 配置管理系列(Part 1- 4) 介绍AD01上配置了Active Directory域服务(ADDS),然后将Configuration Manager服务器(CM16)加入到 ...
- CRUD全栈式编程架构之导入导出的设计
using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.We ...
- IOS笔记 : 一些小技巧
计算单元格高度,在自定义cell中 -(void) resizeTheHeight{ CGFloat contentWidth = 280; UIFont *font = [UIFont fontWi ...
- 如果有反向代理的情况下,获取最原始的IP的办法
HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_HOST"];
- 请教Nutzwk项目,在beetl页面怎么用shiro标签呢?
请教Nutzwk项目,在beetl页面怎么用shiro标签呢? 发布于 381天前 作者 WenTao-Love 195 次浏览 复制 上一个帖子 下一个帖子 标签: nutzwk 如题 ...
- HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序
HashMap通过hashcode对其内容进行快速查找,而 TreeMap中所有的元素都保持着某种固定的顺序,如果你需要得到一个有序的结果你就应该使用TreeMap(HashMap中元素的排列顺序是不 ...
- ELF格式文件符号表全解析及readelf命令使用方法
http://blog.csdn.net/edonlii/article/details/8779075 1. 读取ELF文件头: $ readelf -h signELF Header: Magi ...
- 【CCPC-Wannafly Winter Camp Day4 (Div1) F】小小马(分类讨论)
点此看题面 大致题意: 给你一张\(n*m\)的棋盘,问你一匹马在两个点中是否存在一条经过黑白格子数目相等的路径. 简化题目 首先,我们来简化一下题目. 考虑到马每次走的时候,所经过的格子的颜色必然发 ...
- 创建 XXXXXXXX 的配置节处理程序时出错: 请求失败
今天碰到这个错误,之前的程序在测试的时候都没有问题,同样的程序打包通过QQ传给其他人,在XP下测试也没有问题,我的Win7系统从QQ信箱下载压缩包,解压之后执行程序就会出问题,本来还是考虑自己程序是不 ...
- P2447 [SDOI2010]外星千足虫
怎么说呢? 因为是在mod 2 意义下的吗(一般是遇到二就可能是位运行算或二分图) 就可以利用异或计算. 因为奇数和偶数在二进制上就用判断最后一位就可以了 然后因为异或符合交换律和结合律 直接消元就可 ...