le mie informazioni di contatto
Posta[email protected]
2024-07-12
한어Русский языкEnglishFrançaisIndonesianSanskrit日本語DeutschPortuguêsΕλληνικάespañolItalianoSuomalainenLatina
Sommario
2. Operazioni base delle tavole lineari
(4). Ricerca per ordine di bit, ricerca per valore
(1).Elenco collegato singolarmente
i. Definizione di lista concatenata singolarmente (nodo principale)
Tavola lineare:
(1).Ogni elemento in esso,Stesso tipo di dati。
(2).Tra gli elementi,Al fine。
(3).elemento di intestazioneEelemento di coda。
(4). Ad eccezione dell'intestazione e del piè di pagina, è possibile trovarne uno per ciascun elementoprecursore direttoEsuccessore diretto。
tavoloDa zero
Elenco di inizializzazione(&L):inizializzazioneuna tavola lineare
Elenco di distruzione(&L):distruggere
。
ListInsert(&L, i, e):inserire, inserire l'elemento e alla i-esima posizione della tabella L
ElencoElimina(&L, i, &e):eliminare, elimina l'i-esimo elemento della tabella L e utilizza e per restituire l'elemento eliminato
。
IndividuaElem(L, e): premerevaloreCerca, trova l'elemento della parola chiave specifica e nella tabella, restituisce l'ordine di e, non il pedice
OttieniElem(L, i): premereMorsoCerca per ottenere il valore dell'i-esimo elemento nella tabella
。
Altre operazioni generali:
Lunghezza(L): TrovacapotavolaTrascorrere
StampaElenco(L):produzionetavoloTuttoelemento
Vuoto(L): tabella del giudizioÈ vuoto?, restituisce vero o falso
Molto riassuntivo, ovvero creare vendite e aggiungere, eliminare, modificare e controllare
dimemorizzazione sequenzialetabella lineare di dati
- #define MAX 10
- //顺序表(静态分配)
- class SqList
- {
- public:
- int data[MAX];
- int length;
- };
- //初始化
- void InitList(SqList &l)
- {
- for(int i = 0 ;i < 10 ;i++)
- {
- l.data[i] = 0;
- }
- l.length = 0;
- }
- //打印所有元素
- void PrintList(SqList &l)
- {
- for (int i = 0; i < 10; i++)
- cout << "第" << i << "个数:" << l.data[i] << endl;
- }
-
- //测验
- void test01()
- {
- SqList l;
- InitList(l);
- PrintList(l);
- }
- #define InitSize 10
- //顺序表(动态分配)
- class SqList
- {
- public:
- int* data; //指示动态分配数组的指针
- int MaxSize; //指示最大容量
- int length; //指示当前长度
- };
- //初始化顺序表
- void InitList(SqList& l)
- {
- l.data = new int[InitSize];
- l.MaxSize = InitSize;
- l.length = 0;
- for (int i = 0; i < l.MaxSize; i++)
- {
- l.data[i] = 0;
- }
- }
- //增长数组空间
- void IncreaseSize(SqList& l, int len)
- {
- int* p = l.data; //暂存原数组中的数据
- l.data = new int[10 + len]; //扩展新的数组
- for (int i = 0; i < l.length; i++) //将原数据拷贝进新数组中
- {
- l.data[i] = p[i];
- }
- l.MaxSize = InitSize + len; //修改数组的状态数据
- delete p; //将p释放
- }
- //打印所有元素
- void PrintList(SqList& l)
- {
- for (int i = 0; i < 10; i++)
- cout << "第" << i << "个数:" << l.data[i] << endl;
- }
-
- void test01()
- {
- SqList l;
- InitList(l);
- PrintList(l);
- }
//inserire
bool ListInsert(SqList& l, int d, int e)
{
if (l.length >= MAX) //Per prima cosa, determina se la tabella è piena e se l'inserimento è legale.
{
cout << "Inserimento fallito, è stato raggiunto il limite superiore" << endl;
restituisci falso;
}
se (d < 1 || d > l.lunghezza + 1)
{
cout << "Inserimento fallito, nessun predecessore diretto" << endl;
restituisci falso;
}
for (int j = l.length; j >= d; j--) //Sposta indietro l'elemento dopo il punto di inserimento
l.data[j] = l.data[j - 1];
l.data[d - 1] = e; //Inserimento, poiché d si riferisce al numero, è necessario sottrarne uno nella conversione dell'array
l.lunghezza++;
restituisci vero;
}//eliminare
bool ListDelete(SqList& l, int d, int &e)
{
if (d < 1 || d >l.length) //Determina se la posizione cancellata è legale
restituisci falso;
e = l.data[d - 1] //Memorizza temporaneamente gli elementi cancellati
for (int j = d; j < l.length; j++) //Sposta in avanti l'elemento dopo l'elemento eliminato
l.data[j - 1] = l.data[j]; //Qui deve essere j = d, j-1 è coperto da j, se j = d-1, la seguente copertura diventerà j è coperto j+1 copre e alla fine j+1 potrebbe superare la capacità massima dell'array
l.lunghezza--;
restituisci vero;
}
Codice d'esempio
- #define MAX 10
- //顺序表(静态分配)
- class SqList
- {
- public:
- int data[MAX];
- int length;
- };
- //初始化
- void InitList(SqList& l)
- {
- for (int i = 0; i < 10; i++)
- {
- l.data[i] = 0;
- }
- l.length = 0;
- }
- //打印所有元素
- void PrintList(SqList& l)
- {
- for (int i = 0; i < 10; i++)
- cout << "第" << i << "个数:" << l.data[i] << endl;
- }
- //存入数据
- void InputElem(SqList& l, int e)
- {
- int i = 0;
- while (i < MAX)
- {
- if (l.data[i] == 0)
- {
- l.data[i] = e;
- l.length++;
- break;
- }
- i++;
- }
- }
- //获取顺序表长度
- int GetLength(SqList l)
- {
- //cout << l.length << endl;
- return l.length;
- }
- //插入
- bool ListInsert(SqList& l, int d, int e)
- {
- if (l.length >= MAX) //首先要判断表是否已满、插入是否合法
- {
- cout << "插入失败,已达上限" << endl;
- return false;
- }
-
- if (d < 1 || d > l.length + 1)
- {
- cout << "插入失败,无直接前驱" << endl;
- return false;
- }
- for (int j = l.length; j >= d; j--) //将插入点之后的元素后移
- l.data[j] = l.data[j - 1];
- l.data[d - 1] = e; //插入,因为d指的是第几个数,在数组的换算中要减一
- l.length++;
- return true;
- }
- //删除
- bool ListDelete(SqList& l, int d, int &e)
- {
- if (d < 1 || d >l.length) //判断删除的位置是否合法
- return false;
- e = l.data[d - 1]; //暂存删除掉的元素
- for (int j = d; j < l.length; j++) //将被删除元素之后的元素前移
- l.data[j - 1] = l.data[j]; //此处,必须是j = d,j-1被j覆盖,若j = d-1,则下文的覆盖会变为j 被j+1 覆盖,而j+1在最后有可能会超过数组的最大容量
- l.length--;
- return true;
- }
-
- //查看情况
- void CheckList(SqList& l)
- {
- PrintList(l);
- cout << "当前长度为" << GetLength(l) << endl;
- }
-
- //测验
- void test01()
- {
- SqList l;
- InitList(l);
-
- //输入部分数据
- InputElem(l, 1);
- InputElem(l, 2);
- InputElem(l, 3);
- InputElem(l, 4);
- CheckList(l);
-
- //开始插入
- if(ListInsert(l, 3, 6))
- CheckList(l);
-
- //开始删除
- int a = -1;
- if (ListDelete(l, 2, a))
- CheckList(l);
- }
Molto semplice, non c'è bisogno di entrare nei dettagli
- //判断d的合法性
- bool JugdeD(SqList l, int d)
- {
- if (d < 1 || d > l.length)
- return false;
- return true;
- }
-
- //按位序查找
- int GetElem(SqList l, int d)
- {
- if (JugdeD(l, d))
- return l.data[d - 1];
- return 0;
- }
-
- //按值查找
- int LocateElem(SqList l, int e)
- {
- for (int i = 0; i < l.length; i++)
- {
- if (l.data[i] == e) //数组储存的数据,若是类等复杂的数据类型,则需要对等号进行重载
- return i + 1;
- }
- return 0;
- }
- //其余代码与上文相同
- //其中,JugdeD函数可以替换上文插入与删除中对位序合法性的判别————封装
distoccaggio a catenatabella lineare di dati
- //单链表
- class LNode
- {
- public:
- int data; //数据域,存放数据
- LNode* next; //指针域,指向下一个节点
- };
- //用using关键字给类起别名,用LinkList指代的是头结点,代表的是整个链表
- using LinkList = LNode*;
-
- //初始化
- bool InitList(LinkList& L)
- {
- L = new LNode();
- if (L == nullptr) //如果成立,则说明内存不足,分配失败
- return false;
- L->next = nullptr;
- return true;
- }
Se non è presente alcun nodo head, prestare attenzione al cambiamento del puntatore head. Tutto il resto è uguale.
Inserisci (versione normale)
- //插入
- bool ListInsert(LinkList& L, int i, int e)
- {
- if (i < 1) //判断插入位点是否合法[1]——i值的合法性
- {
- cout << "i为负数" << endl;
- return false;
- }
- LNode* p = L; //让p与L指向相同的位点,L是指示头指针的,所以L是不能改变的
- LNode* s = new LNode(); //新的数据储存
- s->data = e;
- while (p != nullptr && i != 1) //由头结点起始,开始遍历寻找对应位点
- {
- p = p->next;
- i--;
- }
- if (p == nullptr) //判断插入的位点是否合法[2]——i值对应的节点的合法性
- {
- cout << "插入位点超出实际长度" << endl;
- return false;
- }
- s->next = p->next; //开始接轨,顺序不能乱
- p->next = s;
- return true;
- }
Inserisci (versione confezionata)
- //特定节点的后插操作
- bool InsertNextNode(LNode* p, int e)
- {
- if (p == nullptr)
- {
- cout << "插入位点超出实际长度" << endl;
- return false;
- }
- LNode* s = new LNode();
- s->data = e;
- s->next = p->next;
- p->next = s;
- return true;
- }
- //插入
- bool ListInsert(LinkList& L, int i, int e)
- {
- if (i < 1) //判断插入位点是否合法[1]——i值的合法性
- {
- cout << "i为负数" << endl;
- return false;
- }
- LNode* p = L; //让p与L指向相同的位点,L是指示头指针的,所以L是不能改变的
- while (p != nullptr && i != 1) //由头结点起始,开始遍历寻找对应位点
- {
- p = p->next;
- i--;
- }
- return InsertNextNode(p, e); //被封装了的部分
- }