Skip to content

Instantly share code, notes, and snippets.

@ojulianos
Created March 6, 2023 00:37
Show Gist options
  • Save ojulianos/fd226d4caeb0159a6e66b09727221296 to your computer and use it in GitHub Desktop.
Save ojulianos/fd226d4caeb0159a6e66b09727221296 to your computer and use it in GitHub Desktop.
VetorNaoOrdenadoAvancado
class VetorNaoOrdenado:
def __init__(self, capacidade):
self.__capacidade = capacidade
self.__ultimaPosicao = -1
self.__valores = [0]*self.__capacidade
def imprimir(self):
if self.__ultimaPosicao == -1:
return print("Vetor vazio")
for i in range(self.__ultimaPosicao + 1):
print(i, "=", self.__valores[i])
def inserir(self, valor):
if (self.__ultimaPosicao == self.__capacidade-1):
return print("Vetor cheio")
posicao = self.pesquisar(valor)
if(posicao != -1):
return print('Valor já existe no vetor')
self.__ultimaPosicao += 1
self.__valores[self.__ultimaPosicao] = valor
def pesquisar(self, valor):
for i in range(self.__ultimaPosicao + 1):
if valor == self.__valores[i]:
return i
return -1
def excluir(self, valor):
posicao = self.pesquisar(valor)
if posicao == -1:
return -1
for i in range(posicao, self.__ultimaPosicao):
self.__valores[i] = self.__valores[i+1]
self.__ultimaPosicao -= 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment