Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- struct ptr{
- int valor;
- struct ptr *prox, *ant;
- };
- struct desc{
- int cont;
- struct ptr *prim, *ult;
- };
- struct desc *lista, *lista1;
- void inserir_ordenada2(int valor){
- struct ptr *aux;
- if(valor>=0)
- lista->cont++;
- if(lista->prim==(struct ptr *)NULL){
- lista->prim=(struct ptr *)malloc(sizeof(struct ptr));
- lista->prim->valor=valor;
- lista->prim->ant=(struct ptr *)NULL;
- lista->prim->prox=(struct ptr *)NULL;
- }else{
- aux=lista->prim;
- if(valor<aux->valor){
- aux->ant=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->valor=valor;
- aux->ant->prox=aux;
- aux->ant->ant=(struct ptr *)NULL;
- aux=aux->ant;
- lista->prim=aux;
- }else{
- while(valor>aux->valor && aux->prox!=(struct ptr *)NULL)
- aux=aux->prox;
- if(valor<aux->valor){
- aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->prox->valor=valor;
- aux->ant->prox->ant=aux->ant;
- aux->ant->prox->prox=aux;
- aux->ant=aux->ant->prox;
- }else{
- aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->prox->valor=valor;
- aux->prox->ant=aux;
- aux->prox->prox=(struct ptr *)NULL;
- lista->ult=aux->prox;
- }
- }
- }
- }
- void criar_crescente2(){
- lista=(struct desc *)malloc(sizeof(struct desc));
- lista->prim=(struct ptr *)NULL;
- lista->ult=(struct ptr *)NULL;
- int valor;
- printf("Digite o valor:");
- scanf("%d", &valor);
- lista->cont=0;
- while(valor>=0){
- inserir_ordenada2(valor);
- printf("Digite o valor:");
- scanf("%d", &valor);
- }
- }
- void inserir_ordenada(int valor){
- struct ptr *aux;
- if(valor>=0)
- lista1->cont++;
- if(lista1->prim==(struct ptr *)NULL){
- lista1->prim=(struct ptr *)malloc(sizeof(struct ptr));
- lista1->prim->valor=valor;
- lista1->prim->ant=(struct ptr *)NULL;
- lista1->prim->prox=(struct ptr *)NULL;
- }else{
- aux=lista1->prim;
- if(valor<aux->valor){
- aux->ant=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->valor=valor;
- aux->ant->prox=aux;
- aux->ant->ant=(struct ptr *)NULL;
- aux=aux->ant;
- lista1->prim=aux;
- }else{
- while(valor>aux->valor && aux->prox!=(struct ptr *)NULL)
- aux=aux->prox;
- if(valor<aux->valor){
- aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->prox->valor=valor;
- aux->ant->prox->ant=aux->ant;
- aux->ant->prox->prox=aux;
- aux->ant=aux->ant->prox;
- }else{
- aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->prox->valor=valor;
- aux->prox->ant=aux;
- aux->prox->prox=(struct ptr *)NULL;
- lista1->ult=aux->prox;
- }
- }
- }
- }
- void criar_crescente(){
- lista1=(struct desc *)malloc(sizeof(struct desc));
- lista1->prim=(struct ptr *)NULL;
- lista1->ult=(struct ptr *)NULL;
- int valor;
- printf("Digite o valor:");
- scanf("%d", &valor);
- lista1->cont=0;
- while(valor>=0){
- inserir_ordenada(valor);
- printf("Digite o valor:");
- scanf("%d", &valor);
- }
- }
- void mostra(){
- struct ptr *aux;
- aux=lista->prim;
- if(lista->prim==(struct ptr *)NULL)
- printf("Lista vazia");
- else{
- while(aux!=(struct ptr *)NULL){
- printf("%d " , aux->valor);
- aux=aux->prox;
- }
- }
- printf("\n%d ", lista->cont);
- }
- void juntar_lista(){
- struct ptr *aux;
- while(lista1!=(struct ptr *)NULL){
- lista->cont++;
- aux=lista->prim;
- if(lista1->prim->valor<aux->valor){
- aux->ant=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->prox=aux;
- aux->ant->ant=(struct ptr *)NULL;
- aux->ant->valor=lista1->prim->valor;
- aux=aux->ant;
- lista->prim=aux;
- }else{
- while(lista1->prim->valor>aux->valor && aux->prox!=(struct ptr *)NULL);
- aux=aux->prox;
- if(lista1->prim->valor<aux->valor){
- aux->ant->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->ant->prox->ant=aux->ant;
- aux->ant->prox->prox=aux;
- aux->ant=aux->ant->prox;
- aux->ant->valor=lista1->prim->valor;
- }else{
- aux->prox=(struct ptr *)malloc(sizeof(struct ptr));
- aux->prox->ant=aux;
- aux->prox->prox=(struct ptr *)NULL;
- aux->prox->valor=lista1->prim->valor;
- aux=aux->prox;
- lista->ult=aux;
- }
- }
- lista1->prim=lista1->prim->prox;
- }
- }
- main(){
- criar_crescente2();
- criar_crescente();
- juntar_lista();
- mostra();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement