Lista simplesmente encadeada C
Publicado por Andrey (última atualização em 21/06/2016)
[ Hits: 4.675 ]
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;
}
HaiDownlodManager - simples programa para baixar arquivos
Simulador do Sorteio da Mega Sena
Conversor decimal para binario para GNU/LINUX
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 (20)
Quais os códigos mais dificeis que vcs sabem fazer? (8)
Pfsense inacessivel após um periodo de tempo (1)
Não consigo instalar distro antiga no virtualbox nem direto no hd (9)









