Skip to content

Instantly share code, notes, and snippets.

@Tomcat-42
Created June 25, 2019 16:25
Show Gist options
  • Save Tomcat-42/2546483950a7d44acefd83aeff4ab07b to your computer and use it in GitHub Desktop.
Save Tomcat-42/2546483950a7d44acefd83aeff4ab07b to your computer and use it in GitHub Desktop.
//#include "listaSE.h"
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
typedef struct Lnode
{
int Data;
struct Lnode *Next;
}Lnode;
typedef struct ListaSE
{
Lnode *First;
int Lenght;
Lnode *Last;
}ListaSE;
//inicializa uma lista SE
void init_List(ListaSE *l);
//deleta uma lista SE
void del_List(ListaSE *l);
//printa uma lista SE
void print_List(ListaSE l);
//inserção
void insert_Left(ListaSE *l,int n);
void insert_Rigth(ListaSE *l,int n);
//Remoção
void delete_Left(ListaSE *l);
void delete_Right(ListaSE *l);
//Busca
Lnode *search_List(ListaSE *l,int num);
//Ordenação
void bsort_list(ListaSE *l,int n);
void swap(int *a,int *b);
//inserção linear de n elementos
void insert_list_n(ListaSE *l,int n);
//Geração de lista pseudo-randômica de n elementos
void random_list_n(ListaSE *l,int n,int down,int up);
//inverte os elementos da lista
void inv_list(ListaSE *l);
int main()
{
srand(time(NULL));
ListaSE L1;
init_List(&L1);
//insert_list_n(&L1,5);
random_list_n(&L1,10,1,6);
print_List(L1);
//bsort_list(&L1,L1.Lenght);
inv_list(&L1);
print_List(L1);
//printf("%d\n",search_List(&L1,5)->Data);
return 0;
}
void init_List(ListaSE *l)
{
l->First = l->Last = NULL;
l->Lenght = 0;
}
void del_List(ListaSE *l)
{
Lnode *p = l->First;
while(p && l->Lenght--)
{
l->First = l->First->Next;
free(p);
p = l->First;
}
l->Last = l->First = NULL;
}
void print_List(ListaSE l)
{
Lnode *p = l.First;
while(p)
{
printf("%s%d%s",(p==l.First)?("["):(""),p->Data,(p->Next==NULL)?("]\n"):(", "));
p = p->Next;
}
}
void insert_Left(ListaSE *l,int n)
{
//Aloca um novo nó
Lnode *no = (Lnode *)malloc(sizeof(Lnode));
//Insere n no compo de dados do novo nó
no->Data = n;
//Insere o novo nó a esquerda da lista
no->Next = l->First;
l->First = no;
//Se a lista esiver vazia, o primeiro é também o ultimo elemento
if(l->Last ==NULL) l->Last = no;
l->Lenght++;
}
void insert_Rigth(ListaSE *l,int n)
{
//Aloca um novo nó
Lnode *no = (Lnode *)malloc(sizeof(Lnode));
//Se a alocação Falhou
if(no ==NULL)
{
printf("Erro de alocação\n");
return;
}
//Insere o nó a direita
no->Data = n;
no->Next = NULL;
//Se a lista estava vazia
if(l->Last == NULL) l->First = l->Last = no;
else
{
l->Last->Next = no;
l->Last = no;
}
l->Lenght++;
}
void delete_Left(ListaSE *l)
{
Lnode *p = l->First;
l->First = l->First->Next;
free(p);
//Se a lista ficou vazia
if(l->First==NULL) l->Last = NULL;
l->Lenght--;
}
void delete_Right(ListaSE *l)
{
Lnode *p = l->First;
//move o ponteiro até o penúltimo elemento
while(p->Next->Next) p = p->Next;
//Desaloca o último elemento
free(p->Next);
l->Last = p;
p->Next = NULL;
//Se a lista ficou vazia
if(l->First==NULL) l->Last = NULL;
l->Lenght--;
}
Lnode *search_List(ListaSE *l,int num)
{
Lnode *p = l->First;
while(p && p->Data != num) p=p->Next;
//Retorna um ponteiro para o nó encontrado, retorna NULL se não foi encontrado nada
return p;
}
void bsort_list(ListaSE *l,int n)
{
Lnode *p=l->First;
int swaped = 0,aux;
for(int i=0;(i<n-1 && p);i++)
{
if(p->Data > p->Next->Data)
{
swap(&(p->Data),&(p->Next->Data));
swaped = 1;
}
p = p->Next;
}
if(!swaped) return;
return(bsort_list(l,n-1));
}
void swap(int *a,int *b)
{
int p = *a;
*a = *b;
*b = p;
}
void insert_list_n(ListaSE *l,int n)
{
int num;
for(int i=0;i<n;i++)
{
scanf("%d",&num);
insert_Rigth(l,num);
}
}
void random_list_n(ListaSE *l,int n,int down,int up)
{
for(int i=0;i<n;i++)
insert_Rigth(l,((rand()%up+1)+down));
}
void inv_list(ListaSE *l)
{
//ponteiros auxiliares
Lnode *ant=NULL,*atual=l->First,*prox=NULL;
while(atual)
{
prox = atual->Next;
atual->Next = ant;
ant = atual;
atual = prox;
}
l->First = ant;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment