SlideShare uma empresa Scribd logo
1 de 20
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
Código para gerar uma
estrutura de dados do tipo
lista ligada simples.
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
n
struct Numero
aux atual resp
Define o registro.
inicio
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
n
no
aux atual resp
Troca o nome.
inicio
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
n
no
aux atual resp
Declara os ponteiros
externos.
inicio
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
prox
n
no
aux atual resp
Chama a função.
inicio
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
prox
n
no
aux atual resp
Aloca um nó na memória.
inicio
NULL4040
atual
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
Atribui um valor ao membro
do nó.
inicio
NULL4040
atual
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
Atribui NULL ao ponteiro
interno prox.
inicio
NULL4040
atual
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
Condição resulta em
verdadeiro.
inicio
NULL4040
atual
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
inicio recebe o endereço
armazenado em atual.
inicio
NULL4040
atual
NULL
4040
inicio
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
aux recebe o endereço
armazenado em atual.
inicio
NULL4040
atual
NULL
4040
4040
inicio
aux
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
no
aux atual resp
Termina uma iteração.
inicio
NULL4040
atual
NULL
4040
4040
inicio
aux
sim
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Aloca um novo nó.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual respinicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
Atribui um valor ao membro
do nó.
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Atribui NULL ao ponteiro
interno prox.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Conecta o nó anterior
ao nó atual.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
aux aponta para o mesmo
endereço que atual aponta.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
5048
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Não será executada
outra iteração.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
5048 não
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Condição verificada e resulta
Em valor falso.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
5048 não
struct Numero {
int n;
struct Numero *prox;
};
void insere();
typedef struct Numero no;
no *inicio=NULL, *aux, *atual;
int main()
{
insere();
system("pause");
}
void insere()
{
char resp[4];
do{
atual = (no*)malloc(sizeof(no));
printf("Digite um numeron");
scanf("%d",&atual->n);
atual->prox=NULL;
if(inicio==NULL)
{
inicio=atual;
aux = atual;
}
else
{
aux->prox=atual;
aux = atual;
}
printf("Deseja continuar (sim/nao)?n");
scanf("%s",&resp);
}while(!strcmp(resp,"sim"));
}
prox
21
aux atual resp
Termina o algoritmo.
inicio
NULL5048
4040
4040
aux
sim
atual
prox
inicio
42
NULL
5048 não

Mais conteúdo relacionado

Mais procurados

Mais procurados (9)

Linguagem C 02 String
Linguagem C 02 StringLinguagem C 02 String
Linguagem C 02 String
 
Linguagem C 10 Arquivos
Linguagem C 10 ArquivosLinguagem C 10 Arquivos
Linguagem C 10 Arquivos
 
Hashing enderecamento aberto - operações
Hashing enderecamento aberto - operaçõesHashing enderecamento aberto - operações
Hashing enderecamento aberto - operações
 
Tipos Primitivos - Preparatório Certificação - OCAJP7 - Aula 2 - E
Tipos Primitivos - Preparatório Certificação - OCAJP7 - Aula 2 - ETipos Primitivos - Preparatório Certificação - OCAJP7 - Aula 2 - E
Tipos Primitivos - Preparatório Certificação - OCAJP7 - Aula 2 - E
 
Lista duplamente encadeada
Lista duplamente encadeadaLista duplamente encadeada
Lista duplamente encadeada
 
Linguagem C - Vetores
Linguagem C - VetoresLinguagem C - Vetores
Linguagem C - Vetores
 
Tabela Hash com Lista Encadeada
Tabela Hash com Lista EncadeadaTabela Hash com Lista Encadeada
Tabela Hash com Lista Encadeada
 
JavaScript - De verdade
JavaScript - De verdadeJavaScript - De verdade
JavaScript - De verdade
 
Comparando dois números
Comparando dois númerosComparando dois números
Comparando dois números
 

Destaque

Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusim
Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusimPakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusim
Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusimZaini Ithnin
 
Pakej pembajaan tanaman pisang terung bendi timun
Pakej pembajaan tanaman pisang terung bendi timunPakej pembajaan tanaman pisang terung bendi timun
Pakej pembajaan tanaman pisang terung bendi timunZaini Ithnin
 
Georgia STEM Science Initiative
Georgia STEM Science InitiativeGeorgia STEM Science Initiative
Georgia STEM Science Initiativefranciam
 
6 8 planets Saturn-Neptune
6 8 planets Saturn-Neptune6 8 planets Saturn-Neptune
6 8 planets Saturn-Neptune7thscience
 
Failures tacoma narrows collapse
Failures   tacoma narrows collapseFailures   tacoma narrows collapse
Failures tacoma narrows collapseNurSyuhada Hashim
 
Signos vitales UBV
Signos vitales UBVSignos vitales UBV
Signos vitales UBVkdvra
 
Bolsa mexica de valores.
Bolsa mexica de valores.Bolsa mexica de valores.
Bolsa mexica de valores.Dianarojglz
 
Cambia de ruta si es necesario
Cambia de ruta si es necesarioCambia de ruta si es necesario
Cambia de ruta si es necesarioAnn Mary Harvey
 

Destaque (20)

Staying Power II: A Report Card on Manufacturing in MA 2012
Staying Power II: A Report Card on Manufacturing in MA 2012Staying Power II: A Report Card on Manufacturing in MA 2012
Staying Power II: A Report Card on Manufacturing in MA 2012
 
Rodeo en el distrito de Huamantanga
Rodeo en el distrito de HuamantangaRodeo en el distrito de Huamantanga
Rodeo en el distrito de Huamantanga
 
AMORTIZACION
AMORTIZACIONAMORTIZACION
AMORTIZACION
 
Mansionization: Lexington's Experience
Mansionization: Lexington's ExperienceMansionization: Lexington's Experience
Mansionization: Lexington's Experience
 
Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusim
Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusimPakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusim
Pakej pembajaan mangga durian pokok nadir landskap bunga kertas & bermusim
 
Surya
SuryaSurya
Surya
 
Pakej pembajaan tanaman pisang terung bendi timun
Pakej pembajaan tanaman pisang terung bendi timunPakej pembajaan tanaman pisang terung bendi timun
Pakej pembajaan tanaman pisang terung bendi timun
 
Las tic de infografia.edgar marquez urioso.
Las tic de infografia.edgar marquez urioso.Las tic de infografia.edgar marquez urioso.
Las tic de infografia.edgar marquez urioso.
 
Georgia STEM Science Initiative
Georgia STEM Science InitiativeGeorgia STEM Science Initiative
Georgia STEM Science Initiative
 
U learn presentation
U learn presentationU learn presentation
U learn presentation
 
6 8 planets Saturn-Neptune
6 8 planets Saturn-Neptune6 8 planets Saturn-Neptune
6 8 planets Saturn-Neptune
 
Did ccrr del_carmen
Did ccrr del_carmenDid ccrr del_carmen
Did ccrr del_carmen
 
Grecia
GreciaGrecia
Grecia
 
Bike Parking
Bike ParkingBike Parking
Bike Parking
 
Local Economic Development Strategies to Channel the Manufacturing Renaissance
Local Economic Development Strategies to Channel the Manufacturing RenaissanceLocal Economic Development Strategies to Channel the Manufacturing Renaissance
Local Economic Development Strategies to Channel the Manufacturing Renaissance
 
Failures tacoma narrows collapse
Failures   tacoma narrows collapseFailures   tacoma narrows collapse
Failures tacoma narrows collapse
 
Signos vitales UBV
Signos vitales UBVSignos vitales UBV
Signos vitales UBV
 
Bolsa mexica de valores.
Bolsa mexica de valores.Bolsa mexica de valores.
Bolsa mexica de valores.
 
Como fazer video
Como fazer videoComo fazer video
Como fazer video
 
Cambia de ruta si es necesario
Cambia de ruta si es necesarioCambia de ruta si es necesario
Cambia de ruta si es necesario
 

Semelhante a Lista Ligada Simples

Apostila Algoritmos e Estrutura de Dados (AEDS)
Apostila Algoritmos e Estrutura de Dados (AEDS)Apostila Algoritmos e Estrutura de Dados (AEDS)
Apostila Algoritmos e Estrutura de Dados (AEDS)Ricardo Terra
 
Linguagem C 03 Estruturas De Decisao
Linguagem C 03 Estruturas De DecisaoLinguagem C 03 Estruturas De Decisao
Linguagem C 03 Estruturas De DecisaoRegis Magalhães
 
Algoritmos em c #1
Algoritmos em c #1Algoritmos em c #1
Algoritmos em c #1Stark2009
 
TypeScript - Olhe teu tipo, script slides
TypeScript - Olhe teu tipo, script slidesTypeScript - Olhe teu tipo, script slides
TypeScript - Olhe teu tipo, script slidesSuissa
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template LibraryDuda Dornelles
 
Linguagem C 04 Estruturas De Repeticao
Linguagem C 04 Estruturas De RepeticaoLinguagem C 04 Estruturas De Repeticao
Linguagem C 04 Estruturas De RepeticaoRegis Magalhães
 
Conhecendo ou relembrando C
Conhecendo ou relembrando CConhecendo ou relembrando C
Conhecendo ou relembrando CVinícius Hax
 
Linguagem c wellington telles - aula 03
Linguagem c   wellington telles - aula 03Linguagem c   wellington telles - aula 03
Linguagem c wellington telles - aula 03profwtelles
 
