C 删除双链接列表之前节点的未知删除

C 删除双链接列表之前节点的未知删除,c,pointers,doubly-linked-list,recycle-bin,C,Pointers,Doubly Linked List,Recycle Bin,在我的第一次addTrash迭代之后,再到modifyMainList的第一次迭代,它会出现故障,因为我刚要通过modifyMainList从主列表中取出的索引位置中有一个空值。addTrash不应该删除任何内容,所以我认为这一定是指针问题,但我不确定 #include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> struct node { in

在我的第一次addTrash迭代之后,再到modifyMainList的第一次迭代,它会出现故障,因为我刚要通过modifyMainList从主列表中取出的索引位置中有一个空值。addTrash不应该删除任何内容,所以我认为这一定是指针问题,但我不确定

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

struct node {
    int value;
    struct node *next, *previous;
};

struct node * modifyMainList(struct node *mainHead, int link2Delete){

    printf("inside modify list\n\n");
    struct node *curr, *temp;
    temp = NULL;
    curr = mainHead;
    int i;

    for (i = 0; i < link2Delete; i++){

        printf("%d\n", i);
        curr = curr -> next;
    }

    if(curr -> previous == NULL){

        temp = curr;
        curr = curr -> next;

        curr -> previous = NULL;

        temp -> next = NULL;
        free(temp);

        return mainHead;
    }else{

        if((curr -> next == NULL) && (curr -> previous != NULL)){

            temp = curr;
            curr = curr -> previous;
            curr -> next = NULL;

            temp -> previous = NULL;
            free(temp);

            return mainHead;
        }else{

            temp = curr;
            curr = curr -> previous;
            curr -> next = curr -> next -> next;
            curr = temp -> next;
            curr -> previous = curr -> previous -> previous;

            temp -> previous = NULL;
            temp -> next = NULL;
            free(temp);

            return mainHead;            
        }
    }
}

struct node * addTrash(struct node *trashHead, struct node *mainHead, int link2Delete){

    struct node *curr = mainHead, *trashCurr = NULL, *temp = NULL;
    int i = 0;

    for(i = 0; i < link2Delete; i++){

        curr = curr -> next;
    }

    printf("inside addTrash\n\n");
    if(trashHead == NULL){

        trashHead = curr;
        trashHead -> previous = NULL;
        trashHead -> next = NULL;


        return trashHead;

    }else{

        trashCurr = trashHead;
        while(trashCurr -> next != NULL){

            trashCurr = trashCurr -> next;
        }

        trashCurr -> next = curr;
        temp = curr;
        temp -> previous = trashCurr;
        temp -> next = NULL;

        temp = NULL;
        free(temp);

        trashCurr = NULL;
        free(trashCurr);

        return trashHead;

    }
}

//Traverses and prints out data from left to right
void TraverseLeftRight(struct node *head){

    struct node *current;
    current = head;

    while(1){
        if(current != NULL){

            printf("Left to right output:           %d\n", current -> value);
            current = current -> next;
        }else{
            break;
        }           
    }
}


//Traverses and prints out data from right to left
void TraverseRightLeft(struct node *tail){

    struct node *current;


    current = tail;

    while(1){
        if(current != NULL){

            printf("Right to left output:           %d\n", current -> value);
            current = current -> previous;
        }else{
            break;
        }           
    }
}


//inserts a node into the doubly linked linked-list
struct node * insertIntoList(struct node *head, int value){

    int i;
    struct node *current, *temp;

    for(i = 0; i < value; i++){

        //Case 1: List empty
        if (i == 0){

            //create node and assign all pointers and values
            temp = (struct node *) malloc(sizeof(struct node));
            temp -> value = i;
            temp -> next = NULL;
            temp -> previous = NULL;
            head = temp;
            current = head; 
            printf("Input data:             %d\n", current -> value);


        }else{

            //create node and assign pointers and values
            temp = (struct node *) malloc(sizeof(struct node));
            temp -> value = i;
            temp -> next = NULL;

            //assign pointer of previous for temp to the current node
            temp -> previous = current;



            //change current node to the node that was just created
            current -> next = temp;
            current = current -> next;  
            printf("Input data:             %d\n", current -> value);

        }
    }
    printf("\n");
    return head;

}

//frees the data on the doubly linked linked-list

void Free(struct node *head){

    struct node *current, *temp;

    current = head;
    temp = head;    

    while(1){
        if(current != NULL){

            current = current -> next;
            temp -> next = NULL;
            temp -> previous = NULL;
            temp -> value = 0;
            free(temp);
            temp = current;

        }else{
            break;
        }           
    }


}

