Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- #include <stdlib.h>
- typedef struct node {
- char name[10];
- struct node* next;
- } Node;
- Node *start;
- Node *middle;
- Node *last;
- void INSERT(char *src, char *dst, char *add, int method);
- void DELETE(char *del);
- void REVERSE();
- void RENAME(char *oldName, char *newName);
- void TOTAL();
- int main(){
- int N, M;
- char mode[10], src[10], dst[10], add[10], del[10], oldN[10], newN[10];
- int method;
- start = (Node*)malloc(sizeof(Node));
- strcpy(start->name,"NTHU");
- middle = (Node*)malloc(sizeof(Node));
- strcpy(middle->name,"TSMC");
- last = (Node*)malloc(sizeof(Node));
- strcpy(last->name,"NTHU");
- start->next = middle;
- middle->next = last;
- last->next = NULL;
- scanf("%d", &N);
- while(N--) {
- scanf("%s", mode);
- if(strcmp(mode, "INSERT")==0) { //insert mode
- scanf("%s %s %s %d", src, dst, add, &method);
- INSERT(src, dst, add, method);
- }
- else if(strcmp(mode, "DELETE")==0) { //delete mode
- scanf("%s", del);
- DELETE(del);
- }
- else if(strcmp(mode, "REVERSE")==0) {
- REVERSE();
- }
- else if(strcmp(mode, "RENAME")==0) {
- scanf("%s %s", oldN, newN);
- RENAME(oldN, newN);
- }
- }
- //TOTAL
- Node *cur;
- int totalC=0;
- cur = start;
- while(cur!=last) {
- totalC++;
- cur = cur->next;
- }
- printf("Total %d\n", totalC);
- scanf("%d", &M);
- int pairDistance[20];
- int k=0;
- while(M--) {
- char srcPair[10], dstPair[10];
- int min = 0;
- int tempDistance = 0;
- int srcDis = 0;
- int dstDis = 0;
- int srcMiss = 1;
- int dstMiss = 1;
- int NTHUcount = 0;
- scanf("%s %s", srcPair, dstPair);
- Node *curSrc, *curDst;
- curSrc = start;
- while(curSrc!=last) {
- tempDistance = 0;
- dstDis = 0;
- if(strcmp(curSrc->name, srcPair)==0) {
- srcMiss = 0;
- curDst = curSrc;
- while(curDst!=NULL) {
- if(strcmp(curDst->name, dstPair)==0) {
- dstMiss = 0;
- if(min==0 || (tempDistance)<min) {
- min = (tempDistance);
- if(min==totalC) {
- min = 0;
- }
- if(strcmp(curSrc->name, curDst->name)==0) {
- min = 0;
- }
- }
- }
- dstDis++;
- tempDistance++;
- curDst = curDst->next;
- }
- }
- srcDis++;
- curSrc = curSrc->next;
- }
- if(srcMiss==1) {
- curDst = start;
- while(curDst!=NULL) {
- if(strcmp(curDst->name, dstPair)==0) {
- dstMiss = 0;
- }
- curDst = curDst->next;
- }
- }
- if(srcMiss==0 && dstMiss==0) {
- printf("%s %s %d\n", srcPair, dstPair, min);
- }
- else if(srcMiss==1 && dstMiss==1) {
- printf("%s %s Missing both\n", srcPair, dstPair);
- }
- else if(srcMiss==1 && dstMiss==0) {
- printf("%s %s Missing src\n", srcPair, dstPair);
- }
- else if(srcMiss==0 && dstMiss==1) {
- printf("%s %s Missing dst\n", srcPair, dstPair);
- }
- k++;
- }
- /*
- Node *cur;
- Node *np;
- cur = start;
- while(cur->next!=last) {
- printf("%s->", cur->name);
- cur = cur->next;
- }
- printf("%s->%s\n", cur->name, last->name);
- */
- //release memory.
- //Node *cur;
- Node *np;
- cur = start;
- while(cur->next!=last){
- np = cur;
- cur = cur->next;
- free(np);
- }
- free(cur);
- return 0;
- }
- void INSERT(char *src, char *dst, char *add, int method) {
- Node *add_p, *src_p, *dst_p;
- Node *cur;
- cur = start;
- //printf("start:%s\n", start->name);
- while(cur!=last) { //只要數到倒數第二個
- //printf("cur:%s cur_next:%s\n", cur->name, cur->next->name);
- if(strcmp(cur->name, src)==0) {
- //printf("here1\n");
- if(strcmp(cur->next->name, dst)==0) {
- //printf("here2\n");
- add_p = (Node*)malloc(sizeof(Node));
- strcpy(add_p->name, add);
- src_p = cur;
- dst_p = cur->next;
- src_p->next = add_p;
- add_p->next = dst_p;
- break;
- }
- }
- cur = cur->next;
- //printf("here3\n");
- }
- if(method==2) {
- cur = start;
- while(cur!=last) {
- //printf("cur:%s cur_next:%s\n", cur->name, cur->next->name);
- if(strcmp(cur->name, dst)==0) {
- //printf("here4\n");
- if(strcmp(cur->next->name, src)==0) {
- //printf("here5\n");
- Node *add_p2;
- add_p2 = (Node*)malloc(sizeof(Node));
- strcpy(add_p2->name, add);
- dst_p = cur;
- src_p = cur->next;
- dst_p->next = add_p2;
- add_p2->next = src_p;
- break;
- }
- }
- cur = cur->next;
- }
- }
- }
- void DELETE(char *del) {
- Node *cur, *temp, *follow, *another, *temp2, *follow2;
- cur = start;
- temp = start;
- //從頭找到第一個被刪除的點
- while(cur!=last) {
- //printf("here1\n");
- if(strcmp(cur->name, del)==0) {
- break;
- }
- cur = cur->next;
- }
- //測試這個刪除的點前面和後面的關係
- while(temp!=last) {
- //printf("here2\n");
- if(strcmp(temp->next->name, cur->name)==0) {
- break;
- }
- temp = temp->next;
- }
- //是中繼點
- if(strcmp(temp->name, cur->next->name)==0) {
- //printf("here3\n");
- follow = cur->next;
- temp->next = follow->next;
- free(follow);
- free(cur);
- }
- //不是中繼點 看有沒有另一個點
- else {
- int flag = 0;
- //printf("here4\n");
- another = cur->next;
- while(another!=last) {
- if(strcmp(another->name, del)==0) {
- flag = 1;
- break;
- }
- another = another->next;
- }
- //有兩個要被刪除
- if(flag==1) {
- temp2 = cur->next;
- while(temp2!=last) {
- if(strcmp(temp2->next->name, another->name)==0) {
- break;
- }
- temp2 = temp2->next;
- }
- follow = cur->next;
- temp->next = follow;
- follow2 = another->next;
- temp2->next = follow2;
- free(cur);
- free(another);
- }
- //只有一個要被刪除
- else if(flag==0) {
- temp->next = cur->next;
- free(cur);
- }
- }
- return;
- }
- void REVERSE() {
- Node *prev, *follow, *cur, *lastTemp;
- cur = start->next;
- prev = start;
- lastTemp = last;
- while(cur!=lastTemp) {
- if(cur==start->next) {
- last = prev;
- }
- follow = cur->next;
- cur->next = prev;
- prev = cur;
- cur = follow;
- }
- cur->next = prev;
- start = cur;
- return;
- }
- void RENAME(char *oldName, char *newName) {
- Node *cur;
- cur = start;
- while(cur!=last) {
- if(strcmp(cur->name, oldName)==0) {
- strcpy(cur->name, newName);
- }
- cur = cur->next;
- }
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement