Advertisement
vinocastro

ME 6

May 8th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.86 KB | None | 0 0
  1. GeneralizedListNode * readInput()
  2. {
  3.   char symbol;
  4.   Stack S;
  5.   InitStack(&S);
  6.   while(scanf("%c", &symbol) != EOF)
  7.   {
  8.       //    if symbol is left parenthesis
  9.     if(symbol == '(')
  10.     {
  11.     //  push to stack S
  12.     StackElemType x;
  13.     x.character = symbol;
  14.     x.generalizedListNode = NULL;
  15.     PUSH(&S,x);
  16.     }
  17.     //  else if symbol is an atom
  18.     else if(isAtom(symbol))
  19.     {  
  20.         //      push to stack S
  21.         StackElemType x;
  22.         x.character = symbol;
  23.         x.generalizedListNode = NULL;
  24.         PUSH(&S,x);
  25.     }
  26.     //  else if symbol is right parenthesis
  27.     else if(symbol == ')')
  28.     {  
  29.         StackElemType x;
  30.         if(!IsEmptyStack(&S))
  31.         {  
  32.             // pop a stack element,say top, from S (make sure stack is not empty before doing so)
  33.             POP(&S,&x); // x = top
  34.         }
  35.         //      set prevNode to null
  36.         GeneralizedListNode *prevnode = NULL;
  37.         //      while stack S is not empty
  38.         while(!IsEmptyStack(&S))
  39.         {  
  40.             //          if top is an atom
  41.             if(isAtom(x.character) == 1)
  42.             {  
  43.                 //              create a corresponding GeneralizedListNode, say node
  44.                 //                  with tag=0;atom=top.character;list=null;link=prevNode
  45.                 GeneralizedListNode *node = (GeneralizedListNode*) malloc(sizeof(GeneralizedListNode));
  46.                 node->tag = 0;
  47.                 node->dataAtom = x.character;
  48.                 node->dataList = NULL;
  49.                 node->link = prevnode;
  50.                 //              update prevNode with node
  51.                 prevnode = node;
  52.             }
  53.             //          else if top is a left parenthesis
  54.             else if(x.character == '(')
  55.             {  
  56.                 //              create a corresponding GeneralizedListNode
  57.                 //                  with tag=1;atom=0;list=prevNode;link=null
  58.                 GeneralizedListNode *node = (GeneralizedListNode*) malloc(sizeof(GeneralizedListNode));
  59.                 node->tag = 1;
  60.                 // node->dataAtom = 0;
  61.                 node->dataList = prevnode;
  62.                 node->link = NULL;
  63.                 //              update prevNode with node
  64.                 prevnode = node;
  65.                 //              push node to stack S
  66.                 StackElemType y;
  67.                 // y.character = 0;
  68.                 y.generalizedListNode = node;
  69.                 PUSH(&S,y);
  70.                 //              break
  71.                 break;
  72.             }
  73.             //          else if top is a GeneralizedListNode
  74.             else if(x.generalizedListNode != NULL)
  75.             {  
  76.                 //              update value of the link of top with prevNode
  77.                 x.generalizedListNode->link = prevnode;
  78.                 //              update prevNode with value of top
  79.                 prevnode = x.generalizedListNode;
  80.                
  81.             }
  82.             //          pop a stack element from S and store to top
  83.             POP(&S,&x);
  84.         }
  85.         //      if stack is empty
  86.         if(IsEmptyStack(&S))
  87.         {  
  88.             //            return prevNode;
  89.             return prevnode;
  90.         }
  91.     }
  92.   }
  93.  
  94. }
  95.  
  96. void traverse_generalized_list(GeneralizedListNode *root)
  97. {
  98.     Stack S;
  99.     InitStack(&S);
  100.     GeneralizedListNode *t = root;
  101.     if(t == NULL) return;
  102.     while(1)
  103.     {
  104.         if(t == NULL)
  105.         {
  106.             if(!IsEmptyStack(&S))
  107.             {
  108.                 StackElemType stk_elem;
  109.                 POP(&S,&stk_elem);
  110.                 t = stk_elem.generalizedListNode->link;
  111.                 continue;
  112.             }
  113.             else
  114.             {
  115.                 return;
  116.             }
  117.         }
  118.         if(t->tag == 0)
  119.         {
  120.             VISIT(t);
  121.             t = t->link;
  122.         }
  123.         else
  124.         {
  125.             t = t->dataList;
  126.         }
  127.        
  128.     }
  129. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement