Advertisement
Guest User

Untitled

a guest
Apr 15th, 2019
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. typedef struct node {
  6.     char name[10];
  7.     struct node* next;
  8. } Node;
  9.  
  10. Node *start;
  11. Node *middle;
  12. Node *last;
  13.  
  14. void INSERT(char *src, char *dst, char *add, int method);
  15. void DELETE(char *del);
  16. void REVERSE();
  17. void RENAME(char *oldName, char *newName);
  18. void TOTAL();
  19.  
  20. int main(){
  21.     int N, M;
  22.     char mode[10], src[10], dst[10], add[10], del[10], oldN[10], newN[10];
  23.     int method;
  24.  
  25.     start = (Node*)malloc(sizeof(Node));
  26.     strcpy(start->name,"NTHU");
  27.  
  28.     middle = (Node*)malloc(sizeof(Node));
  29.     strcpy(middle->name,"TSMC");
  30.  
  31.     last = (Node*)malloc(sizeof(Node));
  32.     strcpy(last->name,"NTHU");
  33.  
  34.     start->next = middle;
  35.     middle->next = last;
  36.     last->next = NULL;
  37.  
  38.     scanf("%d", &N);
  39.  
  40.     while(N--) {
  41.         scanf("%s", mode);
  42.         if(strcmp(mode, "INSERT")==0) { //insert mode
  43.             scanf("%s %s %s %d", src, dst, add, &method);
  44.             INSERT(src, dst, add, method);
  45.         }
  46.         else if(strcmp(mode, "DELETE")==0) { //delete mode
  47.             scanf("%s", del);
  48.             DELETE(del);
  49.         }
  50.         else if(strcmp(mode, "REVERSE")==0) {
  51.             REVERSE();
  52.         }
  53.         else if(strcmp(mode, "RENAME")==0) {
  54.             scanf("%s %s", oldN, newN);
  55.             RENAME(oldN, newN);
  56.         }
  57.     }
  58.  
  59.     //TOTAL
  60.  
  61.     Node *cur;
  62.     int totalC=0;
  63.     cur = start;
  64.     while(cur!=last) {
  65.         totalC++;
  66.         cur = cur->next;
  67.     }
  68.     printf("Total %d\n", totalC);
  69.  
  70.  
  71.  
  72.     scanf("%d", &M);
  73.  
  74.  
  75.     int pairDistance[20];
  76.  
  77.     int k=0;
  78.  
  79.  
  80.     while(M--) {
  81.         char srcPair[10], dstPair[10];
  82.         int min = 0;
  83.         int tempDistance = 0;
  84.         int srcDis = 0;
  85.         int dstDis = 0;
  86.         int srcMiss = 1;
  87.         int dstMiss = 1;
  88.         int NTHUcount = 0;
  89.         scanf("%s %s", srcPair, dstPair);
  90.         Node *curSrc, *curDst;
  91.         curSrc = start;
  92.         while(curSrc!=last) {
  93.             tempDistance = 0;
  94.             dstDis = 0;
  95.             if(strcmp(curSrc->name, srcPair)==0) {
  96.                 srcMiss = 0;
  97.                 curDst = curSrc;
  98.                 while(curDst!=NULL) {
  99.                     if(strcmp(curDst->name, dstPair)==0) {
  100.                         dstMiss = 0;
  101.                         if(min==0 || (tempDistance)<min) {
  102.                             min = (tempDistance);
  103.                             if(min==totalC) {
  104.                                 min = 0;
  105.                             }
  106.                             if(strcmp(curSrc->name, curDst->name)==0) {
  107.                                 min = 0;
  108.                             }
  109.                         }
  110.                     }
  111.                     dstDis++;
  112.                     tempDistance++;
  113.                     curDst = curDst->next;
  114.                 }
  115.             }
  116.             srcDis++;
  117.             curSrc = curSrc->next;
  118.         }
  119.  
  120.         if(srcMiss==1) {
  121.             curDst = start;
  122.             while(curDst!=NULL) {
  123.                 if(strcmp(curDst->name, dstPair)==0) {
  124.                     dstMiss = 0;
  125.                 }
  126.                 curDst = curDst->next;
  127.             }
  128.         }
  129.  
  130.  
  131.  
  132.         if(srcMiss==0 && dstMiss==0) {
  133.             printf("%s %s %d\n", srcPair, dstPair, min);
  134.         }
  135.         else if(srcMiss==1 && dstMiss==1) {
  136.  
  137.             printf("%s %s Missing both\n", srcPair, dstPair);
  138.         }
  139.         else if(srcMiss==1 && dstMiss==0) {
  140.  
  141.             printf("%s %s Missing src\n", srcPair, dstPair);
  142.         }
  143.         else if(srcMiss==0 && dstMiss==1) {
  144.  
  145.             printf("%s %s Missing dst\n", srcPair, dstPair);
  146.         }
  147.  
  148.         k++;
  149.  
  150.  
  151.  
  152.     }
  153.  
  154.  
  155.  
  156.  
  157.     /*
  158.     Node *cur;
  159.     Node *np;
  160.     cur = start;
  161.     while(cur->next!=last) {
  162.         printf("%s->", cur->name);
  163.         cur = cur->next;
  164.     }
  165.     printf("%s->%s\n", cur->name, last->name);
  166.     */
  167.  
  168.     //release memory.
  169.     //Node *cur;
  170.     Node *np;
  171.     cur = start;
  172.     while(cur->next!=last){
  173.         np = cur;
  174.         cur = cur->next;
  175.         free(np);
  176.     }
  177.     free(cur);
  178.  
  179.     return 0;
  180. }
  181.  
  182. void INSERT(char *src, char *dst, char *add, int method) {
  183.     Node *add_p, *src_p, *dst_p;
  184.     Node *cur;
  185.     cur = start;
  186.     //printf("start:%s\n", start->name);
  187.     while(cur!=last) { //只要數到倒數第二個
  188.         //printf("cur:%s cur_next:%s\n", cur->name, cur->next->name);
  189.         if(strcmp(cur->name, src)==0) {
  190.             //printf("here1\n");
  191.             if(strcmp(cur->next->name, dst)==0) {
  192.                 //printf("here2\n");
  193.                 add_p = (Node*)malloc(sizeof(Node));
  194.                 strcpy(add_p->name, add);
  195.                 src_p = cur;
  196.                 dst_p = cur->next;
  197.                 src_p->next = add_p;
  198.                 add_p->next = dst_p;
  199.  
  200.                 break;
  201.             }
  202.         }
  203.         cur = cur->next;
  204.         //printf("here3\n");
  205.     }
  206.  
  207.     if(method==2) {
  208.         cur = start;
  209.         while(cur!=last) {
  210.             //printf("cur:%s cur_next:%s\n", cur->name, cur->next->name);
  211.             if(strcmp(cur->name, dst)==0) {
  212.                 //printf("here4\n");
  213.                 if(strcmp(cur->next->name, src)==0) {
  214.                     //printf("here5\n");
  215.                     Node *add_p2;
  216.                     add_p2 = (Node*)malloc(sizeof(Node));
  217.                     strcpy(add_p2->name, add);
  218.                     dst_p = cur;
  219.                     src_p = cur->next;
  220.                     dst_p->next = add_p2;
  221.                     add_p2->next = src_p;
  222.                     break;
  223.                 }
  224.             }
  225.             cur = cur->next;
  226.         }
  227.     }
  228. }
  229.  
  230. void DELETE(char *del) {
  231.  
  232.     Node *cur, *temp, *follow, *another, *temp2, *follow2;
  233.     cur = start;
  234.     temp = start;
  235.  
  236.     //從頭找到第一個被刪除的點
  237.     while(cur!=last) {
  238.         //printf("here1\n");
  239.         if(strcmp(cur->name, del)==0) {
  240.             break;
  241.         }
  242.         cur = cur->next;
  243.     }
  244.     //測試這個刪除的點前面和後面的關係
  245.     while(temp!=last) {
  246.         //printf("here2\n");
  247.         if(strcmp(temp->next->name, cur->name)==0) {
  248.             break;
  249.         }
  250.         temp = temp->next;
  251.     }
  252.     //是中繼點
  253.     if(strcmp(temp->name, cur->next->name)==0) {
  254.         //printf("here3\n");
  255.         follow = cur->next;
  256.         temp->next = follow->next;
  257.         free(follow);
  258.         free(cur);
  259.     }
  260.     //不是中繼點 看有沒有另一個點
  261.     else {
  262.         int flag = 0;
  263.         //printf("here4\n");
  264.         another = cur->next;
  265.         while(another!=last) {
  266.             if(strcmp(another->name, del)==0) {
  267.                 flag = 1;
  268.                 break;
  269.             }
  270.             another = another->next;
  271.         }
  272.         //有兩個要被刪除
  273.         if(flag==1) {
  274.             temp2 = cur->next;
  275.             while(temp2!=last) {
  276.                 if(strcmp(temp2->next->name, another->name)==0) {
  277.                     break;
  278.                 }
  279.                 temp2 = temp2->next;
  280.             }
  281.             follow = cur->next;
  282.             temp->next = follow;
  283.             follow2 = another->next;
  284.             temp2->next = follow2;
  285.             free(cur);
  286.             free(another);
  287.         }
  288.         //只有一個要被刪除
  289.         else if(flag==0) {
  290.             temp->next = cur->next;
  291.             free(cur);
  292.         }
  293.     }
  294.  
  295.     return;
  296. }
  297.  
  298. void REVERSE() {
  299.     Node *prev, *follow, *cur, *lastTemp;
  300.     cur = start->next;
  301.     prev = start;
  302.     lastTemp = last;
  303.  
  304.     while(cur!=lastTemp) {
  305.         if(cur==start->next) {
  306.             last = prev;
  307.         }
  308.         follow = cur->next;
  309.         cur->next = prev;
  310.         prev = cur;
  311.         cur = follow;
  312.     }
  313.     cur->next = prev;
  314.     start = cur;
  315.  
  316.     return;
  317. }
  318.  
  319. void RENAME(char *oldName, char *newName) {
  320.     Node *cur;
  321.     cur = start;
  322.     while(cur!=last) {
  323.         if(strcmp(cur->name, oldName)==0) {
  324.             strcpy(cur->name, newName);
  325.         }
  326.         cur = cur->next;
  327.     }
  328.     return;
  329. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement