Buscar

LISTAS DUPLAMENTE ENCADEADA, ORDENADA e PILHA LIFO

Prévia do material em texto

######################## LISTA DUPLAMENTE ENCADEADA ######################## 
 
#include<stdio.h> 
#include<stdlib.h> 
 
 struct Tdupla 
 { 
 int ele; 
 struct Tdupla *next; 
 struct Tdupla *back; 
 }; 
 
 void Insert(int e, struct Tdupla **start) 
 { 
 
 struct Tdupla *p, *aux; 
 
 p=new(struct Tdupla); 
 
 p->ele=e; 
 
 aux=*start; 
 
 if(aux==NULL) 
 { 
 p->next=NULL; 
 p->back=NULL; 
 *start=p; 
 } 
 else 
 { 
 if(aux->next==NULL) 
 { 
 p->next=NULL; 
 p->back=aux; 
 aux->next=p; 
 } 
 else 
 { 
 while(aux->next!=NULL) 
 { 
 aux=aux->next; 
 } 
 p->next=NULL; 
 p->back=aux; 
 aux->next=p; 
 } 
 } 
 } 
 
 void Remove(int e, struct Tdupla **start) 
 { 
 
 struct Tdupla *aux,*del; 
 
 aux=*start; 
 
 if(aux==NULL) 
 { 
 printf("Lista vazia.\n"); 
 } 
 else 
 { 
 while(e!=aux->ele && aux->next!=NULL) 
 { 
 aux=aux->next; 
 } 
 
 if(aux->next==NULL && aux->ele!=e) 
 { 
 printf("Elemento nao encontrado.\n"); 
 } 
 else 
 { 
 
 if(aux->ele==e && aux->next!=NULL && aux->back!=NULL) 
 { 
 del=aux; 
 aux=aux->back; 
 aux->next=del->next; 
 aux=aux->next; 
 aux->back=del->back; 
 
 delete(del); 
 } 
 else 
 { 
 if(aux->ele==e && aux->back==NULL && aux-
>next!=NULL) 
 { 
 del=aux; 
 aux=aux->next; 
 aux->back=NULL; 
 *start=aux; 
 
 delete(del); 
 } 
 else 
 { 
 if(aux->ele==e && aux->next==NULL && aux-
>back!=NULL) 
 { 
 del=aux; 
 aux=aux->back; 
 aux->next=NULL; 
 
 delete(del); 
 } 
 else 
 { 
 if(aux->ele==e && aux->back==NULL 
&& aux->next==NULL) 
 { 
 delete(aux); 
 *start=NULL; 
 } 
 } 
 } 
 } 
 
 printf("Elemento removido.\n"); 
 
 } 
 
 
 } 
 
 } 
 
 void Print(struct Tdupla **start) 
 { 
 
 struct Tdupla *aux; 
 
 aux=*start; 
 
 if(aux==NULL) 
 { 
 printf("Lista vazia."); 
 } 
 else 
 { 
 while(aux!=NULL) 
 { 
 printf("%d - ",aux->ele); 
 aux=aux->next; 
 } 
 } 
 
 } 
 
 main() 
{ 
 
 struct Tdupla *begin=NULL; 
 
 int a,b; 
 
 while(1) 
 { 
 system("cls"); 
 
 printf("1-Insert\n2-Delete\n3-Print\n0-Sair.\n"); 
 scanf("%d",&a); 
 
 system("cls"); 
 
 if(a==1) 
 { 
 scanf("%d",&b); 
 Insert(b,&begin); 
 } 
 if(a==2) 
 { 
 scanf("%d",&b); 
 Remove(b,&begin); 
 system("pause>>NULL"); 
 } 
 if(a==3) 
 { 
 Print(&begin); 
 system("pause>>NULL"); 
 } 
 
 if(a==0) 
 break; 
 } 
 
} 
 
######################## LISTA DUPLAMENTE ENCADEADA ######################## 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
######################## LISTA INSERE ORDENADO ######################## 
 
#include<stdio.h> 
#include<stdlib.h> 
 
struct Tlista 
{ 
 int ele; 
 struct Tlista *next; 
}; 
 
 
void Insert(int e, struct Tlista **start) 
{ 
 struct Tlista *p,*aux,*aux2; 
 
 
 p=new(struct Tlista); 
 
 p->ele=e; 
 p->next=NULL; 
 
 aux=*start; 
 
 if (*start==NULL) 
 { 
 *start=p; 
 } 
 else 
 { 
 if(p->ele<aux->ele) 
 { 
 p->next=aux; 
 *start=p; 
 } 
 else 
 { 
 while(p->ele>aux->ele && aux->next!=NULL) 
 { 
 aux2=aux; 
 aux=aux->next; 
 } 
 if(aux->next==NULL && aux->ele<=p->ele) 
 { 
 p->next=NULL; 
 aux->next=p; 
 } 
 else 
 { 
 p->next=aux; 
 aux2->next=p; 
 } 
 } 
 } 
} 
 
void Remove(int e, struct Tlista **start) 
{ 
 struct Tlista *aux,*aux2; 
 
 aux=*start; 
 
 if(aux==NULL) 
 { 
 printf("lista vazia.\n"); 
 } 
 else 
 { 
 if(e==aux->ele) 
 { 
 *start=aux->next; 
 delete(aux); 
 } 
 else 
 { 
 while(aux->ele!=e && aux->next!=NULL) 
 { 
 aux2=aux; 
 aux=aux->next; 
 } 
 
 if(aux->next==NULL && aux->ele!=e) 
 { 
 printf("elemento nao encontrado.\n"); 
 } 
 else 
 { 
 if(aux->next==NULL) 
 { 
 aux2->next=NULL; 
 delete(aux); 
 } 
 else 
 { 
 aux2->next=aux->next; 
 delete(aux); 
 } 
 printf("elemento deletado.\n"); 
 } 
 } 
 } 
} 
 
void Print(struct Tlista **start) 
{ 
 struct Tlista *aux; 
 
 aux=*start; 
 
 while(aux!=NULL) 
 { 
 printf("%d - ",aux->ele); 
 aux=aux->next; 
 } 
} 
 
main() 
{ 
 
 struct Tlista *begin=NULL; 
 
 int a,b; 
 
 while(1) 
 { 
 system("cls"); 
 printf("1-Insert\n2-Delete\n3-Print\n0-Sair.\n"); 
 scanf("%d",&a); 
 system("cls"); 
 
 if(a==1) 
 { 
 scanf("%d",&b); 
 Insert(b,&begin); 
 } 
 if(a==2) 
 { 
 scanf("%d",&b); 
 Remove(b,&begin); 
 system("pause>>NULL"); 
 } 
 if(a==3) 
 { 
 Print(&begin); 
 system("pause>>NULL"); 
 } 
 
 if(a==0) 
 break; 
 } 
 
} 
 
######################## LISTA INSERE ORDENADO ######################## 
############################### PILHA LIFO ############################ 
 
#include<stdio.h> 
#include<stdlib.h> 
 
struct Tpilha 
{ 
 int ele; 
 struct Tpilha *down; 
}; 
 
void Insert(int e, struct Tpilha **top) 
{ 
 
 struct Tpilha *p, *aux; 
 
 p=new(struct Tpilha); 
 p->ele=e; 
 
 aux=*top; 
 
 if(aux==NULL) 
 { 
 p->down=NULL; 
 *top=p; 
 } 
 else 
 { 
 p->down=aux; 
 *top=p; 
 } 
 
} 
 
int Remove(struct Tpilha **top) 
{ 
 
 int e; 
 struct Tpilha *aux; 
 
 aux=*top; 
 
 if(aux==NULL) 
 { 
 printf("Pilha vazia.\n"); 
 } 
 else 
 { 
 e=aux->ele; 
 
 if(aux->down==NULL) 
 { 
 *top=NULL; 
 delete(aux); 
 } 
 else 
 { 
 *top=aux->down; 
 delete(aux); 
 } 
 printf("Elemento %d removido",e); 
 return e; 
 } 
} 
 
void Print(struct Tpilha **top) 
{ 
 
 struct Tpilha *aux; 
 
 aux=*top; 
 
 while(aux!=NULL) 
 { 
 printf("%d\n",aux->ele); 
 aux=aux->down; 
 } 
 
} 
 
main() 
{ 
 
 struct Tpilha *first; 
 first=NULL; 
 
 int a,b; 
 
 while(1) 
 { 
 system("cls"); 
 
 printf("1-Insert\n2-Remove\n3-Print\n0-Sair.\n"); 
 scanf("%d",&a); 
 
 system("cls"); 
 
 if(a==1) 
 { 
 scanf("%d",&b); 
 Insert(b,&first); 
 } 
 if(a==2) 
 { 
 b=Remove(&first); 
 system("pause>>NULL"); 
 } 
 if(a==3) 
 { 
 Print(&first); 
 system("pause>>NULL"); 
 } 
 
 if(a==0) 
 break; 
 } 
 
} 
 
############################### PILHA LIFO ############################

Continue navegando