Aula6 - Linguagem C
Aula6 - Linguagem CAula6 - Linguagem C
Aula6 - Linguagem Capolllorj
 

Semelhante a Lista Ligada Simples (14)

Linguagem C 09 Ponteiros
Linguagem C 09 PonteirosLinguagem C 09 Ponteiros
Linguagem C 09 Ponteiros
 
Pilha em C
Pilha em CPilha em C
Pilha em C
 
Árvore Binária em C
Árvore Binária em CÁrvore Binária em C
Árvore Binária em C
 
Ling c
Ling cLing c
Ling c
 
Apostila Algoritmos e Estrutura de Dados (AEDS)
Apostila Algoritmos e Estrutura de Dados (AEDS)Apostila Algoritmos e Estrutura de Dados (AEDS)
Apostila Algoritmos e Estrutura de Dados (AEDS)
 
Linguagem C 03 Estruturas De Decisao
Linguagem C 03 Estruturas De DecisaoLinguagem C 03 Estruturas De Decisao
Linguagem C 03 Estruturas De Decisao
 
Função malloc
Função mallocFunção malloc
Função malloc
 
Algoritmos em c #1
Algoritmos em c #1Algoritmos em c #1
Algoritmos em c #1
 
TypeScript - Olhe teu tipo, script slides
TypeScript - Olhe teu tipo, script slidesTypeScript - Olhe teu tipo, script slides
TypeScript - Olhe teu tipo, script slides
 
C++ Standard Template Library
C++ Standard Template LibraryC++ Standard Template Library
C++ Standard Template Library
 
Linguagem C 04 Estruturas De Repeticao
Linguagem C 04 Estruturas De RepeticaoLinguagem C 04 Estruturas De Repeticao
Linguagem C 04 Estruturas De Repeticao
 
Conhecendo ou relembrando C
Conhecendo ou relembrando CConhecendo ou relembrando C
Conhecendo ou relembrando C
 
Linguagem c wellington telles - aula 03
Linguagem c   wellington telles - aula 03Linguagem c   wellington telles - aula 03
Linguagem c wellington telles - aula 03
 
Aula6 - Linguagem C
Aula6 - Linguagem CAula6 - Linguagem C
Aula6 - Linguagem C
 

Lista Ligada Simples

  • 1. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } Código para gerar uma estrutura de dados do tipo lista ligada simples.
  • 2. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox n struct Numero aux atual resp Define o registro. inicio
  • 3. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox n no aux atual resp Troca o nome. inicio
  • 4. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox n no aux atual resp Declara os ponteiros externos. inicio NULL
  • 5. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); prox n no aux atual resp Chama a função. inicio NULL
  • 6. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); prox n no aux atual resp Aloca um nó na memória. inicio NULL4040 atual
  • 7. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp Atribui um valor ao membro do nó. inicio NULL4040 atual
  • 8. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp Atribui NULL ao ponteiro interno prox. inicio NULL4040 atual NULL
  • 9. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp Condição resulta em verdadeiro. inicio NULL4040 atual NULL
  • 10. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp inicio recebe o endereço armazenado em atual. inicio NULL4040 atual NULL 4040 inicio
  • 11. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp aux recebe o endereço armazenado em atual. inicio NULL4040 atual NULL 4040 4040 inicio aux
  • 12. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 no aux atual resp Termina uma iteração. inicio NULL4040 atual NULL 4040 4040 inicio aux sim
  • 13. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Aloca um novo nó. inicio NULL5048 4040 4040 aux sim atual prox inicio
  • 14. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual respinicio NULL5048 4040 4040 aux sim atual prox inicio 42 Atribui um valor ao membro do nó.
  • 15. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Atribui NULL ao ponteiro interno prox. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL
  • 16. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Conecta o nó anterior ao nó atual. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL
  • 17. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp aux aponta para o mesmo endereço que atual aponta. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL 5048
  • 18. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Não será executada outra iteração. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL 5048 não
  • 19. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Condição verificada e resulta Em valor falso. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL 5048 não
  • 20. struct Numero { int n; struct Numero *prox; }; void insere(); typedef struct Numero no; no *inicio=NULL, *aux, *atual; int main() { insere(); system("pause"); } void insere() { char resp[4]; do{ atual = (no*)malloc(sizeof(no)); printf("Digite um numeron"); scanf("%d",&atual->n); atual->prox=NULL; if(inicio==NULL) { inicio=atual; aux = atual; } else { aux->prox=atual; aux = atual; } printf("Deseja continuar (sim/nao)?n"); scanf("%s",&resp); }while(!strcmp(resp,"sim")); } prox 21 aux atual resp Termina o algoritmo. inicio NULL5048 4040 4040 aux sim atual prox inicio 42 NULL 5048 não