当前位置:网站首页>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
边栏推荐
- Essential qualities of advanced programmers
- Winsock programming interface experiment: Ping
- OCR识别PDF文件
- Actual combat of industrial defect detection project (IV) -- ceramic defect detection based on hrnet
- Probabilistic model of machine learning
- 【Hcip】OSPF常用的6种LSA详解
- The problem of removing spaces from strings
- Traversal of l2-006 tree (middle and later order determination binary tree & sequence traversal)
- 基于多态的职工管理系统源码与一些理解
- windows MySQL8 zip安装
猜你喜欢
PDH optical transceiver 4-way E1 + 4-way 100M Ethernet 4-way 2m optical transceiver FC single fiber 20km rack type
Introduction to ACM [inclusion exclusion theorem]
The input of El input input box is invalid, and error in data(): "referenceerror: El is not defined“
基于Scrum进行创新和管理
HLS / chisel practice CORDIC high performance computing complex square root
Servlet template engine usage example
Plug in for vscode
重大危险源企业如何保障年底前完成双预防机制数字化建设任务
tf. keras. layers. Timedistributed function
Niuke white moon race 5 [problem solving mathematics field]
随机推荐
JS using the parameters of art template
Use of MySQL command line client and common commands
基于ele封装下拉菜单等组件
Servlet template engine usage example
Numpy stack function
Store consumption SMS notification template
OCR识别PDF文件
Opencv reads webcam video and saves it locally
Flink learning (XI) watermark
接口请求时间太长,jstack观察锁持有情况
Specific field information of MySQL export table (detailed operation of Navicat client)
Numpy append function
mysql function函数语法
学习正则表达式选项、断言
Linux redis - redis ha sentinel cluster construction details & redis master-slave deployment
Essential qualities of advanced programmers
Innovation and management based on Scrum
Some problems encountered in setting Django pure interface, channel and MySQL on the pagoda panel
AC & A2C & A3C
Regular object type conversion tool - Common DOM class