내 연락처 정보
우편메소피아@프로톤메일.com
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
순차 저장 선형 테이블은 선형 테이블의 요소를 특정 순서로 연속된 주소를 가진 저장 단위 세트에 저장하는 기본 데이터 구조입니다. 이번 블로그에서는 순차 저장 선형 테이블의 구현을 자세히 소개하고, C, C#, C++의 세 가지 프로그래밍 언어를 예로 들어 순차 저장 선형 테이블을 구현하는 방법을 보여드리겠습니다.
순차 저장 선형 테이블은 선형 테이블의 구현 방법으로 다음과 같은 특징을 가지고 있습니다.
요소 유형은 동일합니다. 순차 저장 선형 테이블의 요소는 동일한 유형이며 각 요소는 특정 저장 공간을 차지합니다.
요소는 순서가 지정됩니다. 순차 저장 선형 목록의 요소는 특정 순서, 일반적으로 감소하지 않는 순서로 정렬됩니다.
무작위 액세스: 순차 스토리지 선형 테이블은 무작위 액세스를 지원합니다. 즉, 테이블의 모든 요소는 인덱스를 통해 직접 액세스할 수 있습니다.
동적 확장: 순차 스토리지 선형 테이블은 더 많은 요소를 수용하기 위해 필요에 따라 스토리지 공간을 동적으로 확장할 수 있습니다.
먼저 선형 테이블의 순차적 저장을 위한 데이터 구조를 정의해야 합니다. C 언어를 예로 들면, 구조(struct)를 사용하여 다음을 정의할 수 있습니다.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data; // 指向动态分配的数组
int size; // 线性表的当前长度
int capacity; // 线性表的容量
} SequenceList;
선형 테이블은 초기화 시퀀스에 저장되고 초기 용량이 할당됩니다. C 언어에서는 malloc 함수를 사용하여 메모리를 동적으로 할당할 수 있습니다.
void initSequenceList(SequenceList *list, int capacity) {
list->data = (int *)malloc(capacity * sizeof(int));
if (list->data == NULL) {
exit(-1); // 内存分配失败,退出程序
}
list->size = 0;
list->capacity = capacity;
}
순차적으로 저장된 선형 목록에 요소를 추가합니다. 현재 용량이 새 요소를 수용할 만큼 충분하지 않은 경우 먼저 용량을 확장해야 합니다.
void appendSequenceList(SequenceList *list, int value) {
if (list->size == list->capacity) {
// 扩展容量
int newCapacity = list->capacity * 2;
int *newData = (int *)malloc(newCapacity * sizeof(int));
if (newData == NULL) {
exit(-1); // 内存分配失败,退出程序
}
for (int i = 0; i < list->size; i++) {
newData[i] = list->data[i];
}
free(list->data);
list->data = newData;
list->capacity = newCapacity;
}
list->data[list->size++] = value;
}
순차적으로 저장된 선형 목록의 지정된 요소에 액세스합니다.
int getSequenceList(const SequenceList *list, int index) {
if (index < 0 || index >= list->size) {
return -1; // 索引无效,返回-1
}
return list->data[index];
}
순차 저장소 선형 목록에서 지정된 요소를 수정합니다.
void setSequenceList(SequenceList *list, int index, int value) {
if (index < 0 || index >= list->size) {
return; // 索引无效,不进行操作
}
list->data[index] = value;
}
순차적으로 저장된 선형 목록에서 지정된 요소를 삭제합니다.
void removeSequenceList(SequenceList *list, int index) {
if (index < 0 || index >= list->size) {
return; // 索引无效,不进行操作
}
for (int i = index + 1; i < list->size; i++) {
list->data[i - 1] = list->data[i];
}
list->size--;
}
순차 스토리지 선형 테이블을 삭제하고 할당된 메모리를 해제합니다.
void destroySequenceList(SequenceList *list) {
free(list->data);
list->data = NULL;
list->size = 0;
list->capacity = 0;
}
아래에서는 세 가지 프로그래밍 언어인 C, C# 및 C++를 사용하여 순차 저장 선형 테이블을 구현하고 선형 테이블의 요소를 추가, 삭제, 액세스 및 인쇄합니다.
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *data;
int size;
int capacity;
} SequenceList;
void initSequenceList(SequenceList *list, int capacity) {
list->data = (int *)malloc(capacity * sizeof(int));
if (list->data == NULL) {
exit(-1);
}
list->size = 0;
list->capacity = capacity;
}
void appendSequenceList(SequenceList *list, int value) {
if (list->size == list->capacity) {
int newCapacity = list->capacity * 2;
int *newData = (int *)malloc(newCapacity * sizeof(int));
if (newData == NULL) {
exit(-1);
}
for (int i = 0; i < list->size; i++) {
newData[i] = list->data[i];
}
free(list->data);
list->data = newData;
list->capacity = newCapacity;
}
list->data[list->size++] = value;
}
int getSequenceList(const SequenceList *list, int index) {
if (index < 0 || index >= list->size) {
return -1;
}
return list->data[index];
}
void setSequenceList(SequenceList *list, int index, int value) {
if (index < 0 || index >= list->size) {
return;
}
list->data[index] = value;
}
void removeSequenceList(SequenceList *list, int index) {
if (index < 0 || index >= list->size) {
return;
}
for (int i = index + 1; i < list->size; i++) {
list->data[i - 1] = list->data[i];
}
list->size--;
}
void destroySequenceList(SequenceList *list) {
free(list->data);
list->data = NULL;
list->size = 0;
list->capacity = 0;
}
int main() {
SequenceList list;
initSequenceList(&list, 10);
appendSequenceList(&list, 1);
appendSequenceList(&list, 2);
appendSequenceList(&list, 3);
printf("Get element at index 1: %dn", getSequenceList(&list, 1));
setSequenceList(&list, 1, 4);
printf("Get element at index 1 after modification: %dn", getSequenceList(&list, 1));
removeSequenceList(&list, 0);
printf("Get element at index 0 after removal: %dn", getSequenceList(&list, 0));
destroySequenceList(&list);
return 0;
}
using System;
public class SequenceList
{
private int[] data;
private int size;
private int capacity;
public SequenceList(int capacity)
{
this.data = new int[capacity];
this.size = 0;
this.capacity = capacity;
}
public void Append(int value)
{
if (size == capacity)
{
int newCapacity = capacity * 2;
int[] newData = new int[newCapacity];
for (int i = 0; i < size; i++)
{
newData[i] = data[i];
}
data = newData;
capacity = newCapacity;
}
data[size++] = value;
}
public int Get(int index)
{
if (index < 0 || index >= size)
{
return -1;
}
return data[index];
}
public void Set(int index, int value)
{
if (index < 0 || index >= size)
{
return;
}
data[index] = value;
}
public void RemoveAt(int index)
{
if (index < 0 || index >= size)
{
return;
}
for (int i = index + 1; i < size; i++)
{
data[i - 1] = data[i];
}
size--;
}
public void Clear()
{
size = 0;
}
}
class Program
{
static void Main(string[] args)
{
SequenceList list = new SequenceList(10);
list.Append(1);
list.Append(2);
list.Append(3);
Console.WriteLine("Get element at index 1: " + list.Get(1));
list.Set(1, 4);
Console.WriteLine("Get element at index 1 after modification: " + list.Get(1));
list.RemoveAt(0);
Console.WriteLine("Get element at index 0 after removal: " + list.Get(0));
list.Clear();
}
}
#include <iostream>
class SequenceList {
private:
int *data;
int size;
int capacity;
public:
SequenceList(int capacity) {
data = new int[capacity];
size = 0;
capacity = capacity;
}
void Append(int value) {
if (size == capacity) {
int newCapacity = capacity * 2;
int *newData = new int[newCapacity];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
delete[] data;
data = newData;
capacity = newCapacity;
}
data[size++] = value;
}
int Get(int index) {
if (index < 0 || index >= size) {
return -1;
}
return data[index];
}
void Set(int index, int value) {
if (index < 0 || index >= size) {
return;
}
data[index] = value;
}
void RemoveAt(int index) {
if (index < 0 || index >= size) {
return;
}
for (int i = index + 1; i < size; i++) {
data[i - 1] = data[i];
}
size--;
}
void Clear() {
size = 0;
}
};
int main() {
SequenceList list(10);
list.Append(1);
list.Append(2);
list.Append(3);
std::cout << "Get element at index 1: " << list.Get(1) << std::endl;
list.Set(1, 4);
std::cout << "Get element at index 1 after modification: " << list.Get(1) << std::endl;
list.RemoveAt(0);
std::cout << "Get element at index 0 after removal: " << list.Get(0) << std::endl;
list.Clear();
return 0;
}
이 세 가지 예에서는 순차 저장소 선형 목록 클래스를 정의하고 요소 추가, 삭제, 액세스 및 인쇄와 같은 일련의 기본 작업을 제공합니다. 이러한 예제를 통해 다양한 프로그래밍 언어로 순차 스토리지 선형 테이블을 구현하는 방법을 배울 수 있습니다.