Lista simplesmente encadeada C
Publicado por Andrey (última atualização em 21/06/2016)
[ Hits: 4.603 ]
Homepage: https://github.com/andreycdias
Download 6404.singlyLinkedList.c
Download headed_linked_list.c (versão 2)
Lista simplesmente encadeada dinâmica em C.
/*
* simplyLinkedList.c
* andrey cadima dias 2016 fgz <fgz@arch>
*
* functions: insert begin/end,
* remove, display and size;
*
*/
#include <stdlib.h>
#include <stdio.h>
typedef struct NODE{
int data;
struct NODE*next;
}node;
node*aux; //global assist var
void startList(node*pointer){
pointer->next = NULL;
}
node*create(){
node*head = (node*)malloc(sizeof(node));
if(!head){
printf ("\nMemory unavailable\n");
exit(1);
}else{
startList(head);
return head;
}
}
int empty (node*pointer){
if(pointer->next==NULL) return 1;
else return 0;
}
void insertBegin(node*pointer, int info){
node*new = (node*)malloc(sizeof(node));
new->data = info;
aux = pointer->next;
pointer->next = new;
new->next = aux;
}
void insertEnd(node*pointer, int info){
while(pointer->next!=NULL){
pointer = pointer->next;
}
pointer->next = (node*)malloc(sizeof(node));
pointer = pointer->next;
pointer->data = info;
pointer->next = NULL;
}
void display(node*pointer){
if(empty(pointer)){
printf ("\nEmpty list\n");
return ;
}
aux = pointer->next;
printf ("\n Current list: ");
while(aux!=NULL){
printf ("%i => ", aux->data);
aux=aux->next;
}
printf ("NULL\n");
}
void removeN(node*pointer, int info){
while(pointer->next!=NULL && pointer->next->data!=info){
pointer=pointer->next;
}
if(pointer->next==NULL){
printf ("\nElement not found on the list\n");
return ;
}
aux = pointer->next;
pointer->next = aux->next;
free(aux);
return ;
}
node* removeBegin(node*pointer){
if(empty(pointer)){
printf ("\nEmpty list\n");
return ;
}else{
aux = pointer->next;
pointer->next = aux->next;
return aux;
}
}
void sizeList(node*pointer){
int cont=0;
aux = pointer->next;
while(aux!=NULL){
aux = aux->next;
cont++;
}
printf ("\n %i Element(s)\n", cont);
}
int menu(){
int opt;
printf ("0_Exit\n");
printf ("1_Insert begin\n");
printf ("2_Insert end\n");
printf ("3_Remove by a number\n");
printf ("4_Remove begin\n");
printf ("5_Display\n");
printf ("6_Size\n");
printf ("7_Clear\n");
printf ("Option: ");scanf ("%i", &opt);
return opt;
}
int readData(){
int info;
printf ("\nEnter the number: ");
scanf ("%i", &info);
return info;
}
int main(int argc, char **argv){
int opt,info;
node*head;
head = create();
do{
printf ("\n");
opt = menu();
switch(opt){
case 1:
info = readData();
insertBegin(head, info);
break;
case 2:
info = readData();
insertEnd(head, info);
break;
case 3:
if(empty(head))
printf ("\nEmpty list\n");
else{
info = readData();
removeN(head, info);
}
break;
case 4:
removeBegin(head);
break;
case 5:
display(head);
break;
case 6:
if(empty(head))
printf ("\nEmpty list - 0 Elements\n");
else
sizeList(head);
break;
case 7:
system("clear");
break;
default:
if(opt!=0) printf ("\nInvalid option");
}
}while(opt);
return 0;
}
Ler N números e ver qual é o maior
CalDOS - 60 funções em uma calculadora
Faixa salarial de funcionários
Nenhum comentário foi encontrado.
Como extrair chaves TOTP 2FA a partir de QRCODE (Google Authenticator)
Linux em 2025: Segurança prática para o usuário
Desktop Linux em alta: novos apps, distros e privacidade marcam o sábado
IA chega ao desktop e impulsiona produtividade no mundo Linux
Novos apps de produtividade, avanços em IA e distros em ebulição agitam o universo Linux
Como instalar o repositório do DBeaver no Ubuntu
Como instalar o Plex Media Server no Ubuntu
Digitando underscore com "shift" + "barra de espaços"
Como ativar a lixeira e recuperar aquivos deletados em um servidor Linux
Como mudar o nome de dispositivos Bluetooth via linha de comando
dpkg: erro: gatilho de arquivo duplicado chamado pelo arquivo de nome (6)
Instalação não está resolvendo as dependencias (2)
Captação de áudio no zorin linux começa a diminuir com o tempo (5)
Alternativas ao Multilogin para gerenciamento de múltiplas contas/prof... (0)









