Advertisement
Guest User

Untitled

a guest
Oct 31st, 2014
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.46 KB | None | 0 0
  1. #include <stdlib.h>
  2. #include <stdio.h>
  3. #include <string.h>
  4.  
  5. typedef struct no{
  6.     char c;
  7.     struct no* next;
  8.     struct no* lista;
  9. }no;
  10.  
  11. char str[1000];
  12. int pos;
  13.  
  14. no* transformaLista(){
  15.     no *aux, *novo=NULL,*ret;
  16.     while(str[pos]){
  17.         if(str[pos]=='('){
  18.             aux = (no*) malloc(sizeof(no));
  19.             aux->c=str[pos];
  20.             aux->next=NULL;
  21.             pos++;
  22.             aux->lista = transformaLista();
  23.             if(novo==NULL){
  24.                 novo = aux;
  25.                 ret = novo;
  26.             }
  27.             else{
  28.                 novo->next = aux;
  29.                 novo=novo->next;
  30.             }
  31.         }
  32.         else if(str[pos]==')'){
  33.             pos++;
  34.             return ret;
  35.         }
  36.         else{
  37.             aux = (no*) malloc(sizeof(no));
  38.             aux->c=str[pos];
  39.             aux->next=NULL;
  40.             aux->lista = NULL;
  41.             if(novo==NULL){
  42.                 novo = aux;
  43.                 ret = novo;
  44.             }
  45.             else{
  46.                 novo->next = aux;
  47.                 novo=novo->next;
  48.             }
  49.         }
  50.         pos++;
  51.     }
  52.     return ret;
  53. }
  54.  
  55. //Para duplicar o elemento, precisa colocar o ponteiro next = NULL antes de chamar essa função.
  56. no* duplicaElemento(no* raiz){
  57.     no* novo=NULL,*ret,*aux;
  58.     while(raiz!=NULL){
  59.         aux=(no*) malloc(sizeof(no));
  60.         aux->c = raiz->c;
  61.         aux->next=NULL;
  62.         if(raiz->lista==NULL) aux->lista=NULL;
  63.         else{
  64.             aux->lista=duplicaElemento(raiz->lista);
  65.         }
  66.        
  67.         if(novo==NULL){
  68.             novo=aux;
  69.             ret = novo;
  70.         }
  71.         else{
  72.             novo->next = aux;
  73.             novo= novo->next;
  74.         }
  75.         raiz=raiz->next;
  76.     }
  77.    
  78.     return ret;
  79.    
  80. }
  81.  
  82. int main(){
  83.     //freopen("in.txt","r",stdin);
  84.     no* raiz=NULL;
  85.     scanf("%s",str);
  86.     pos=0;
  87.     raiz = transformaLista();
  88.     return 0;
  89. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement