当前位置:网站首页>Dynamic sequence table + OJ
Dynamic sequence table + OJ
2022-04-23 03:00:00 【C_ TQH】
A sequence table is essentially an array , This paper simply realizes the addition of dynamic sequence table , Delete , check , Change .
The key is to learn the idea of sequential table implementation , The sequence table is very detailed , Think about it in many ways .
// Sequential header file
#pragma once
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
// requirement : Stored data from 0 Start , Sequential continuous storage
// The essence of a sequential table is an array , An array that can be expanded
//#define N 100
//typedef struct SeqList
//{
// int a[N];
// int size;// Record the number of data stored
//}SeqList;
// Static sequence table , There are defects
// Dynamic sequence table
typedef int SLDataType;
typedef struct SeqList
{
SLDataType* a;
int size; // Number of storage
int capacity;// Storage capacity
}SeqList;
// Sequence table initialization
void SeqListInit(SeqList* psl);
// Destroy the sequence table
void SeqListDestroy(SeqList* psl);
// Tail insertion
void SeqListPushBack(SeqList* psl, SLDataType x);
// Deletion at the end
void SeqListPopBack(SeqList* psl);
// Head insertion
void SeqListPushFront(SeqList* psl, SLDataType x);
// Head deletion
void SeqListPopFront(SeqList* psl);
// Head insertion
void SeqListPopFront(SeqList* psl);
// Print
void SeqListPrint(SeqList* psl);
// Check whether expansion is needed
void SeqListCheckCapacity(SeqList* psl);
// stay pos Position insert x
void SeqListInsert(SeqList* psl, int pos, SLDataType x);
// stay pos Location delete data
void SeqListErase(SeqList* psl, int pos);
// lookup
int SeqListFind(SeqList* psl, SLDataType x);
#include "SeqList.h"
// initialization
void SeqListInit(SeqList* psl)
{
assert(psl);
psl->a = NULL;
psl->size = 0;
psl->capacity = 0;
}
// The destruction
void SeqListDestroy(SeqList* psl)
{
assert(psl);
free(psl->a);
psl->a = NULL;
psl->capacity = psl->size = 0;
}
// Check whether expansion is needed
void SeqListCheckCapacity(SeqList* psl)
{
assert(psl);
// If there is not enough space, it needs to be expanded
if (psl->size == psl->capacity)
{
size_t newcapacity = psl->capacity == 0 ? 4 : psl->capacity * 2;
SLDataType* tmp = realloc(psl->a, sizeof(SLDataType) * newcapacity);
if (tmp == NULL)
{
printf("realloc fail\n");
exit(-1);
}
else
{
psl->a = tmp;
psl->capacity = (int)newcapacity;
}
}
}
// Tail insertion
void SeqListPushBack(SeqList* psl, SLDataType x)
{
assert(psl);
SeqListCheckCapacity(psl);
// When there is enough space
psl->a[psl->size] = x;
psl->size++;
}
// Deletion at the end
void SeqListPopBack(SeqList* psl)
{
assert(psl);
// Some details ,size Greater than 0 Just delete , Otherwise, delete it to a negative number
if (psl->size > 0)
{
psl->size--;
}
}
// Head insertion
void SeqListPushFront(SeqList* psl, SLDataType x)
{
assert(psl);
SeqListCheckCapacity(psl);
// Ideas : Move all elements backward to cover
size_t end = psl->size - 1;
while (end >= 0)
{
psl->a[end + 1] = psl->a[end];
end--;
}
psl->a[0] = x;
psl->size++;
}
// Head deletion
void SeqListPopFront(SeqList* psl)
{
assert(psl);
if (psl->size > 0)
{
size_t begin = 1;
while (begin < psl->size)
{
psl->a[begin - 1] = psl->a[begin];
begin++;
}
psl->size--;
}
}
// Print
void SeqListPrint(SeqList* psl)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
printf("%d ", psl->a[i]);
}
printf("\n");
}
// stay pos Position insert element
void SeqListInsert(SeqList* psl, size_t pos, SLDataType x)
{
assert(psl);
SeqListCheckCapacity(psl);
int end = psl->size;
while (end > pos)
{
psl->a[end] = psl->a[end - 1];
end--;
}
psl->a[pos] = x;
psl->size++;
}
// stay pos Position delete element
void SeqListErase(SeqList* psl, size_t pos)
{
// Place the subscript at pos Delete the elements of
assert(psl);
if (psl->size > 0)
{
int begin = (int)pos;
while (begin < psl->size)
{
psl->a[begin] = psl->a[begin + 1];
begin++;
}
psl->size--;
}
}
// lookup , Find the return subscript , No return found -1
int SeqListFind(SeqList* psl, SLDataType x)
{
assert(psl);
for (int i = 0; i < psl->size; i++)
{
if (psl->a[i] == x)
{
return i;
}
}
return -1;
}
Pay attention to the following points in the sequence table :
1. When writing each interface , Add assertions , Easy to find errors
2. Adding data ( Head insertion , Tail insertion ) when , Be sure to check whether there is enough space , call SeqListCheckCapacity function
3. Deleting data ( Head deletion , Deletion at the end ) when , Make sure that the array size cannot be negative
4.SeqListInsert It can replace the head plug , Tail insertion ;SeqListErase Can replace header deletion , Deletion at the end . But check if it's feasible
The order sheet OJ