int main(int argv, char **argc){

    struct node *head, *current, *tail, *temp, *trashHead;
    int input, link2Delete = 0, size = 0, y = 0, number2Delete = 0, i;
    head = NULL;
    trashHead = NULL;
    temp = NULL;
    current = NULL;
    tail = NULL;

    //Check to see if there is the correct amount of arguments
    if(argv < 2){
          printf("************************************************\n");
          printf("* You must include a number for size of list.  *\n");
          printf("************************************************\n");

        //exit program
        return 0;

     }else{
        if(argv > 2){
            printf("*****************************************************************\n");
            printf("* You have entered too many arguments, arguments need to be 2.  *\n");
            printf("*****************************************************************\n");

            //exit program
            return 0;

        }else{

            if(argv == 2){

                //convert string to int
                input = atoi(argc[1]);

                //create the doubly linked linked-list
                head = insertIntoList(head, input); 



                //traverse and print values from left to right order
                TraverseLeftRight(head);

                //traverses the list to create the tail
                current = head;

                while(1){
                    if(current != NULL){
                        temp = current;
                        current = current -> next;

                    }else{
                        break;
                    }           
                }   

                tail = temp;

                printf("\n");               

                //traverse and print values from right to left order
                TraverseRightLeft(tail);

                //Generate the random numbers for the corresponding names to be deleted and the numbers of
                //deletions made
                srand( time(NULL) );
                size = input;
                number2Delete = rand() % size + 1;

                printf("\n\nThis is the random number: %d\n", rand());
                printf("This is the nuber of nodes to be deleted: %d\n", number2Delete);

                for(i = 0; i < number2Delete; i++){

                    y = 0;
                    //Pick a random node for deletion
                    link2Delete = (rand() % size);
                    current = head;
                    while(current != NULL){                 

                        current = current -> next;
                        if(current != NULL){
                            printf("this is node: %d\n", y);
                            y++;
                        }
                    }

                    printf("this is the node to be deleted: %d\n\n", link2Delete);
                    size--;

                    trashHead = addTrash(trashHead, head, link2Delete);
                    printf("this is the head of trash: %d\n\n", trashHead -> value);
                    head = modifyMainList(head, link2Delete);           
                }

                Free(head);
                return 0;
            }
        }
    }
}
#包括
#包括
#包括
#包括
结构节点{
int值;
结构节点*下一个,*上一个;
};
结构节点*modifyMainList(结构节点*mainHead,int link2Delete){
printf(“内部修改列表\n\n”);
结构节点*curr,*temp;
温度=零;
curr=主机头;
int i;
对于(i=0;i下一步;
}
如果(当前->先前==NULL){
温度=电流;
当前=当前->下一步;
curr->previous=NULL;
temp->next=NULL;
免费(临时);
返回主机;
}否则{
如果((当前->下一步==NULL)和(&(当前->上一步!=NULL)){
温度=电流;
当前=当前->上一个;
当前->下一步=空;
temp->previous=NULL;
免费(临时);
返回主机;
}否则{
温度=电流;
当前=当前->上一个;
当前->下一步=当前->下一步->下一步;
当前=临时->下一步;
当前->先前=当前->先前->先前;
temp->previous=NULL;
temp->next=NULL;
免费(临时);
返回主机;
}
}
}
结构节点*addTrash(结构节点*trashHead,结构节点*mainHead,int-link2Delete){
结构节点*curr=mainHead,*trashCurr=NULL,*temp=NULL;
int i=0;
对于(i=0;i下一步;
}
printf(“内部addTrash\n\n”);
if(垃圾头==NULL){
垃圾头=电流;
垃圾头->上一个=空;
垃圾头->下一步=空;
返回垃圾头;
}否则{
垃圾槽=垃圾头;
while(垃圾槽->下一步!=NULL){
垃圾槽=垃圾槽->下一步;
}
垃圾币->下一个=币;
温度=电流;
temp->previous=trashCurr;
temp->next=NULL;
温度=零;
免费(临时);
垃圾币=NULL;
免费(垃圾箱);
返回垃圾头;
}
}
//从左向右遍历并打印数据
void TraverseLeftRight(结构节点*头部){
结构节点*当前;
电流=水头;
而(1){
如果(当前!=NULL){
printf(“从左到右输出:%d\n”,当前->值);
当前=当前->下一步;
}否则{
打破
}           
}
}
//从右向左遍历并打印数据
void TraverseRightLeft(结构节点*尾部){
结构节点*当前;
电流=尾部;
而(1){
如果(当前!=NULL){
printf(“从右到左输出:%d\n”,当前->值);
当前=当前->上一个;
}否则{
打破
}           
}
}
//将节点插入到双链接列表中
结构节点*插入列表(结构节点*头,int值){
int i;
结构节点*当前,*温度;
对于(i=0;i值=i;
temp->next=NULL;
temp->previous=NULL;
压头=温度;
电流=水头;
printf(“输入数据:%d\n”,当前->值);
}否则{
//创建节点并分配指针和值
temp=(结构节点*)malloc(sizeof(结构节点));
温度->值=i;
temp->next=NULL;
//将上一个for temp的指针指定给当前节点
温度->先前=当前;
//将当前节点更改为刚创建的节点
当前->下一步=温度;
当前=当前->下一步;
printf(“输入数据:%d\n”,当前->值);
}
}
printf(“\n”);
回流头;
}
//释放双链接列表上的数据
无空洞(结构节点*头部){
结构节点*当前,*温度;
电流=水头;
温度=水头;
而(1){
如果(当前!=NULL){
当前=当前->下一步;
temp->next=NULL;
temp->previous=NULL;
温度->值=0;
免费(临时);
温度=电流;
}否则{
打破
}           
}
}
int main(int argv,字符**argc){
结构节点*head、*current、*tail、*temp、*trashHead;
int输入,link2Delete=0,size=0,y=0,number2Delete=0,i;
head=NULL;
垃圾头=空;
温度=零;
电流=零;
tail=NULL;
//检查参数数量是否正确
如果(argv<2){
printf(“******************************************************************\n”);
printf(“*您必须包含列表大小的数字。*\n”);
printf(“******************************************************************\n”);
//退出程序
返回0;
}否则{
如果(argv>2){
printf(“************************************************************************************************************************\n”);
printf(“*您输入的参数太多,参数必须为2.*\n”);
printf(“************************************************************************************************************************\n”);
//退出程序
返回0;
}否则{
如果(argv==2){
//将字符串转换为int
输入=atoi(argc[1]);
//创建双链接列表
head=插入列表(head,输入);
//tr
    temp = NULL;
    free(temp);

    trashCurr = NULL;
    free(trashCurr);
    trashCurr = trashHead;