Lista simplesmente encadeada C

Publicado por Andrey (última atualização em 21/06/2016)

[ Hits: 4.356 ]

Homepage: https://github.com/andreycdias

Download 6404.singlyLinkedList.c

Download headed_linked_list.c (versão 2)




Lista simplesmente encadeada dinâmica em C.

  



Versões atualizadas deste script

Versão 2 - Enviado por Andrey em 16/05/2016

Changelog: Lista simplesmente encadeada C (com cabeça)

Download headed_linked_list.c


Esconder código-fonte

/*
 * 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;
}

Scripts recomendados

Matriz Bidimensional

Sequencia Fibonacci

Locadora

[C] Raiz quadrada

NCURSES - Parte III


  

Comentários

Nenhum comentário foi encontrado.


Contribuir com comentário




Patrocínio

Site hospedado pelo provedor RedeHost.
Linux banner

Destaques

Artigos

Dicas

Tópicos

Top 10 do mês

Scripts