Skip to content

Instantly share code, notes, and snippets.

@brunoruas2
Last active February 9, 2023 01:09
Show Gist options
  • Save brunoruas2/da13f73beddbc996d6a4663dc7575c67 to your computer and use it in GitHub Desktop.
Save brunoruas2/da13f73beddbc996d6a4663dc7575c67 to your computer and use it in GitHub Desktop.
Código que exemplifica a criação de uma Lista Linear estudada na disciplina de Algoritmos e Estrutura de Dados
// Criação de uma lista linear
class ListaLinear {
private int[] array;
private int n;
// construtor vazio
ListaLinear() {
array = new int[6];
n = 0;
}
// construtor com definição do tamanho
ListaLinear(int tamanho) {
array = new int[tamanho];
n = 0;
}
// relação de procedimentos
void InserirInicio(int x) {
// código encerra se tivermos mais ítens do que
// a lista pode comportar
if (n >= array.Length)
Environment.Exit(0);
for(int i = n; i > 0; i--) {
array[i] = array[i-1];
}
array[0] = x;
n++;
}
void InserirFinal(int x) {
if (n >= array.Length)
Environment.Exit(0);
array[n] = x;
n++;
}
void Inserir(int x, int pos) {
if(n >= array.Length || pos < 0 || os > n) {
Environment.Exit(0);
// leva os elementos para o final do array
for (int i = n; i > pos; i--){
array[i] = array[i-1];
}
array[pos] = x;
n++;
}
}
int RemoverInicio(){
if (n == 0)
Environment.Exit(0);
int resp = array[0];
n--;
for (int i = 0; i < n; i++) {
array[i] = array[i+1];
}
return resp;
}
int RemoverFim() {
if (n == 0)
Environment.Exit(0);
return array[--n];
}
int Remover(int pos) {
if (n == 0 || pos < 0 || pos >= n)
Environment.Exit(0);
int resp = array[pos];
n--;
for (int i = pos; i < n; n++) {
array[i] = array[i+1];
}
return resp;
}
void Mostrar() {
Console.Write("[");
for (int i = 0; i < n; i++) {
Console.Write(array[i] + " ");
}
Console.Write("]");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment