【问题标题】:Doubly Linked List insert function双向链表插入功能
【发布时间】:2017-05-10 13:07:36
【问题描述】:

我的代码有问题。有人可以帮我修复它吗? 问题出在插入函数中,尤其是 malloc(allocating)。我不明白为什么我不能 malloc。它没有打印调试文本。

 #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;


}

【问题讨论】:

    标签: data-structures doubly-linked-list


    【解决方案1】:

    这是工作代码,

        #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,而不是仅长度,因此会导致与指针相关的错误。

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-02-18
    • 1970-01-01
    • 2018-11-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-09-29
    • 2011-07-21
    相关资源
    最近更新 更多