双向链表插入函数

Doubly Linked List insert function

我遇到了这个问题 code.Can 有人帮我解决了吗? 问题出在插入函数中,尤其是 malloc(分配)。我不明白为什么我不能 malloc.It 不打印调试文本。

 #include<stdlib.h>
 #include<stdio.h>
 #define TRUE 1
 #define FALSE 0

struct Node{
    int data;
    struct Node* next;
    struct Node* prev;
};

struct ListRecord
{
 struct Node *head;
 struct Node *tail;
 int length;
}; 

typedef struct ListRecord *DoubleList;

DoubleList createList()
{
    return NULL;
}

DoubleList MakeEmptyList(DoubleList l)
{
    l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
    if(l==NULL)
    {
        printf("Memory allocation failed!");
    }
    else
    {
        l->head->next=NULL;
        l->head->prev=NULL;
        l->tail=l->head;
        l->length=0;
    }
}

DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
{
    struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
    printf("debug");
    if(newNode==NULL)
    {
        printf("debug");
        newNode->data=val;
        newNode->next=NULL;
        newNode->prev=NULL;


        if(pos > l->length+1)
        {
            pos=l->length+1;
        }
        else if(pos==l->length+1)
        {
            struct Node *iterator=l->head;
            while(iterator->next != NULL)
            {
                iterator=iterator->next;
            }
            iterator->next=newNode;
            newNode->prev=iterator;
            newNode->next=NULL;
        }
        else
        {
            struct Node* iterator=l->head;
            int i;
            for(i=0;i<pos;i++)
                iterator=iterator->next;
            newNode->next=iterator->next;
            newNode->prev=iterator;
            iterator->next->prev=newNode;
            iterator->next=newNode; 
        }
        l->length++;

    }
    else
    {
        printf("Memory allocation failed!");
    }
}

int DeleteListAtPosition(DoubleList l,int pos)
{
    int value;
    if(l==NULL)
    {
        printf("List is empty"); 
        return 0;   
    }
    else
    {
        if(pos==1)
        {
            struct Node* iterator=l->head;
            value=iterator->data;
            free(iterator);
            return value;
        }
        else
        {
            struct Node* iterator=l->head;
            int i;
            for(i=1;i<pos;i++)
                iterator=iterator->next;
            if(iterator->next==NULL)
            {
                value=iterator->data;
                free(iterator);
                return value;
            }
            else
            {
                iterator->prev->next=iterator->next;
                iterator->next->prev=iterator->prev;
                free(iterator);             
            }
        }
    }
}

void printList(DoubleList l) {
    struct Node* temp = l->head;
    printf("Forward: ");
    while(temp != NULL) {
        printf("%d ",temp->data);
        temp = temp->next;
    }
    printf("\n");
}

int main(){
    DoubleList myList;
    int exit,pos,value;
    char command;

    myList=createList();
    exit=FALSE;

    while(!exit)
    {
        fflush(stdin);
        printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
        scanf("%c", &command); 
        fflush(stdin);        
        switch(command)  
        {       
        case 'm':
            myList = MakeEmptyList(myList);
            break;
        case 'i':
            printf("Enter position to be added: ");
            scanf("%d",&pos);
            printf("Enter value to be added: ");
            scanf("%d",&value);
            InsertListAtPosition(myList,pos,value);
            //printList(myList);
            break;
        case 'e':
            exit = TRUE;
            break;
        default:
            printf("command not recognized\n");
            break;     
        }
    }
    printf("\n\n");              
    system("PAUSE");    
    return 0;


}

这是工作代码,

    #include<stdlib.h>
    #include<stdio.h>
    #include <bits/stdc++.h> 
     #define TRUE 1
     #define FALSE 0

    using namespace std;

    struct Node{
        int data;
        struct Node* next;
        struct Node* prev;
    };

    struct ListRecord
    {
     struct Node *head;
     struct Node *tail;
     int length;
    }; 

    typedef struct ListRecord *DoubleList;

    DoubleList createList()
    {
        return NULL;
    }

    DoubleList MakeEmptyList()
    {
        DoubleList l;
        l = (struct ListRecord*)malloc(sizeof(struct ListRecord));
        if(l==NULL)
        {
            printf("Memory allocation failed!");
        }
        else
        {
            struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
            newNode->next = NULL;
            newNode->prev = NULL;

            l->head = newNode;
            l->tail=l->head;
            l->length=0;
        }
        return l;
    }

    DoubleList InsertListAtPosition(DoubleList l,int pos,int val)
    {
        struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
        if(newNode != NULL)
        {

            newNode->data=val;
            newNode->next=NULL;
            newNode->prev=NULL;

            if(pos > l->length)
            {
                pos=l->length+1;
            }
            else if(pos==l->length)
            {
                struct Node *iterator=l->head;
                while(iterator->next != NULL)
                {
                    iterator=iterator->next;
                }
                iterator->next=newNode;
                newNode->prev=iterator;
                newNode->next=NULL;
            }
            else
            {
                struct Node* iterator=l->head;
                int i;
                for(i=0;i<pos;i++)
                    iterator=iterator->next;

                newNode->next=iterator->next;
                newNode->prev=iterator;
                iterator->next->prev=newNode;
                iterator->next=newNode; 
            }
            l->length++;

        }
        else
        {
            printf("Memory allocation failed!");
        }
    }

    int DeleteListAtPosition(DoubleList l,int pos)
    {
        int value;
        if(l==NULL)
        {
            printf("List is empty"); 
            return 0;   
        }
        else
        {
            if(pos==1)
            {
                struct Node* iterator=l->head;
                value=iterator->data;
                free(iterator);
                return value;
            }
            else
            {
                struct Node* iterator=l->head;
                int i;
                for(i=1;i<pos;i++)
                    iterator=iterator->next;
                if(iterator->next==NULL)
                {
                    value=iterator->data;
                    free(iterator);
                    return value;
                }
                else
                {
                    iterator->prev->next=iterator->next;
                    iterator->next->prev=iterator->prev;
                    free(iterator);             
                }
            }
        }
    }

    void printList(DoubleList l) {
        struct Node* temp = l->head;
        printf("Forward: ");
        while(temp != NULL) {
            printf("%d ",temp->data);
            temp = temp->next;
        }
        printf("\n");
    }

    int main(){
        DoubleList myList;
        int exit,pos,value;
        char command;

        exit=FALSE;

        while(!exit)
        {
            fflush(stdin);
            printf("\nMenu:\n m)akeEmpty\n i)nsert\n e)xit\n");
            scanf("%c", &command); 
            fflush(stdin);        
            switch(command)  
            {       
            case 'm':
                myList = MakeEmptyList();
                break;
            case 'i':
                printf("Enter position to be added: ");
                scanf("%d",&pos);
                printf("Enter value to be added: ");
                scanf("%d",&value);
                if(myList == NULL) {
                  myList = MakeEmptyList();
                }
                InsertListAtPosition(myList,pos,value);
                //printList(myList);
                break;
            case 'e':
                exit = TRUE;
                break;
            default:
                printf("command not recognized\n");
                break;     
            }
        }
        printf("\n\n");              
        system("PAUSE");    
        return 0;


    }

所以你犯了以下错误,

  1. 您的 MakeEmptyList 方法 return 什么都没有,它应该 return l.
  2. 在您插入的内容中,您正在检查长度 + 1,而不仅仅是长度,因此它导致了与指针相关的错误。