Buscar

Algoritmo de Lista Encadeada Circular em c

Esta é uma pré-visualização de arquivo. Entre para ver o arquivo original

ListaCircular/deployment.pri
# This file was generated by an application wizard of Qt Creator.
# The code below handles deployment to Android and Maemo, aswell as copying
# of the application data to shadow build directories on desktop.
# It is recommended not to modify this file, since newer versions of Qt Creator
# may offer an updated version of it.
defineTest(qtcAddDeployment) {
for(deploymentfolder, DEPLOYMENTFOLDERS) {
 item = item$${deploymentfolder}
 greaterThan(QT_MAJOR_VERSION, 4) {
 itemsources = $${item}.files
 } else {
 itemsources = $${item}.sources
 }
 $$itemsources = $$eval($${deploymentfolder}.source)
 itempath = $${item}.path
 $$itempath= $$eval($${deploymentfolder}.target)
 export($$itemsources)
 export($$itempath)
 DEPLOYMENT += $$item
}
MAINPROFILEPWD = $$PWD
android-no-sdk {
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 item = item$${deploymentfolder}
 itemfiles = $${item}.files
 $$itemfiles = $$eval($${deploymentfolder}.source)
 itempath = $${item}.path
 $$itempath = /data/user/qt/$$eval($${deploymentfolder}.target)
 export($$itemfiles)
 export($$itempath)
 INSTALLS += $$item
 }
 target.path = /data/user/qt
 export(target.path)
 INSTALLS += target
} else:android {
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 item = item$${deploymentfolder}
 itemfiles = $${item}.files
 $$itemfiles = $$eval($${deploymentfolder}.source)
 itempath = $${item}.path
 $$itempath = /assets/$$eval($${deploymentfolder}.target)
 export($$itemfiles)
 export($$itempath)
 INSTALLS += $$item
 }
 x86 {
 target.path = /libs/x86
 } else: armeabi-v7a {
 target.path = /libs/armeabi-v7a
 } else {
 target.path = /libs/armeabi
 }
 export(target.path)
 INSTALLS += target
} else:win32 {
 copyCommand =
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 source = $$MAINPROFILEPWD/$$eval($${deploymentfolder}.source)
 source = $$replace(source, /, \\)
 sourcePathSegments = $$split(source, \\)
 target = $$OUT_PWD/$$eval($${deploymentfolder}.target)/$$last(sourcePathSegments)
 target = $$replace(target, /, \\)
 target ~= s,\\\\\\.?\\\\,\\,
 !isEqual(source,$$target) {
 !isEmpty(copyCommand):copyCommand += &&
 isEqual(QMAKE_DIR_SEP, \\) {
 copyCommand += $(COPY_DIR) \"$$source\" \"$$target\"
 } else {
 source = $$replace(source, \\\\, /)
 target = $$OUT_PWD/$$eval($${deploymentfolder}.target)
 target = $$replace(target, \\\\, /)
 copyCommand += test -d \"$$target\" || mkdir -p \"$$target\" && cp -r \"$$source\" \"$$target\"
 }
 }
 }
 !isEmpty(copyCommand) {
 copyCommand = @echo Copying application data... && $$copyCommand
 copydeploymentfolders.commands = $$copyCommand
 first.depends = $(first) copydeploymentfolders
 export(first.depends)
 export(copydeploymentfolders.commands)
 QMAKE_EXTRA_TARGETS += first copydeploymentfolders
 }
} else:ios {
 copyCommand =
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 source = $$MAINPROFILEPWD/$$eval($${deploymentfolder}.source)
 source = $$replace(source, \\\\, /)
 target = $CODESIGNING_FOLDER_PATH/$$eval($${deploymentfolder}.target)
 target = $$replace(target, \\\\, /)
 sourcePathSegments = $$split(source, /)
 targetFullPath = $$target/$$last(sourcePathSegments)
 targetFullPath ~= s,/\\.?/,/,
 !isEqual(source,$$targetFullPath) {
 !isEmpty(copyCommand):copyCommand += &&
 copyCommand += mkdir -p \"$$target\"
 copyCommand += && cp -r \"$$source\" \"$$target\"
 }
 }
 !isEmpty(copyCommand) {
 copyCommand = echo Copying application data... && $$copyCommand
 !isEmpty(QMAKE_POST_LINK): QMAKE_POST_LINK += ";"
 QMAKE_POST_LINK += "$$copyCommand"
 export(QMAKE_POST_LINK)
 }
} else:unix {
 maemo5 {
 desktopfile.files = $${TARGET}.desktop
 desktopfile.path = /usr/share/applications/hildon
 icon.files = $${TARGET}64.png
 icon.path = /usr/share/icons/hicolor/64x64/apps
 } else:!isEmpty(MEEGO_VERSION_MAJOR) {
 desktopfile.files = $${TARGET}_harmattan.desktop
 desktopfile.path = /usr/share/applications
 icon.files = $${TARGET}80.png
 icon.path = /usr/share/icons/hicolor/80x80/apps
 } else { # Assumed to be a Desktop Unix
 copyCommand =
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 source = $$MAINPROFILEPWD/$$eval($${deploymentfolder}.source)
 source = $$replace(source, \\\\, /)
 macx {
 target = $$OUT_PWD/$${TARGET}.app/Contents/Resources/$$eval($${deploymentfolder}.target)
 } else {
 target = $$OUT_PWD/$$eval($${deploymentfolder}.target)
 }
 target = $$replace(target, \\\\, /)
 sourcePathSegments = $$split(source, /)
 targetFullPath = $$target/$$last(sourcePathSegments)
 targetFullPath ~= s,/\\.?/,/,
 !isEqual(source,$$targetFullPath) {
 !isEmpty(copyCommand):copyCommand += &&
 copyCommand += $(MKDIR) \"$$target\"
 copyCommand += && $(COPY_DIR) \"$$source\" \"$$target\"
 }
 }
 !isEmpty(copyCommand) {
 copyCommand = @echo Copying application data... && $$copyCommand
 copydeploymentfolders.commands = $$copyCommand
 first.depends = $(first) copydeploymentfolders
 export(first.depends)
 export(copydeploymentfolders.commands)
 QMAKE_EXTRA_TARGETS += first copydeploymentfolders
 }
 }
 !isEmpty(target.path) {
 installPrefix = $${target.path}
 } else {
 installPrefix = /opt/$${TARGET}
 }
 for(deploymentfolder, DEPLOYMENTFOLDERS) {
 item = item$${deploymentfolder}
 itemfiles = $${item}.files
 $$itemfiles = $$eval($${deploymentfolder}.source)
 itempath = $${item}.path
 $$itempath = $${installPrefix}/$$eval($${deploymentfolder}.target)
 export($$itemfiles)
 export($$itempath)
 INSTALLS += $$item
 }
 !isEmpty(desktopfile.path) {
 export(icon.files)
 export(icon.path)
 export(desktopfile.files)
 export(desktopfile.path)
 INSTALLS += icon desktopfile
 }
 isEmpty(target.path) {
 target.path = $${installPrefix}/bin
 export(target.path)
 }
 INSTALLS += target
}
export (ICON)
export (INSTALLS)
export (DEPLOYMENT)
export (LIBS)
export (QMAKE_EXTRA_TARGETS)
}
ListaCircular/funcoes.h
void inicializaLista (Lista * lista){
 lista->inicio = NULL;
 lista->fim = NULL;
 lista->tamanho = 0;//para medir tamanho da lista
}
/* inserção em uma lista vazia */
int insercaoListaVazia(Lista * lista, char *valor){
 Elemento *novo_elemento;
 if ((novo_elemento = (Elemento *) malloc (sizeof (Elemento))) == NULL)
 return -1;
 if ((novo_elemento->valor = (char *) malloc (50 * sizeof (char))) == NULL)
 return -1;
 strcpy (novo_elemento->valor, valor);
 novo_elemento->seguinte = novo_elemento;
 lista->inicio = novo_elemento;
 lista->fim = novo_elemento;
 lista->tamanho++;
 return 0;
}
/* inserção em uma lista não vazia */
//A função retorna -1 em caso de falha, se não ela retorna 0
int insercaoLista(Lista * lista, Elemento *curso, char *valor){
 Elemento *novo_elemento;
 //Aloca memória para o novo elemento
 if ((novo_elemento= (Elemento *) malloc (sizeof (Elemento))) == NULL)
return -1;
 if ((novo_elemento->valor = (char *) malloc (50 * sizeof (char))) == NULL)
 return -1;
 strcpy (novo_elemento->valor, valor);
 if(curso!= lista->fim)
 return -1;
 novo_elemento->seguinte = curso->seguinte;
 curso->seguinte = novo_elemento;//aponta para o endereço do primeiro elemento
 lista->fim = novo_elemento;//aponta para o novo elemento
 lista->tamanho++;// incrementa
 return 0;
}
/* remoção no inicio da lista */
int removeInicioLista(Lista * lista){
 if (lista->tamanho < 2)
 return -1;
 Elemento *remove_elemento;//contem o endereço do primeiro elemento
 remove_elemento = lista->inicio;
 lista->inicio = lista->inicio->seguinte;//aponta para o segundo elemento
 lista->fim->seguinte = lista->inicio;//aponta para o primeiro elemento
 free (remove_elemento->valor);//remove elemento da lista
 free (remove_elemento);
 lista->tamanho--;//decrementa o tamanho da lista
 return 0;
}
/* remoção em uma lista com um único elemento*/
int removeUnicoElemento (Lista *lista){
 if (lista->tamanho != 1)
 return -1;
 Elemento *remove_elemento;//endereço do elemento
 remove_elemento = lista->inicio;
 lista->inicio = NULL;//aponta para um nulo
 lista->fim = NULL;
 free (remove_elemento->valor);//remove elemento
 free (remove_elemento);
 lista->tamanho--;
 return 0;
 //a lista fica vazia apos isso
}
/* Exibição da lista do primeiro ao último elemento */
/*
 * ao contrario da lista simples,
 * onde a condição de parada é dada pela ponteiro seguinte do último elemento(NULL),na lista circular,
 * não há ponto de parada, então deve-se escolher um
 * */
void imprimeLista (Lista * lista){
 Elemento *curso;
 curso = lista->inicio;
 int i;
 for(i=0;i<lista->tamanho;++i){
 printf ("%p - %s\n", curso, curso->valor);
 curso = curso->seguinte;
 }
}
int imprimeMenu (Lista *lista){
 int escolha;
 printf("********** MENU **********\n");
 if (lista->tamanho == 0){
 printf ("1. Adicionar o primeiro elemento\n");
 printf ("2. Fechar\n");
 }else {
 printf ("1. Inserir um elemento\n");
 printf ("2. Remover elemento no inicio (a lista deve ter pelo menos 2 elementos)\n");//não consegui implementar o remover por elemento
 printf ("3. Remover em uma lista com apenas um elemento\n");
 printf ("4. Imprimir a lista circular\n");
 printf ("\n\n\[Ctrl+C] para fechar o programa\n");
 printf ("7. Fechar\n");
 }
 printf ("------------------------------------------------------");
 printf ("\n\nEscolha uma opcao: ");
 scanf ("%d", &escolha);
 printf ("------------------------------------------------------");
 getchar();
 if(lista->tamanho == 0 && escolha == 2)
 escolha = 7;
 return escolha;
}
ListaCircular/lista_circular.h
typedef struct ListaCircular {
 struct ListaCircular *seguinte;//permite o acesso ao próximo elemento
 char *valor;//elemento da lista
} Elemento;
typedef struct ListaDetectada {
 Elemento *inicio;//endereço do primeiro elemento da lista
 Elemento *fim;//endereço do último elemento da lista
 int tamanho;//número de elementos
} Lista;
/* faz a inicializacao da lista */
void inicializaLista (Lista *lista);
/* INSERÇÃO */
/* inserção em uma lista vazia */
int insercaoListaVazia(Lista *lista, char *valor);
int insercaoLista(Lista *lista, Elemento *curso, char *valor);
/* REMOÇÃO */
int removeInicioLista (Lista *lista);
int removeUnicoElemento (Lista *lista);
int imprimeMenu (Lista *lista);
void imprimeLista (Lista *lista);
/* -------- FIM lista_circ.h --------- */
ListaCircular/ListaCircular.pro
TEMPLATE = app
CONFIG += console
CONFIG -= app_bundle
CONFIG -= qt
SOURCES += main.cpp
include(deployment.pri)
qtcAddDeployment()
HEADERS += \
 lista_circular.h \
 funcoes.h
