当前位置:网站首页>Establishment, addition, deletion, modification and query of sequence table
Establishment, addition, deletion, modification and query of sequence table
2022-04-22 05:28:00 【Li Feiyu_ smile】
C Establishment, addition, deletion, modification and query of language version sequence table
From data structure C Establishment, addition, deletion, modification and query of sequence table in the second edition of language
Article content : Establishment of sequence table , Value , lookup , modify , Insert , Delete .
Two 、 Use steps
1. Import and stock in
The code is as follows ( Example ):
#include<stdio.h> // Call basic input / Output library file
#include<stdlib.h> // Call the standard library header file ,malloc The function is in this header file
#include<process.h> // Use system library files , Pause for a moment , See the execution result
Last code
The core idea : Insert and delete , In the sequence table , Just move the batch to complete , It's just a matter of moving forward and backward . effect : Fixed length of array is solved , And the disadvantages of not being able to insert and delete operations .
Construction of sequence table
// Construction of sequence table
#include<stdio.h>
#include<stdlib.h> //malloc The function is in this header file library
#include<process.h> // This is used for system pause
#define MAX 100 // Maximum length of sequence table
typedef struct Seq
{
int elem[MAX]; // Array elements
int length; // length
}RSeq;
// Build a sequence table
RSeq init()
{
RSeq *R1;
R1 = (struct Seq*)malloc(sizeof(struct Seq));
R1->length = 0;
for (int i = 0; i < 10; i++)
{
R1->elem[i] = i + 1;
R1->length++;
}
return *R1;
}
// Value
int Seq_getValue(RSeq R, int index)
{
if (index < 0 && index > R.length)
{
printf(" Illegal location ");
exit(0);
}
return R.elem[index];
}
// lookup
int Seq_locateElem(RSeq R, int elem)
{
int flag = 0;
for (int i = 0; i < R.length; i++) // In order to find , Traverse the sequence table
{
if (R.elem[i] == elem)
{
return i;
}
}
return -1; // To find the failure
}
// Insert
void Seq_insert(RSeq* L, int index, int elem)
{
if (index < 0 && index > L->length)
{
printf(" Illegal location ");
exit(0);
}
else {
for (int i = L->length - 1; i >= index; i--)
L->elem[i + 1] = L->elem[i];
// Add an empty position and move back , stay index Back
L->elem[index] = elem;
L->length++;
}
}
// modify
void Seq_revise(RSeq *L, int index, int value)
{
if (index < 0 && index > L->length)
{
printf(" Illegal location ");
exit(0);
}
else
{
L->elem[index] = value;
}
}
// Delete
void Seq_delete(RSeq* L, int k) //k Is the location of the deletion
{
if (k < 0 && k > L->length)
{
printf(" Illegal location ");
exit(0);
}
else
{
for (int i = k; i <= L->length - 1; i++)
L->elem[i] = L->elem[i+1]; // Move forward to fill the vacancy
L->length--;
}
}
// Print the results
void display(RSeq R)
{
for (int i = 0; i < R.length; i++)
{
printf("%4d", R.elem[i]);
}
}
int main()
{
RSeq A;
A = init(); // Construct a sequence table
printf(" The original sequence table :");
display(A);
printf("\n");
printf(" Sequence table value :%d\n",Seq_getValue(A, 2));
printf(" The sequence table lookup value is 7 The subscript :%d\n", Seq_locateElem(A,7));
printf(" Sequence table after insertion :");
Seq_insert(&A, 4, 13);
display(A);
printf("\n Change the subscript of the sequence table to 2 The sequence table after the value of :");
Seq_revise(&A, 2, 11);
display(A);
printf("\n Delete the subscript of the sequence table as 2 and 4 The sequence table after the value of :");
Seq_delete(&A, 2);
Seq_delete(&A, 3);
// After deleting one, the table length is reduced by one , Subscripts after the last deletion will be affected , Not in the front .
display(A);
system("pause");
return 0;
}
/* The result is : The original sequence table : 1 2 3 4 5 6 7 8 9 10 Sequence table value :3 The sequence table lookup value is 7 The subscript :6 Sequence table after insertion : 1 2 3 4 13 5 6 7 8 9 10 Change the subscript of the sequence table to 2 The sequence table after the value of : 1 2 11 4 13 5 6 7 8 9 10 Delete the subscript of the sequence table as 2 and 4 The sequence table after the value of : 1 2 4 5 6 7 8 9 10 */
版权声明
本文为[Li Feiyu_ smile]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204210618260437.html
边栏推荐
- GBase 8s V8. 8 SQL Guide: tutorial-5.2 (3)
- Mengxin sees the recruitment of volunteers in the open source community of wedatasphere
- Visitor mode from 2022-1-3 to 2022-1-16
- GBase 8s V8.8 SQL 指南:教程-6.1
- GBase 8s V8.8 SQL 指南:教程-5.3
- [network protocol] why learn network protocol
- 【CANdelaStudio编辑CDD】-2.3-实现$27服务多个SecurityLevel之间的跳转(UDS诊断)
- Three locking methods of the bottom layer of synchronized: (3.21-3.27)
- Neural network -- basic idea
- Configure security policy on ENSP
猜你喜欢

NP, OSPF neighbor adjacency

Multithreaded rendering mechanism of Unreal Engine

AWD platform construction – cardinal

Apache poi HSSF operation Excel

使用easyexcel导出excel表格
![[WPF] use ellipse or rectangle to make circular progress bar](/img/5b/6153e0decf612f2a1c28093d595bb2.png)
[WPF] use ellipse or rectangle to make circular progress bar

Four startup modes of activity

libcurl等第三方库通用编译方法

IO流..

Intermediary model (3.28-4.3)
随机推荐
abcabc
mysql表删除重复值,只保留一个
[C] file operation
GBase 8s V8.8 SQL 指南:教程-6.1.1(4)
TCP three handshakes and four waves (2022.4.18-4.24)
Codeforces Round #781 (Div. 2) ABCD
Integer源码
Fundamentals of graphics - Ambient Occlusion
GBase 8s V8.8 SQL 指南:教程-5.2(3)
Measuring the impact of a successful DDoS attack on the customer behavior of managed DNS servi
One click evaluation course script of educational administration management information system
GBase 8s V8. 8 SQL Guide: Tutorial - 6.1.1 (4)
GBase 8s V8.8 SQL 指南:教程-5.3
Codeforces Round #784 (Div. 4) All Problems
用minscript脚本语言实现数学函数曲线编辑器
Spark 之 unsafeRow
GBase 8s V8. 8 SQL Guide: tutorial-5.3
Complexité temporelle et spatiale
[C #] remodeling matrix (staggered array)
Linked list (C language)