2d_linked_list

 avatar
unknown
c_cpp
a year ago
10 kB
7
Indexable
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

typedef struct Node{
    int value;
    struct Node *next;
}Node;

typedef struct NodeList{
    char name[11];
    struct Node *head;
    struct NodeList *next;
}NodeList;

NodeList* searchList(NodeList *head,char name[]);
void appendList(NodeList *head,char name[]);
void deleteList(NodeList *head,char name[]);
void nameList(NodeList *head,char name[],char newname[]);
void sort(NodeList *head,char name[]);
void merge(NodeList *head,char targetName[],char dataName[]);
void reverse(NodeList *head,char name[]);
void printHead(NodeList *head);

void append(NodeList *head,char name[],int data);
void update(NodeList *head,char name[],int target,int data);
void insert(NodeList *head,char name[],int target,int data);
void deleten(NodeList *head,char name[],int target);

void printNode(NodeList *head,char name[]);

int main(){
    struct NodeList *ListHead = (NodeList *)malloc(sizeof(struct NodeList));
    ListHead->next=NULL;

    char command[1009];
    const char delim[]=" \n\r\t";
    while(fgets(command,1009,stdin)!=NULL){
        
        char *token=strtok(command,delim);

        if(strcmp(token,"appendList")==0){
            token=strtok(NULL,delim);
            char *name=token;
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            appendList(ListHead,name);
        }
        else if(strcmp(token,"deleteList")==0){
            token=strtok(NULL,delim);
            char *name=token;
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            deleteList(ListHead,name);
        }
        else if(strcmp(token,"nameList")==0){
            char *name,*new_name;
            token=strtok(NULL,delim);
            name=token;
            token=strtok(NULL,delim);
            new_name=token;
            if(new_name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            nameList(ListHead,name,new_name);
        }
        else if(strcmp(token,"searchList")==0){
            char *name;
            token=strtok(NULL,delim);
            name=token;
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            searchList(ListHead,name);
        }
        else if(strcmp(token,"sort")==0){
            char *name=strtok(NULL,delim);
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            sort(ListHead,name);
        }
        else if(strcmp(token,"merge")==0){
            char *targetname=strtok(NULL,delim),*dataname=strtok(NULL,delim);
            if(dataname==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            merge(ListHead,targetname,dataname);
        }
        else if(strcmp(token,"reverse")==0){
            char *name=strtok(NULL,delim);
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            reverse(ListHead,name);
        }
        else if(strcmp(token,"append")==0){
            char *name;
            int data;
            token=strtok(NULL,delim);
            name=token;
            char *num=strtok(NULL,delim);
            data=atoi(num);
            if(name==NULL||strtok(NULL,delim)!=NULL){
                printf("Parameter Error\n");
                continue;
            }
            if(num!="0"&&data==0){
                printf("Type Error\n");
                continue;
            }
            //printf("%s %d\n",name,data);
            append(ListHead,name,data);
        }
        else if(strcmp(token,"update")==0){
            char *name,*target,*data;
            name=strtok(NULL,delim);
            target=strtok(NULL,delim);
            data=strtok(NULL,delim);
            if((target!="0"&&atoi(target)==0)||(data!="0"&&atoi(data)==0)){
                printf("Type Error\n");
                continue;
            }
            update(ListHead,name,atoi(target),atoi(data));
        }
        else if(strcmp(token,"insert")==0){
            char *name;
            int target,data;
            name=strtok(NULL,delim);
            target=atoi(strtok(NULL,delim));
            data=atoi(strtok(NULL,delim));
            insert(ListHead,name,target,data);
        }
        else if(strcmp(token,"delete")==0){
            char *name;
            int target;
            name=strtok(NULL,delim);
            target=atoi(strtok(NULL,delim));
            deleten(ListHead,name,target);
        }
        else if(strcmp(token,"printHead")==0){
            printHead(ListHead);
        }
        else if(strcmp(token,"printNode")==0){
            char *name;
            token=strtok(NULL,delim);
            name=token;
            printNode(ListHead,name);
        }
        else{
            printf("Syntax Error\n");
        }
    }
}

NodeList *searchList(NodeList *head,char name[]){
    struct NodeList *pos=head;
    while(pos->next!=NULL){
        //printf("sear : %s\n",pos->next->name);
        if(strcmp(pos->next->name,name)==0)
            break;
        pos=pos->next;
    }
    return pos;
}

void appendList(NodeList *head,char name[]){
    struct NodeList *pos=head;
    while(pos->next!=NULL)
        pos=pos->next;
    struct NodeList *cur = (NodeList *)malloc(sizeof(struct NodeList));
    pos->next=cur;
    cur->head=(Node *)malloc(sizeof(struct Node));
    cur->head->value=0;
    cur->head->next=NULL;
    strcpy(cur->name,name);
    cur->next=NULL;
    printf("appendList_SUCC\n");
}

void deleteList(NodeList *head, char name[]){
    struct NodeList *pos=searchList(head,name);
    pos->next=(pos->next->next);
    printf("deleteList_SUCC\n");    
}

void nameList(NodeList *head,char name[],char newname[]){
    struct NodeList *pos=searchList(head,name);
    strcpy(pos->next->name,newname);
    printf("nameList_SUCC\n");
}

void sort(NodeList *head,char name[]){
    struct NodeList *pos=searchList(head,name);
    struct Node *nhead=pos->next->head;
    struct Node *tail=nhead;
    struct Node *tem=nhead;
    struct Node *cur=nhead;
    struct Node *pre=nhead;
    int len=0;
    while(tail!=NULL){
        len++;
        tail=tail->next;
    }
    printf("seq size : %d\n",len);
    for(int i=len;i>0;i--){
        cur=nhead;
        pre=nhead;
        for(int j=0;j<i-1&&cur->next!=NULL;j++){
            if(cur->value>cur->next->value){
                tem=cur->next;
                cur->next=tem->next;
                tem->next=cur;
                if(cur==nhead){
                    nhead=tem;
                    pre=tem;
                }
                else{
                    pre->next=tem;
                    pre=pre->next;
                }
            }
            else{
                cur=cur->next;
                if(j!=0) pre=pre->next;
            }
        }
    }
    printf("sort_SUCC\n");
}

void merge(NodeList *head,char targetName[],char dataName[]){
    struct NodeList *targetpos=searchList(head,targetName);
    struct NodeList *datapos=searchList(head,dataName);
    struct Node *targetidx=(targetpos->next->head);
    while(targetidx->next!=NULL){
        targetidx=targetidx->next;
    }
    struct Node *dataidx=(datapos->next->head);
    targetidx->next=dataidx->next;
    dataidx->next=NULL;
    printf("merge_SUCC\n");
}

void reverse(NodeList *head,char name[]){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=(pos->next->head);
    struct Node *tem=idx->next;
    idx->next=NULL;
    while(tem!=NULL){
        struct Node *cur=tem;
        tem=tem->next;
        cur->next=idx->next;
        idx->next=cur;
    }
    printf("reverse_SUCC\n");
}
void printHead(NodeList *head){
    struct NodeList *pos=head;
    while(pos!=NULL){
        printf("test : %s\n",pos->name);
        pos=pos->next;
    }
}

void append(NodeList *head,char name[],int data){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=(pos->next->head);
    while(idx->next!=NULL){
        idx=idx->next;
    }
    struct Node *cur = (Node *)malloc(sizeof(struct Node));
    idx->next=cur;
    cur->value=data;
    cur->next=NULL;
    printf("append_SUCC\n");
}

void update(NodeList *head,char name[],int target,int data){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=(pos->next->head);
    while(idx!=NULL){
        if(idx->value==target){
            idx->value=data;
            break;
        }
        idx=idx->next;
    }
    printf("update_SUCC\n");
}

void insert(NodeList *head,char name[],int target,int data){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=(pos->next->head);
    while(idx->next!=NULL){
        if(idx->next->value==target){
            struct Node *cur = (Node *)malloc(sizeof(struct Node));
            cur->next=idx->next;
            cur->value=data;
            idx->next=cur;
            break;
        }
        idx=idx->next;
    }
    printf("insert_SUCC\n");
}

void deleten(NodeList *head,char name[],int target){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=(pos->next->head);
    while(idx->next!=NULL){
        if(idx->next->value==target){
            idx->next=idx->next->next;
            break;
        }
        idx=idx->next;
    }
    printf("delete_SUCC\n");
} 

void printNode(NodeList *head,char name[]){
    struct NodeList *pos=searchList(head,name);
    struct Node *idx=pos->next->head;
    while(idx!=NULL){
        printf("ntest : %d\n",idx->value);
        idx=idx->next;
    }
}
Editor is loading...
Leave a Comment