当前位置:网站首页>Operation 7.19 sequence table
Operation 7.19 sequence table
2022-07-25 09:35:00 【Unknown college student M】
1. Finish modifying functions by value
function code
// Modify by value
int listUpdateValue(seqList *S,datatype old_e,datatype new_e)
{
int pos=listSearchValue(S,old_e);
if(pos>=0)
{
S->data[pos]=new_e;
printf(" Modification successful \n");
return 0;
}
else
{
printf(" Modification failed \n");
return -1;
}
}
Test code
#include "seqlist.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
seqList *S = listCreate();
if(NULL==S)
{
return -1;
}
// Call add function
listAdd(S,7);listAdd(S,2);listAdd(S,5);
listAdd(S,6);listAdd(S,4);listAdd(S,4);
listAdd(S,4);listAdd(S,7);listAdd(S,8);
listShow(S);
// Call the modify by value function
listUpdateValue(S,6,9);
listShow(S);
listUpdateValue(S,1,3);
listShow(S);
}
Running results
The initial data element of the sequence table is 7 2 5 6 4 4 4 7 8
Set data element as 6 The element of is changed to 9, Modification successful
Set data element as 1 The element of is changed to 3, Modification failed

2. Complete the search function by position , Return the found data
function code
// Find by location
datatype listSearchPos(seqList *S,int pos)
{
if(pos<0||pos>=S->len)
{
printf(" Illegal location , To find the failure \n");
return -1;
}
printf(" Find success , Indexes %d The element in position is %d\n",pos,S->data[pos]);
return S->data[pos];
}
Test code
#include "seqlist.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
seqList *S = listCreate();
if(NULL==S)
{
return -1;
}
// Call add function
listAdd(S,7);listAdd(S,2);listAdd(S,5);
listAdd(S,6);listAdd(S,4);listAdd(S,4);
listAdd(S,4);listAdd(S,7);listAdd(S,8);
listShow(S);
// Call the bitwise lookup function
listSearchPos(S,2);
listSearchPos(S,9);
}
Running results
The initial data element of the sequence table is 7 2 5 6 4 4 4 7 8
Search index is 2 The elements of , Find success , The data element is 5
Search index is 9 The elements of , To find the failure

3. Complete the use of selective sorting to achieve the descending order of the sequence table
function code
// Selection sort
void listSort2(seqList *S)
{
for(int i=0;i<S->len-1;i++)
{
datatype max =S->data[i];
int index=i;
for(int j=i+1;j<S->len;j++)
{
if(max<S->data[j])
{
index=j;
max=S->data[j];
}
}
if(index!=i)
{
datatype temp = S->data[index];
S->data[index]=S->data[i];
S->data[i]=temp;
}
}
printf(" Sort success \n");
}
Test code
#include "seqlist.h"
#include <stdio.h>
#include <stdlib.h>
int main(int argc, const char *argv[])
{
seqList *S = listCreate();
if(NULL==S)
{
return -1;
}
// Call add function
listAdd(S,7);listAdd(S,2);listAdd(S,5);
listAdd(S,6);listAdd(S,4);listAdd(S,4);
listAdd(S,4);listAdd(S,7);listAdd(S,8);
listShow(S);
// Call select sort
listSort2(S);
listShow(S);
}
Running results
The initial data element of the sequence table is 7 2 5 6 4 4 4 7 8
After sorting, the result is 8 7 7 6 5 4 4 4 2

Source code link
https://github.com/WeiJiaM/seqlist.git
边栏推荐
猜你喜欢

The jar package has been launched on Alibaba cloud server and the security group has been opened, but postman still can't run. What should we do

【cf】Round 128 C. Binary String

OC--Foundation--字符串+日期和时间

OC--Foundation--数组

Redis string structure command

~5 ccf 2021-12-2 序列查询新解

STM32+HC05串口蓝牙设计简易的蓝牙音箱

TCP网络应用程序开发流程

@3-1 CCF 2020-09-1 称检测点查询

~4.2 ccf 2021-12-1 序列查询
随机推荐
Machine learning -- detailed introduction of standardscaler (), transform (), fit () in sklearn package
@2-1 CCF 2020-12-01 期末预测之安全指数
梦想启航(第一篇博客)
多态和接口
sqli-labs Basic Challenges Less1-10
【代码源】每日一题 三段式
Flex 布局语法与用例
Detailed explanation of the use of nanny scanner class
Learn redis Linux and install redis
¥1-2 例2.2 将两个集合的并集放到线性表中
TCP网络应用程序开发流程
Redis string structure command
换电脑后如何配置SSH
Deep understanding of static keyword
[GYCTF2020]Node Game
【代码源】每日一题 分数拆分
数据分析之numpy基础包
Excl batch import data, background public parsing method
【代码源】每日一题 分割(nlogn&n解法)
*6-3 节约小能手