Lista encadeada com cabecalho
Publicado por Leonardo Barrozo dos Santos 10/03/2003
[ Hits: 12.721 ]
Código fonte de um programa sobre lista encadeada com cabeçalho.
/*
+-----------------------------------------------------------+
| Programador: Leonardo Barrozo dos Santos |
| Descrição..: Programa que demonstra o funcionamente de |
| ...........: uma lista encadeada com cabeçalho. |
| Arquivo....: cabeclistaenca.c |
+-----------------------------------------------------------+
+-+---+-+
+-----------------| | | |----------------+
| +-+---+-+ |
| |
+---+-+ +---+-+ +---+-+ +---+-+
| | |-->| | |-->| | |--> ... | | |
+---+-+ +---+-+ +---+-+ +---+-+
*/
#include <stdio.h>
#include <stdlib.h>
#define MALLOC(x) ((x *) malloc (sizeof(x)))
void insere (void);
void exibir (void);
void exclui (void);
struct no{
int item;
struct no *proximo;
};
typedef struct no no_t;
no_t *novo;
struct cabec{
int tamanho;
no_t *first;
no_t *last;
};
typedef struct cabec cabec_t;
cabec_t *t;
void main(void)
{
int op,k;
t=MALLOC(cabec_t);
t->tamanho = 0;
novo = t->first = t->last = 0;
for (;;)
{
system("clear");
printf("\n1-Inserir");
printf("\n2-Exibir");
printf("\n3-Excluir");
printf("\n4-Sair");
printf("\nSua opção: ");
scanf("%d",&op);
switch(op){
case 1 : insere();
break;
case 2 : exibir();
break;
case 3 : exclui();
break;
case 4 : exit(0);
default: system("clear");
printf("Opção Errada");
scanf("%d",&k);
}
}
}
void insere(void)
{
int j;
printf("ITEM: ");
scanf("%d",&j);
novo = MALLOC(no_t);
novo->proximo = 0;
novo->item = j;
if (t->first == 0) t->first = novo;
else
t->last->proximo = novo;
t->last = novo;
t->tamanho++;
}
void exibir(void)
{
no_t *p;
int k;
system("clear");
if (t->first==0)
{
system("clear");
printf("Lista Vazia");
scanf("%d",&k);
return;
}
p = t->first;
do{
printf("\nITEM %d",p->item);
p = p->proximo;
}while (p!=0);
printf("\n\nTEM %d ITENS NA LISTA",t->tamanho);
scanf("%d",&k);
}
void exclui(void)
{
no_t *anterior, *novo1;
int j,i;
novo1 = t->first;
if (t->first==0)
{
system("clear");
printf("Lista Vazia");
scanf("%d",&i);
return;
}
i=0;
system("clear");
printf("Digite o item que deseja excluir: ");
scanf("%d",&j);
do{
if (novo1->item == j)
{
if (t->first->proximo == 0)
{
novo = t->first = t->last = 0;
t->tamanho = 0;
break;
}
else
if (novo1->proximo == 0)
{
anterior->proximo=0;
t->last = anterior;
free(novo1);
t->tamanho--;
break;
}
else
if (i==0 && t->first->proximo != 0)
{
t->first = novo1->proximo;
novo1->proximo = 0;
free(novo1);
t->tamanho--;
break;
}
else
if (novo1->proximo != 0)
{
anterior->proximo = novo1->proximo;
novo1->proximo = 0;
free(novo1);
t->tamanho--;
break;
}
}
anterior = novo1;
novo1 = novo1->proximo;
i = i + 1;
}while(novo1 != 0);
}
Desenhando Nuvens ou o Fractal de Plasma
Nenhum comentário foi encontrado.
Cirurgia para acelerar o openSUSE em HD externo via USB
Void Server como Domain Control
Modo Simples de Baixar e Usar o bash-completion
Monitorando o Preço do Bitcoin ou sua Cripto Favorita em Tempo Real com um Widget Flutuante
[Resolvido] VirtualBox can't enable the AMD-V extension
Como verificar a saúde dos discos no Linux
Como instalar , particionar, formatar e montar um HD adicional no Linux?
Como automatizar sua instalação do Ubuntu para desenvolvimento de software.
Fiz uma pergunta no fórum mas não consigo localizar (13)
Quais os códigos mais dificeis que vcs sabem fazer? (2)
Não consigo instalar distro antiga no virtualbox nem direto no hd (7)
Servidor Ubuntu 24.04 HD 500 não tenho espaço na \home\adminis... [RES... (8)
Dá para criar um bom jogo usando a linguagem de programação C? [RESOLV... (1)









