Lista simplesmente encadeada C
Publicado por Andrey (última atualização em 21/06/2016)
[ Hits: 4.711 ]
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;
}
Sequência fibonacci com 35 linhas e for
Simulador do Sorteio da Mega Sena
Funções de soma e subtração de matrizes alocadas dinamicamente
Nenhum comentário foi encontrado.
Cinnamon seria a aposta acertada frente às outras interfaces gráficas mais populares?
KDE Plasma - porque pode ser a melhor opção de interface gráfica
Gentoo: detectando impressoras de rede e como fixar uma impressora por IP
Como o GNOME conseguiu o feito de ser preterido por outras interfaces gráficas
Por que sua empresa precisa de uma PKI (e como automatizar EMISSÕES de certificados via Web API)
Instalando NoMachine no Gentoo com Systemd (acesso Remoto em LAN)
Vou destruir sua infância:) (8)
Interface gráfica com problema (2)
Instalar Linux em notebook Sony Vaio VPCEG13EB (13)