Method : Double pointer
Spatial complexity O(1)
Time complexity O(N)
Realization :
1.src Location is not val, Put it on dst Location , then src++,dst++
2.src Location is val,src++
版权声明
本文为[C_ TQH]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220643359966.html
边栏推荐
- Typescript Learning Guide
- tf. keras. layers. Timedistributed function
- PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
- Face longitude:
- [hcip] detailed explanation of six LSAS commonly used by OSPF
- Get together to watch (detailed version) eat a few cents a day
- Gavl021, gavl281, AC220V to 5v200ma small volume non isolated chip scheme
- Practice of industrial defect detection project (III) -- Based on FPN_ PCB defect detection of tensorflow
- MySQL complex query uses temporary table / with as (similar to table variable)
- The problem of removing spaces from strings
猜你喜欢

Er and eer models

Error installing Mongo service 'mongodb server' on win10 failed to start

重大危险源企业如何保障年底前完成双预防机制数字化建设任务

How can enterprises with major hazard installations ensure the completion of the digital construction task of double prevention mechanism by the end of the year

Plug in for vscode

L2-006 树的遍历(中后序确定二叉树&层序遍历)

Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment

Linux Redis ——Redis HA Sentinel 集群搭建详解 & Redis主从部署

JS learning notes

It turns out that PID was born in the struggle between Lao wangtou and Lao sky
随机推荐
ele之Table表格的封装
基于ele封装下拉菜单等组件
tf. keras. layers. MaxPooling? D function
Introduction to ACM [inclusion exclusion theorem]
Difference between relative path and absolute path (often asked in interview)
Shell script learning notes - regular expressions
Cloud computing learning 1 - openstack cloud computing installation and deployment steps with pictures and texts (Xiandian 2.2)
The shell monitors the depth of the IBM MQ queue and scans it three times in 10s. When the depth value exceeds 5 for more than two times, the queue name and depth value are output.
tf. keras. layers. Conv? D function
Servlet template engine usage example
@Usage and difference between mapper and @ repository
JSON data text
Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
《信息系统项目管理师总结》第四章 项目成本管理
MySQL complex query uses temporary table / with as (similar to table variable)
Liunx foundation - zabbix5 0 monitoring system installation and deployment
Chapter IV project cost management of information system project manager summary
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
Android 高阶面试必问:全局业务和项目的架构设计与重构
Linux Redis——Redis 数据库缓存服务