当前位置:网站首页>Basic operation of sequence table
Basic operation of sequence table
2022-06-24 20:27:00 【Learning The Monk】
Catalog
One 、 The experimental requirements
One 、 The experimental requirements
1、 Confirmatory experiments : Realize the basic operation of sequence table
Experimental content : Write a program sqlist.cpp ( or .c),
Realize various basic operations of the sequence table and the overall table building algorithm ( Suppose the content type of the sequence table ElemType by char),
On this basis, a program is designed exp1.cpp ( or .c) Complete the function .
(1) Initialization sequence table L.
(2) Put the element a、b、c、d、e Insert sequence table in sequence L in .
(3) Output sequence table L.
(4) Output sequence table L The length of .
(5) Judgment sequence table L Is it empty .
(6) Output sequence table L Of the 3 Elements .
(7) Output elements a The location of .
(8) In the 4 Insert elements... At element locations f.
(9) Output sequence table L.
(10) Delete order table L Of the 3 Elements .
(11) Output sequence table L.
(12) Destruction sequence table L.
Two 、 Code implementation
/*
1、 Confirmatory experiments : Realize the basic operation of sequence table
Experimental content : Write a program sqlist.cpp ( or .c),
Realize various basic operations of the sequence table and the overall table building algorithm ( Suppose the content type of the sequence table ElemType by char),
On this basis, a program is designed exp1.cpp ( or .c) Complete the function .
(1) Initialization sequence table L.
(2) Put the element a、b、c、d、e Insert sequence table in sequence L in .
(3) Output sequence table L.
(4) Output sequence table L The length of .
(5) Judgment sequence table L Is it empty .
(6) Output sequence table L Of the 3 Elements .
(7) Output elements a The location of .
(8) In the 4 Insert elements... At element locations f.
(9) Output sequence table L.
(10) Delete order table L Of the 3 Elements .
(11) Output sequence table L.
(12) Destruction sequence table L.
*/
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100 // Maximum allocated capacity of linear table
#define LISTINCREMENT 10 // Linear table expansion capacity
typedef char ElemType; // The data element is of character type
typedef struct
{
/* data */
ElemType *data;
int length;
int listsize;
}sqlist;
// Initialization sequence table
int InitList(sqlist *L)
{
// Dynamic allocation :malloc Function to apply for a continuous piece of storage space
L->data=(ElemType*)malloc(LIST_INIT_SIZE*sizeof(ElemType));
if(!L->data)
{
return ERROR;
}
L->length=0; // The initial table length is 0
L->listsize=LIST_INIT_SIZE;
return OK;
}
// Put the element e Insert into the sequence table L Of the k A place .
int List_Insert(sqlist *L,int k,ElemType e)
{
int i=0;
if(!L->data)
return ERROR;
if (k>L->length+1|| k<1)// Determine the legitimacy of the insertion position
{
return ERROR;
}
if (L->length >= L->listsize)
{
char *new_base; // The space is insufficient , Redistribution
new_base = (char*)realloc(L->data, sizeof(char)*(L->listsize + LISTINCREMENT));
L->data = new_base;
if (!L->data)
{
return ERROR;
}
L->data = new_base;
L->listsize += LISTINCREMENT;
}
// Insert elements e
if(k<L->length)
{
for (i = L->length - 1 ; i>= k - 1; i--)
L->data[i + 1] = L->data[i];
}
L->data[k - 1] = e;
L->length++; // One more element length plus 1
return OK;
}
// Output sequence table L.
int print_List_data(sqlist *L)
{
if(!L->data)
return ERROR;
for (int i = 0; i < L->length; i++)
{
/* code */
printf("%c ",L->data[i]);
}
printf("\n");
return OK;
}
// Output sequence table L The length of .
int print_List_Length(sqlist *L)
{
if(!L->data)
return ERROR;
printf("%d\n",L->length);
return OK;
}
// Judgment sequence table L Is it empty .
int ListEmpty(sqlist *L)
{
if(!L->data||L->length!=0)
return ERROR;
else
return OK;
}
// Output sequence table L Of the i Elements .( This element e It needs to be returned to the user )
int GetElem(sqlist *L, int i, ElemType *e)
{
if (!L->data)
{
return ERROR;
}
if (i < 1 || i >= L->length-1)
{
return ERROR; //i Illegal value
}
*e =L->data[i-1];
return OK;
}
// Output elements a The location of .( This element e It does not need to be returned to the user )
int LocateElem(sqlist *L, char e)
{
if (!L->data)
{
return ERROR;
}
for (int i = 0; i < L->length; i++)
{
if (L->data[i]==e)
{
return i+1; // Returns the location of the element
}
}
return OK;
}
// Delete order table L Of the i Elements .( This element e It does not need to be returned to the user )
int ListDelete(sqlist *L, int i, char e)
{
int k;
if (!L->data)
{
return ERROR;
}
if (i<1 || i>L->length)
{
return ERROR; //i Illegal value
}
e = L->data[i - 1];
for (k = i; k <= L->length ;k++)
L->data[k - 1] = L->data[k];
L->length--;
return OK;
}
// Destruction sequence table L.
int DestroyList(sqlist *L)
{
if (!L->data)
{
return ERROR; // Table does not exist
}
else
free(L->data); // Free memory Destroy the linear table
printf(" Linear table destroyed successfully !");
return 0;
}
int main()
{
sqlist L;
ElemType e;
printf("1: Initialization sequence table L\n");
InitList(&L);
printf("2: Insert... By tail interpolation in turn a , b , c , d , e Elements \n");
List_Insert(&L, 1, 'a');
List_Insert(&L, 2, 'b');
List_Insert(&L, 3, 'c');
List_Insert(&L, 4, 'd');
List_Insert(&L, 5, 'e');
printf("3: The order sheet L:");
print_List_data(&L);
printf("4: The linear table L The watch is :");
print_List_Length(&L);
printf("5: The order sheet L by :%s\n", (ListEmpty(&L) ? " empty " : " Non empty "));
GetElem(&L, 3, &e);
printf("6: The first in the sequence 3 The element is :%c\n", e);
printf("7: The linear table L Medium element a The location of the for : %d\n", LocateElem(&L, 'a'));
printf("8: In the 4 Insert at element locations f Elements \n");
List_Insert(&L, 4, 'f');
printf(" The order sheet L by :");
print_List_data(&L);
printf("9: Delete L Of the 3 Elements \n");
ListDelete(&L, 3, e);
printf(" The order sheet L by :");
print_List_data(&L);
printf("10: Destruction sequence table L\n");
DestroyList(&L);
return 0;
}3、 ... and 、 Running results

边栏推荐
- With its own cells as raw materials, the first 3D printing ear transplantation was successful! More complex organs can be printed in the future
- 对“宁王”边卖边买,高瓴资本“高抛低吸”已套现数十亿
- Bytebase rejoint la communauté de base de données open source d'alicloud polardb
- DX12引擎开发课程进度-这个课程到底讲到哪里了
- Compressed list of redis data structures
- 1、 Downloading and installing appium
- 年轻人捧红的做饭生意经:博主忙卖课带货,机构月入百万
- 【CANN文档速递06期】初识TBE DSL算子开发
- Map跟object 的区别
- 首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
猜你喜欢

Bytebase加入阿里云PolarDB开源数据库社区
![[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map](/img/3a/db240deb4c66b219ef86f40d4c7b7d.png)
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map

"Super point" in "Meng Hua Lu", is the goose wronged?

Application practice | massive data, second level analysis! Flink+doris build a real-time data warehouse scheme

云计算发展的 4 个阶段,终于有人讲明白了

Map跟object 的区别

Redis installation of CentOS system under Linux, adding, querying, deleting, and querying all keys

Information theory of popular science Shannon

Cooking business experience of young people: bloggers are busy selling classes and bringing goods, and the organization earns millions a month

Zadig + cave Iast: let safety dissolve in continuous delivery
随机推荐
Uninstall tool v3.5.10.5670 single file portable official version
字节、腾讯也下场,这门「月赚3000万」的生意有多香?
The latest simulated question bank and answers of the eight members (Electrical constructors) of Sichuan architecture in 2022
《梦华录》“超点”,鹅被骂冤吗?
Stop using system Currenttimemillis() takes too long to count. It's too low. Stopwatch is easy to use!
Some ideas about chaos Engineering
R for Data Science (notes) -- data transformation (used by filter)
大一女生废话编程爆火!懂不懂编程的看完都拴Q了
Ribbon source code analysis @loadbalanced and loadbalancerclient
Bytebase 加入阿里云 PolarDB 开源数据库社区
实现基于Socket自定义的redis简单客户端
Oracle create tablespaces and tables
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
Apache+php+mysql environment construction is super detailed!!!
【Go語言刷題篇】Go從0到入門4:切片的高級用法、初級複習與Map入門學習
Bytebase joins Alibaba cloud polardb open source database community
Huawei cloud modelarts has ranked first in China's machine learning public cloud service market for the fourth time!
Jsup supports XPath
Jd.com: how does redis implement inventory deduction? How to prevent oversold?
顺序表的基本操作