anonymous No title
No License C
2021年11月15日
Copy Clone
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

 struct CELL{
    struct CELL *prev;
    int value;
    struct CELL *next;
};
struct CELL *insertCellPrev(struct CELL*, int);

struct CELL head;

struct CELL *appendCell(int data){
    struct CELL *p;
    int x=data;
    p=insertCellPrev(&head , x);
    return p;
}
struct CELL *insertCellPrev(struct CELL *p , int data){
    struct CELL *x;
    if(p==NULL){
        printf("insert : 引数が正しくありません\n");
        return NULL;
    }
    x=(struct CELL*)malloc(sizeof(struct CELL));
    if(x==NULL){
        printf("not enough memory\n");
        return NULL;
    }
    x->value=data;
    x->next=p;
    x->prev=p->prev;
    p->prev->next=x;
    p->prev=x;
    return x;
}

void printDlist(struct CELL *q){
    struct CELL *p;
    printf("LIST[");
    for(p=q; p!=&head ; p=p->next){
        printf(" %d ", p->value);
    }
    printf("]\n");

}
int deleteDuplicatedCell(int data){
    struct CELL *p;
    int num=0;
    for(p=head.next ; p!=&head ; p=p->next){
        if(p->value==data){
            num++;
            p->prev->next=p->next;
            p->next->prev=p->prev;
            free(p);
        }
    }
    if(num==0){
        return -1;
    }else{
        return num;
    }
}
void freeDlist(){
    struct CELL *p;
    int x=0;
    p=&head;
    for(p=head.next ; p->prev=&head ; p=p->next){
        printDlist(p);
        p->prev->next=p->next;
        p->next->prev=p->prev;
        free(p);
    }
}
void main(int argc , char *argv[]){
    struct CELL *p;
    int num;
    int n;
    head.prev = head.next = &head;

    for(int i=1 ; i<argc ; i++){
       p=appendCell(atoi(argv[i]));
    }
    printDlist(head.next);

while(1){
    printf("Delete Data : "); scanf("%d", &num);
    if(num<0){
        printf("free list\n");
        freeDlist();
        exit(0);
    }
    n=deleteDuplicatedCell(num);
    if(n!=-1){
        printf("Deleted : %d\n", n);
    }else if(n==-1){
        printf("Not deleted .....\n");
    }
    printDlist(head.next);
}

    return ;
}
#include <stdio.h>
#include <stdlib.h>
#include<string.h>

 struct CELL{
    struct CELL *prev;
    int value;
    struct CELL *next;
};
struct CELL *insertCellPrev(struct CELL*, int);

struct CELL head;

struct CELL *appendCell(int data){
    struct CELL *p;
    int x=data;
    p=insertCellPrev(&head , x);
    return p;
}
struct CELL *insertCellPrev(struct CELL *p , int data){
    struct CELL *x;
    if(p==NULL){
        printf("insert : 引数が正しくありません\n");
        return NULL;
    }
    x=(struct CELL*)malloc(sizeof(struct CELL));
    if(x==NULL){
        printf("not enough memory\n");
        return NULL;
    }
    x->value=data;
    x->next=p;
    x->prev=p->prev;
    p->prev->next=x;
    p->prev=x;
    return x;
}

void printDlist(struct CELL *q){
    struct CELL *p;
    printf("LIST[");
    for(p=q; p!=&head ; p=p->next){
        printf(" %d ", p->value);
    }
    printf("]\n");

}
int deleteDuplicatedCell(int data){
    struct CELL *p;
    int num=0;
    for(p=head.next ; p!=&head ; p=p->next){
        if(p->value==data){
            num++;
            p->prev->next=p->next;
            p->next->prev=p->prev;
            free(p);
        }
    }
    if(num==0){
        return -1;
    }else{
        return num;
    }
}
void freeDlist(){
    struct CELL *p;
    int x=0;
    p=&head;
    for(p=head.next ; p->prev=&head ; p=p->next){
        printDlist(p);
        p->prev->next=p->next;
        p->next->prev=p->prev;
        free(p);
    }
}
void main(int argc , char *argv[]){
    struct CELL *p;
    int num;
    int n;
    head.prev = head.next = &head;

    for(int i=1 ; i<argc ; i++){
       p=appendCell(atoi(argv[i]));
    }
    printDlist(head.next);

while(1){
    printf("Delete Data : "); scanf("%d", &num);
    if(num<0){
        printf("free list\n");
        freeDlist();
        exit(0);
    }
    n=deleteDuplicatedCell(num);
    if(n!=-1){
        printf("Deleted : %d\n", n);
    }else if(n==-1){
        printf("Not deleted .....\n");
    }
    printDlist(head.next);
}

    return ;
}
No one still commented. Please first comment.