ListaCircular/ListaCircular.pro.user
 
 
 EnvironmentId
 {adb9ebfe-1bc2-4b54-b08b-e22ed67a7698}
 
 
 ProjectExplorer.Project.ActiveTarget
 0
 
 
 ProjectExplorer.Project.EditorSettings
 
 true
 false
 true
 
 Cpp
 
 CppGlobal
 
 
 
 QmlJS
 
 QmlJSGlobal
 
 
 2
 UTF-8
 false
 4
 false
 80
 true
 true
 1
 true
 false
 0
 true
 0
 8
 true
 1
 true
 true
 true
 false
 
 
 
 ProjectExplorer.Project.PluginSettings
 
 
 
 ProjectExplorer.Project.Target.0
 
 Desktop Qt 5.5.0 MinGW 32bit
 Desktop Qt 5.5.0 MinGW 32bit
 qt.55.win32_mingw492_kit
 0
 0
 0
 
 C:/Users/Diogo/Google Drive/PUC 2015-2/Enfase 1/Estrutura de dados/build-ListaCircular-Desktop_Qt_5_5_0_MinGW_32bit-Debug
 
 
 true
 qmake
 
 QtProjectManager.QMakeBuildStep
 false
 true
 
 false
 false
 false
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 false
 
 
 
 2
 Build
 
 ProjectExplorer.BuildSteps.Build
 
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 true
 clean
 
 
 1
 Clean
 
 ProjectExplorer.BuildSteps.Clean
 
 2
 false
 
 Debug
 
 Qt4ProjectManager.Qt4BuildConfiguration
 2
 true
 
 
 C:/Users/Diogo/Google Drive/PUC 2015-2/Enfase 1/Estrutura de dados/build-ListaCircular-Desktop_Qt_5_5_0_MinGW_32bit-Release
 
 
 true
 qmake
 
 QtProjectManager.QMakeBuildStep
 false
 true
 
 false
 false
 false
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 false
 
 
 
 2
 Build
 
 ProjectExplorer.BuildSteps.Build
 
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 true
 clean
 
 
 1
 Clean
 
 ProjectExplorer.BuildSteps.Clean
 
 2
 false
 
 Release
 
 Qt4ProjectManager.Qt4BuildConfiguration
 0
 true
 
 2
 
 
 0
 Deploy
 
 ProjectExplorer.BuildSteps.Deploy
 
 1
 Deploy locally
 
 ProjectExplorer.DefaultDeployConfiguration
 
 1
 
 
 
 false
 false
 false
 false
 true
 0.01
 10
 true
 1
 25
 
 1
 true
 false
 true
 valgrind
 
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 2
 
 ListaCircular
 
 Qt4ProjectManager.Qt4RunConfiguration:C:/Users/Diogo/Google Drive/PUC 2015-2/Enfase 1/Estrutura de dados/ListaCircular/ListaCircular.pro
 
 ListaCircular.pro
 false
 true
 
 3768
 false
 true
 false
 false
 true
 
 1
 
 
 
 ProjectExplorer.Project.Target.1
 
 Desktop Qt 5.4.2 MinGW 32bit2
 Desktop Qt 5.4.2 MinGW 32bit2
 qt.54.win32_mingw491_kit
 0
 0
 0
 
 C:/Users/Diogo/Google Drive/PUC 2015-2/Enfase 1/Estrutura de dados/build-ListaCircular-Desktop_Qt_5_4_2_MinGW_32bit2-Debug
 
 
 true
 qmake
 
 QtProjectManager.QMakeBuildStep
 false
 true
 
 false
 false
 false
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 false
 
 
 
 2
 Build
 
 ProjectExplorer.BuildSteps.Build
true
 Make
 
 Qt4ProjectManager.MakeStep
 
 true
 clean
 
 
 1
 Clean
 
 ProjectExplorer.BuildSteps.Clean
 
 2
 false
 
 Debug
 
 Qt4ProjectManager.Qt4BuildConfiguration
 2
 true
 
 
 C:/Users/Diogo/Google Drive/PUC 2015-2/Enfase 1/Estrutura de dados/build-ListaCircular-Desktop_Qt_5_4_2_MinGW_32bit2-Release
 
 
 true
 qmake
 
 QtProjectManager.QMakeBuildStep
 false
 true
 
 false
 false
 false
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 false
 
 
 
 2
 Build
 
 ProjectExplorer.BuildSteps.Build
 
 
 
 true
 Make
 
 Qt4ProjectManager.MakeStep
 
 true
 clean
 
 
 1
 Clean
 
 ProjectExplorer.BuildSteps.Clean
 
 2
 false
 
 Release
 
 Qt4ProjectManager.Qt4BuildConfiguration
 0
 true
 
 2
 
 
 0
 Deploy
 
 ProjectExplorer.BuildSteps.Deploy
 
 1
 Deploy locally
 
 ProjectExplorer.DefaultDeployConfiguration
 
 1
 
 
 
 false
 false
 false
 false
 true
 0.01
 10
 true
 1
 25
 
 1
 true
 false
 true
 valgrind
 
 0
 1
 2
 3
 4
 5
 6
 7
 8
 9
 10
 11
 12
 13
 14
 
 -1
 
 
 
 false
 %{buildDir}
 Custom Executable
 
 ProjectExplorer.CustomExecutableRunConfiguration
 3768
 false
 true
 false
 false
 true
 
 1
 
 
 
 ProjectExplorer.Project.TargetCount
 2
 
 
 ProjectExplorer.Project.Updater.FileVersion
 18
 
 
 Version
 18
 
