双链表插入功能

时间:2017-05-10 13:07:36

标签: data-structures doubly-linked-list

我对此代码有疑问。有人可以帮我解决吗?    问题在于插入功能,尤其是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;


}

1 个答案:

答案 0 :(得分: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方法什么也没有返回,它应该返回l。
  2. 在插入内部,你检查长度为+ 1,而不是仅检查长度,因此导致指针相关错误。