Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdlib.h>
- #include <stdio.h>
- #include <string.h>
- typedef struct no{
- char c;
- struct no* next;
- struct no* lista;
- }no;
- char str[1000];
- int pos;
- no* transformaLista(){
- no *aux, *novo=NULL,*ret;
- while(str[pos]){
- if(str[pos]=='('){
- aux = (no*) malloc(sizeof(no));
- aux->c=str[pos];
- aux->next=NULL;
- pos++;
- aux->lista = transformaLista();
- if(novo==NULL){
- novo = aux;
- ret = novo;
- }
- else{
- novo->next = aux;
- novo=novo->next;
- }
- }
- else if(str[pos]==')'){
- pos++;
- return ret;
- }
- else{
- aux = (no*) malloc(sizeof(no));
- aux->c=str[pos];
- aux->next=NULL;
- aux->lista = NULL;
- if(novo==NULL){
- novo = aux;
- ret = novo;
- }
- else{
- novo->next = aux;
- novo=novo->next;
- }
- }
- pos++;
- }
- return ret;
- }
- //Para duplicar o elemento, precisa colocar o ponteiro next = NULL antes de chamar essa função.
- no* duplicaElemento(no* raiz){
- no* novo=NULL,*ret,*aux;
- while(raiz!=NULL){
- aux=(no*) malloc(sizeof(no));
- aux->c = raiz->c;
- aux->next=NULL;
- if(raiz->lista==NULL) aux->lista=NULL;
- else{
- aux->lista=duplicaElemento(raiz->lista);
- }
- if(novo==NULL){
- novo=aux;
- ret = novo;
- }
- else{
- novo->next = aux;
- novo= novo->next;
- }
- raiz=raiz->next;
- }
- return ret;
- }
- int main(){
- //freopen("in.txt","r",stdin);
- no* raiz=NULL;
- scanf("%s",str);
- pos=0;
- raiz = transformaLista();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement