Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<bits/stdc++.h>
- using namespace std;
- /*Listas Duplamente Encadeadas*/
- typedef struct lista{
- char nome[30];
- int num;
- struct lista *prox,*ant;
- }LISTA;
- LISTA *inicio,*fim,*temp,*aux,*ant;
- /*Inserir Inicio*/
- void InserirInicio(){
- int x;
- do{
- temp=(LISTA*)malloc(sizeof(LISTA));
- printf("Numero: ");
- scanf("%d",&temp->num);
- printf("Nome: ");
- do{
- gets(temp->nome);
- }while(strlen(temp->nome)==0);
- if(inicio==NULL){
- inicio=temp;
- inicio->prox=inicio->ant=NULL;
- }
- else{
- temp->prox=inicio;
- inicio->ant=temp;
- temp->ant=NULL;
- inicio=temp;
- }
- printf("Continuar? : ");
- scanf("%d",&x);
- }while(x==1);
- }
- /*Inserção no fim*/
- void InserirFim(){
- int c;
- do{
- temp=(LISTA*)malloc(sizeof(LISTA));
- printf("Numero: ");
- scanf("%d",&temp->num);
- printf("Nome: ");
- do{
- gets(temp->nome);
- }while(strlen(temp->nome)==0);
- if(inicio==NULL){
- inicio=temp;
- fim=temp;
- inicio->prox=inicio->ant=NULL;
- }
- else{
- fim->prox=temp;
- temp->ant=fim;
- temp->prox=NULL;
- fim=temp;
- }
- printf("Continuar?: ");
- scanf("%d",&c);
- }while(c==1);
- }
- /*Consulta Global*/
- void consulta(){
- temp=inicio;
- while(temp!=NULL){
- printf("Numero:%d\nNome :%s\n\n",temp->num,temp->nome);
- temp=temp->prox;
- }
- }
- /*Inserir ordenada ascendentemente*/
- void InserirAscendentemente(){
- int x;
- do{
- temp=(LISTA*)malloc(sizeof(LISTA));
- printf("Numero: ");
- scanf("%d",&temp->num);
- printf("Nome: ");
- do{
- gets(temp->nome);
- }while(strlen(temp->nome)==0);
- if(inicio==NULL){
- inicio=temp;
- inicio->prox=inicio->ant=NULL;
- }
- else{
- if(temp->num < inicio->num){
- temp->prox=inicio;
- inicio->ant=temp;
- temp->ant=NULL;
- inicio=temp;
- }
- else{
- aux=inicio;
- while(aux->prox!=NULL && temp->num > aux->num){
- aux=aux->prox;
- }
- if(temp->num < aux->num){
- aux->prox=temp;
- temp->ant=aux;
- temp->prox=NULL;
- }
- else{
- temp->prox=aux;
- temp->ant=aux;
- aux->ant->prox=temp;
- aux->ant=temp;
- }
- }
- }
- printf("Continuar?: ");
- scanf("%d",&x);
- }while(x==1);
- }
- void InserirDescendentemente(){
- int x;
- do{
- temp=(LISTA*)malloc(sizeof(LISTA));
- printf("Numero: ");
- scanf("%d",&temp->num);
- printf("Nome: ");
- do{
- gets(temp->nome);
- }while(strlen(temp->nome)==0);
- if(inicio==NULL){
- inicio=temp;
- inicio->prox=inicio->ant=NULL;
- }
- else{
- if(temp->num > inicio->num){
- temp->prox=inicio;
- inicio->ant=temp;
- temp->ant=NULL;
- inicio=temp;
- }
- else{
- aux=inicio;
- while(aux->prox!=NULL && temp->num < aux->num){
- aux=aux->prox;
- }
- if(temp->num > aux->num){
- aux->prox=temp;
- temp->ant=aux;
- temp->prox=NULL;
- }
- else{
- temp->prox=aux;
- temp->ant=aux;
- aux->ant->prox=temp;
- aux->ant=temp;
- }
- }
- }
- printf("Continuar?: ");
- scanf("%d",&x);
- }while(x==1);
- }
- void eliminar(){
- int x;
- printf("Numero a eliminar:");
- scanf("%d",&x);
- aux=inicio;
- while(x!=temp->num && aux->prox!=NULL){
- aux=aux->prox;
- }
- if(x==temp->num){
- if(aux==inicio){
- inicio=inicio->prox;
- if(inicio!=NULL){
- inicio->ant=NULL;
- }
- }
- else{
- aux->ant->prox=aux->prox;
- if(aux->prox!=NULL){
- aux->prox->ant=aux->ant;
- }
- }
- free(aux);
- printf("Foi eliminado um nodo!\n");
- }
- else{
- printf("Não existe\n");
- }
- }
- /*Main*/
- int main(){
- int op;
- do{
- printf("Listas Simplesmente Encadeadas\n");
- printf("1-Inserir no inicio\n");
- printf("2-Inserir no fim\n");
- printf("3-CONSULTA GLOBAL\n");
- printf("4-Inserir ordenada ascendentemente\n");
- printf("5-Inserir ordenada descendentemente\n");
- printf("6-Eliminacao\n");
- printf("7-Sair\n");
- printf("Escolha: ");
- scanf("%d",&op);
- system("cls");
- switch(op){
- case 1:InserirInicio();
- break;
- case 2:InserirFim();
- break;
- case 3:consulta();
- break;
- case 4:InserirAscendentemente();
- break;
- case 5:InserirDescendentemente();
- break;
- case 6:eliminar();
- break;
- }
- }while (op!=7);
- }
Add Comment
Please, Sign In to add comment