1 solutions

  • 0
    @ 2024-12-10 23:09:26

    C :

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    struct list{
        char studentNo[16];
        struct list *next;
    };
    typedef struct list Sqlist;
    
    int funQuery(Sqlist*, const char*);
    Sqlist* funPrintAll(Sqlist*);
    Sqlist* funInsert(Sqlist*, const char*);
    Sqlist* funDelete(Sqlist*, const char*);
    
    int main()
    {
    	int tmp, i;
    	char command[16];
    	Sqlist *head[128];
    	
    	for (i = 0; i < 128; i++){
    		head[i] = (Sqlist*)malloc(sizeof (Sqlist));
    		head[i]->next = NULL;
    	}
    	
    	while (scanf("%s", command) && strcmp(command, "end")){
    		
    		switch (command[0]){
    			case '+':
    				head[command[1]] = funInsert(head[command[1]], &command[1]);
    				break;
    			case '-':
    				head[command[1]] = funDelete(head[command[1]], &command[1]);
    				break;
    			case '?':
    				tmp = funQuery(head[command[1]], &command[1]);
    				if (tmp){
    					printf("yes\n");
    				}else{
    					printf("no\n");
    				}
    				break;
    		}
    	}
    	
    	for (i = 0; i < 128; i++){
    		head[i] = funPrintAll(head[i]);
    		free(head[i]);
    		head[i] = NULL;
    	}
    	
    	return 0;
    }
    
    Sqlist* funPrintAll(Sqlist* head)
    {
    	Sqlist *q, *p;
    	p = head;
    	q = p->next;
    	while (q != NULL){
    		printf("%s\n", q->studentNo);
    		p = q;
    		q = p->next;
    		free(p);
    	}
    	return head;
    }
    
    int funQuery(Sqlist* head, const char* stuNo)
    {
    	int tmp;
    	Sqlist *q;
    	q = head;
    	while (q = q->next){
    		if (q == NULL){
    			break;
    		}
    		tmp = strcmp(q->studentNo, stuNo);
    		if (!tmp){
    			return 1;
    		}
    		if (tmp > 0){
    			return 0;
    		}
    	}
    	return 0;
    }
    
    Sqlist* funInsert(Sqlist* head, const char* stuNo)
    {
    	int tmp;
    	Sqlist *p, *q, *s;
    	p = head;
    	while (q = p->next){
    		if (q == NULL){
    			break;
    		}
    		tmp = strcmp(q->studentNo, stuNo);
    		if (!tmp){
    			return head;
    		}
    		if (tmp < 0){
    			p = q;
    		}else{
    			break;
    		}
    	}
    	s = (Sqlist*)malloc(sizeof (Sqlist));
    	strcpy(s->studentNo, stuNo);
    	s->next = q;
    	p->next= s;
    	
    	return head;
    }
    
    Sqlist* funDelete(Sqlist* head, const char* stuNo)
    {
    	int tmp;
    	Sqlist *p, *q;
    	p = head;
    	while (q = p->next){
    		if (q == NULL){
    			break;
    		}
    		tmp = strcmp(q->studentNo, stuNo);
    		if (!tmp){
    			p->next = q->next;
    			free(q);
    		}
    		if (tmp < 0){
    			p = q;
    		}else{
    			break;
    		}
    	}
    	
    	return head;
    }
    
    
    • 1

    Information

    ID
    575
    Time
    2000ms
    Memory
    256MiB
    Difficulty
    (None)
    Tags
    (None)
    # Submissions
    0
    Accepted
    0
    Uploaded By