当前位置:网站首页>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

边栏推荐
- Using dynamic time warping (DTW) to solve the similarity measurement of time series and the similarity identification analysis of pollution concentration in upstream and downstream rivers
- R for Data Science (notes) -- data transformation (used by filter)
- Confirm whether the host is a large terminal or a small terminal
- To open the registry
- Predicate
- 【CANN文档速递04期】揭秘昇腾CANN算子开发
- Huawei cloud modelarts has ranked first in China's machine learning public cloud service market for the fourth time!
- Ribbon source code analysis @loadbalanced and loadbalancerclient
- 1、 Downloading and installing appium
- Q1: error in JMeter filename must not be null or empty
猜你喜欢

二叉树的基本性质与遍历

Teach you how to view the number of connected people on WiFi in detail how to view the number of connected people on WiFi
![[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer](/img/14/0313857adc178ecee4c866a05e54aa.jpg)
[video tutorial] functions that need to be turned off in win10 system. How to turn off the privacy option in win10 computer

顺序表的基本操作

Stackoverflow 年度报告 2022:开发者最喜爱的数据库是什么?

物联网?快来看 Arduino 上云啦

Steering gear control (stm32f103c8t6)

Internet of things? Come and see Arduino on the cloud

The Network Security Review Office launched a network security review on HowNet, saying that it "has a large amount of important data and sensitive information"

宅男救不了元宇宙
随机推荐
开放可编程基础设施(OPI)项目,重新定义DPU/IPU
Some ideas about chaos Engineering
Why is the executor thread pool framework introduced
Eureka source code shallow reading - automatic fault removal
16个优秀业务流程管理工具
Install the custom module into the system and use find in the independent project_ Package found
【CANN文档速递04期】揭秘昇腾CANN算子开发
[go language questions] go from 0 to entry 4: advanced usage of slice, elementary review and introduction to map
Methods for comparing float types in the kernel
Huawei cloud modelarts has ranked first in China's machine learning public cloud service market for the fourth time!
Coinbase将推出首个针对个人投资者的加密衍生产品
京东一面:Redis 如何实现库存扣减操作?如何防止商品被超卖?
Introduction: continuously update the self-study version of the learning manual for junior test development engineers
别再用 System.currentTimeMillis() 统计耗时了,太 Low,StopWatch 好用到爆!
托管服务与SASE,纵享网络与安全融合 | 一期一会回顾
【CANN文档速递05期】一文让您了解什么是算子
首个大众可用PyTorch版AlphaFold2复现,哥大开源OpenFold,star量破千
First understand redis' data structure - string
Microsoft Office Excel 2013 2016 graphic tutorial on how to enable macro function
VXLAN 与 MPLS:从数据中心到城域以太网