DS-001 27 数据结构:顺序表及代码实现
时间:2019-02-14
本文章向大家介绍DS-001 27 数据结构:顺序表及代码实现,主要包括DS-001 27 数据结构:顺序表及代码实现使用实例、应用技巧、基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋友可以参考一下。
Data Structure: Code Implementation of Sequential table
1 Construction of the sequential table
1.1 Construct a sequential table
In order to facilitate the later use of the data in the table, the sequential table needs to record the following two items:
(1) The storage capacity applied by the sequential table;
(2) The length of the sequential table (the number of elements stored in the table).
typedef struct Table{
int * head; // Declares an array of indeterminate length: head (a dynamic array)
int length; // Records the length of the current sequential table
int size; // Record the storage capacity allocated by the sequential table
}table;
1.2 Initialize the sequential table
#define Size 5 // Macro definition of 'Size'(To represent the space request of the sequential table
table initTable(){
table t;
t.head=(int*)malloc(Size*sizeof(int)); // Construct an empty sequential table, dynamically apply for storage space
if (!t.head) // If failed, prompt and quit
{
printf("Initialization failed");
exit(0);
}
t.length=0; // Initialize the length of sequential table
t.size=Size; // Initialize the size of storage space
return t;
}
1.3 Output
Now let’s try to out put the sequential table.
void displayTable(table t){
for (int i=0;i<t.length;i++) {
printf("%d ",t.head[i]);
}
printf("\n");
}
int main(){
table t=initTable();
for (int i=1; i<=Size; i++) { // Add elements to the sequential table
t.head[i-1]=i;
t.length++;
}
printf("The elements stored in the sequential table are:\n");
displayTable(t);
return 0;
}
Output:
2 Basic operations of sequential table
2.1 Insert elements
// Insert function (elem: inserted element, add: the position of the inserted place)
table addTable(table t,int elem,int add)
{
if (add>t.length+1||add<1) { // If the position doesn't exist, quit
printf("Wrong position!");
return t;
}
if (t.length==t.size) { // Determines whether the sequential table has excess storage space, if not, apply for it
t.head=(int *)realloc(t.head, (t.size+1)*sizeof(int));
if (!t.head) {
printf("Storage allocation failed"); // If failed, quit
return t;
}
t.size+=1;
}
for (int i=t.length-1; i>=add-1; i--) { // Insert the element(Move back the subsequent elements of the inserted position)
t.head[i+1]=t.head[i];
}
t.head[add-1]=elem; // Now insert the element
t.length++; // The length should add one
return t;
}
2.2 Delete elements
table delTable(table t,int add){
if (add>t.length || add<1) {
printf("Wrong position!");
exit(0);
}
for (int i=add; i<t.length; i++) { // Delete operation
t.head[i-1]=t.head[i];
}
t.length--;
return t;
}
2.3 Find the element
//Lookup function (elem: the value of the target element)
int selectTable(table t,int elem){
for (int i=0; i<t.length; i++) {
if (t.head[i]==elem) {
return i+1;
}
}
return -1; // If failed, return -1
}
2.4 Alter the element
table amendTable(table t,int elem,int newElem){ // elem->newElem
int add=selectTable(t, elem);
t.head[add-1]=newElem; // Change the element
return t;
}
2.5 Check the code
Let’s see how it works:
int main(){
table t1=initTable();
for (int i=1; i<=Size; i++) {
t1.head[i-1]=i;
t1.length++;
}
printf("Origial sequential table:\n");
displayTable(t1);
printf("Delete element 1:\n");
t1=delTable(t1, 1);
displayTable(t1);
printf("Insert element 5 at position 2:\n");
t1=addTable(t1, 5, 2);
displayTable(t1);
printf("Look for element 3:\n");
int add=selectTable(t1, 3);
printf("%d\n",add);
printf("Change element 3 for 6:\n");
t1=amendTable(t1, 3, 6);
displayTable(t1);
return 0;
}
Out put:
- 【Go 语言社区】HTML5 canvas验证码识别
- 迁移式升级的测试(二)(r10笔记第35天)
- Golang实现图片缩放服务器
- mongodb数据结构与基本操作增删改查整理(二)
- 使用在线重定义重构亿级分区表(r10笔记第34天)
- 【Go 语言社区】一个WebSocket的简单Echo例子
- Java基础-day10-代码题-继承&抽象类
- 闪回区空间不足引发的SQL问题分析(r10笔记第32天)
- JavaScript Window - 浏览器对象模型
- 纯CSS实现的圆角折叠菜单特效代码
- MySQL和Oracle中的半连接测试总结(一)(r10笔记第31天)
- 【Go 语言社区】关于select和channel数组的配合使用--转
- Java基础-day09-代码题-对象;类;封装
- MySQL replace into的使用细则(r10笔记第48天)
- JavaScript 教程
- JavaScript 编辑工具
- JavaScript 与HTML
- JavaScript 与Java
- JavaScript 数据结构
- JavaScript 基本数据类型
- JavaScript 特殊数据类型
- JavaScript 运算符
- JavaScript typeof 运算符
- JavaScript 表达式
- JavaScript 类型转换
- JavaScript 基本语法
- JavaScript 注释
- Javascript 基本处理流程
- Javascript 选择结构
- Javascript if 语句
- Javascript if 语句的嵌套
- Javascript switch 语句
- Javascript 循环结构
- Javascript 循环结构实例
- Javascript 跳转语句
- Javascript 控制语句总结
- Javascript 函数介绍
- Javascript 函数的定义
- Javascript 函数调用
- Javascript 几种特殊的函数
- JavaScript 内置函数简介
- Javascript eval() 函数
- Javascript isFinite() 函数
- Javascript isNaN() 函数
- parseInt() 与 parseFloat()
- escape() 与 unescape()
- Javascript 字符串介绍
- Javascript length属性
- javascript 字符串函数
- Javascript 日期对象简介
- Javascript 日期对象用途
- Date 对象属性和方法
- Javascript 数组是什么
- Javascript 创建数组
- Javascript 数组赋值与取值
- Javascript 数组属性和方法