ListaCircular/main.cpp
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "lista_circular.h"
#include "funcoes.h"
int main()
{
 char escolha;
 char *nome;
 Lista *lista;
 Elemento *curso;
 if ((lista = (Lista *) malloc (sizeof (Lista))) == NULL)
 return -1;
 if ((nome = (char *) malloc (50)) == NULL)
 return -1;
 curso = NULL;
 escolha = 'o';
 inicializaLista (lista);
 while (escolha!= 7){
 escolha = imprimeMenu (lista);//chama o menu de opcoes e guarda a opcao escolhida
 switch (escolha){
 case 1:
 printf ("\nEntre um elemento: ");
 scanf ("%s", nome);
 getchar ();
 if(lista->tamanho == 0){//se lista estiver vazia
 insercaoListaVazia (lista,nome);
 } else {
 insercaoLista (lista,lista->fim,nome);
 }
 printf ("\n%d ->Elementos: inicio=%s, fim=%s\n",lista->tamanho,lista->inicio->valor,lista->fim->valor);
 imprimeLista (lista);
 break;
 case 2:
 if(lista->tamanho < 2){
 break;
 }
 removeInicioLista (lista);
 if (lista->tamanho != 0){
 printf ("\n%d ->Elementos:inicio=%s, fim=%s\n",lista->tamanho,lista->inicio->valor,lista->fim->valor);
 }
 imprimeLista (lista);
 break;
 case 3:
 if(lista->tamanho != 1){
 break;
 }
 removeUnicoElemento(lista);
 printf("A lista está vazia\n");
 break;
 case 4:
 imprimeLista (lista);
 break;
 }
 }
 return 0;
}

Teste o Premium para desbloquear

Aproveite todos os benefícios por 3 dias sem pagar! 😉
Já tem cadastro?

Outros materiais