显示,在正确位置中删除并删除的问题(链接列表)

发布于 2025-02-03 14:40:54 字数 3329 浏览 4 评论 0原文

在尝试在链接列表中添加和打印时,代码停止之后。当它确实运行时(删除某些功能后)删除SI之间的删除和在定义的位置不在。

#include <stdio.h>
#include <stdlib.h>
struct FlavorList{
    char name[100];
    struct FlavorList* next ; 
};
void addAtfront(struct FlavorList **head){
    struct FlavorList *new_cake = (struct FlavorList*)malloc(sizeof(struct FlavorList));
    printf("First cake: ");
    scanf("%s",new_cake->name);
    new_cake->next = *head ; 
    *head = new_cake ; 
}
void addAtback(struct FlavorList **head){
    struct FlavorList* new_cake = (struct FlavorList*) malloc(sizeof(struct FlavorList)); 
    printf("Second cake : ");
    scanf("%s",new_cake->name);
    new_cake->next = NULL;
    struct FlavorList *last = *head;  
    if (*head == NULL)    {
       *head = new_cake;
       return;    
} 
    while (last->next != NULL)
        last = last->next;
    last->next = new_cake;
    return;   
}
void addInbetween(struct FlavorList  ** head , int pos){
    struct FlavorList * new_cake = (struct FlavorList *) malloc(sizeof(struct FlavorList ));
    printf("Between cake : ");
    scanf("%s",new_cake->name);
    new_cake->next = NULL;
    struct FlavorList  *zig = *head ; 
    int count = 0 ; 
    while(zig!=NULL && count<=pos){
        count ++ ; 
        zig=zig->next ; 
    }
    new_cake->next = zig->next ; 
    zig->next = new_cake ; 
}
void delete(struct FlavorList **head, int pos){
    struct FlavorList*current=*head;
    struct FlavorList*previous=*head;
    if(head ==NULL)    {
        printf("List is empty");
    }
    else if (pos==1)    {
        *head=current->next;
        free(current);
        current=NULL;
    }
    else    {
        while(pos!=1)        {
            previous=current;
            current=current->next;
            pos--;
        }
        previous->next=NULL;
        free(current);
        current=NULL;
    }
}
void display(struct FlavorList  **head){
    struct FlavorList  *zig = * head ;
    while(zig != NULL){
        printf("List of cakes");
        printf("%s",zig->name);
        printf(" ");
        zig=zig->next; 
    }
    printf("\n");
}
int main(){
    struct FlavorList *head = NULL ; 
    printf("--------------------------------\n");
    printf("-----------Cake Shop------------\n");
    printf("--------------------------------\n");
    addAtfront(&head);
    addAtback(&head);
    addInbetween(&head,2);
    display(&head);
    printf("Deleting cake at position 1: \n");
    delete(&head,1);
    printf("Present of now:\n");
    display(&head);
}

我得到的输出。

--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First
Second cake : Second
Between cake : Between


...Program finished with exit code 0
Press ENTER to exit console.

当我添加另一个addatfront和addatback时,我可以到达显示器和删除部分,但它们不在定义的位置。 之后输出:

--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First1
First cake: First2
Second cake : Second1
Second cake : Second2
Between cake : Middle
List of cakes
First2
First1
Second1
Second2
Middle

Deleting cake at position 1: 
Present of now:
List of cakes
First1
Second1
Second2
Middle

While trying to add and print in the linked list the code stops after inbetween. And when it does run (after deleting some function) the deleting and inbetween si not in the defined position.

#include <stdio.h>
#include <stdlib.h>
struct FlavorList{
    char name[100];
    struct FlavorList* next ; 
};
void addAtfront(struct FlavorList **head){
    struct FlavorList *new_cake = (struct FlavorList*)malloc(sizeof(struct FlavorList));
    printf("First cake: ");
    scanf("%s",new_cake->name);
    new_cake->next = *head ; 
    *head = new_cake ; 
}
void addAtback(struct FlavorList **head){
    struct FlavorList* new_cake = (struct FlavorList*) malloc(sizeof(struct FlavorList)); 
    printf("Second cake : ");
    scanf("%s",new_cake->name);
    new_cake->next = NULL;
    struct FlavorList *last = *head;  
    if (*head == NULL)    {
       *head = new_cake;
       return;    
} 
    while (last->next != NULL)
        last = last->next;
    last->next = new_cake;
    return;   
}
void addInbetween(struct FlavorList  ** head , int pos){
    struct FlavorList * new_cake = (struct FlavorList *) malloc(sizeof(struct FlavorList ));
    printf("Between cake : ");
    scanf("%s",new_cake->name);
    new_cake->next = NULL;
    struct FlavorList  *zig = *head ; 
    int count = 0 ; 
    while(zig!=NULL && count<=pos){
        count ++ ; 
        zig=zig->next ; 
    }
    new_cake->next = zig->next ; 
    zig->next = new_cake ; 
}
void delete(struct FlavorList **head, int pos){
    struct FlavorList*current=*head;
    struct FlavorList*previous=*head;
    if(head ==NULL)    {
        printf("List is empty");
    }
    else if (pos==1)    {
        *head=current->next;
        free(current);
        current=NULL;
    }
    else    {
        while(pos!=1)        {
            previous=current;
            current=current->next;
            pos--;
        }
        previous->next=NULL;
        free(current);
        current=NULL;
    }
}
void display(struct FlavorList  **head){
    struct FlavorList  *zig = * head ;
    while(zig != NULL){
        printf("List of cakes");
        printf("%s",zig->name);
        printf(" ");
        zig=zig->next; 
    }
    printf("\n");
}
int main(){
    struct FlavorList *head = NULL ; 
    printf("--------------------------------\n");
    printf("-----------Cake Shop------------\n");
    printf("--------------------------------\n");
    addAtfront(&head);
    addAtback(&head);
    addInbetween(&head,2);
    display(&head);
    printf("Deleting cake at position 1: \n");
    delete(&head,1);
    printf("Present of now:\n");
    display(&head);
}

Output I get.

--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First
Second cake : Second
Between cake : Between


...Program finished with exit code 0
Press ENTER to exit console.

When I add one more of addatfront and addatback I can get to the display and the delete part but they are not in the defined positions.
The output afterwards:

--------------------------------
-----------Cake Shop------------
--------------------------------
First cake: First1
First cake: First2
Second cake : Second1
Second cake : Second2
Between cake : Middle
List of cakes
First2
First1
Second1
Second2
Middle

Deleting cake at position 1: 
Present of now:
List of cakes
First1
Second1
Second2
Middle

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

深府石板幽径 2025-02-10 14:40:54

因此,这是我处理您的代码的方式。
我将完成并解释这个答案,但是要让您可以查看一些内容,我将提供整个代码

max_name_size

此宏将允许您快速更改名称参数的最大允许大小。

更改链接列表:

让我们从更改您的struct的方式开始,我将其转换为typedef,以便更容易进行分配(这样我们就不必键入struct struct每次),我允许将任何尺寸授予名称参数。

Sentinel

现在,更重要的更改将使您更简单地进行代码。这是一个Wiki页面,可以了解它是 sentinel> sentinel 作为头节点,其中包含一个我们永远不想读的值,因此可以作为简单的指针而不是双重指针将头传递!

err.h

我添加了此标头,以允许一种更干净的方法来管理错误异常,例如,何时要删除节点的索引超出范围。

免费链接列表

我已经实现了一个简单的免费功能,一旦您完成了所有需要的操作,它将释放您的链接列表,否则我们将面临内存泄漏!


    #include <stdio.h>
    #include <stdlib.h>
    #include <err.h>
    
    #define MAX_NAME_SIZE 100
    
    typedef struct FlavorList
    {
        char *name;
        struct FlavorList *next;
    }FlavorList;
    
    FlavorList * FlavorListBuildSentinel()
    {
        FlavorList *cake_sentinel = malloc(sizeof(FlavorList));
        cake_sentinel->name = malloc(sizeof(char));
        cake_sentinel->next = NULL;
        return cake_sentinel;
    }
    
    void addAtfront(FlavorList *head)
    {
        printf("Adding a new cake in the front!\n");
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    
    void addAtback(FlavorList *head)
    {
        printf("Adding a new cake in the back!\n");
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        while(head->next!=NULL)
        {
            head = head->next;
        }
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    void addInbetween(FlavorList *head,int pos)
    {
        printf("Adding a new cake at pos: %d!\n",pos);
        while(pos && head->next!=NULL)
        {
            head = head->next;
            pos--;
        }
        //If the pos is still not 0 and the list is out of range, we quit!
        if(pos!=0)
        {
            errx(1, "Index out of range!");
        }
        //If not do the insertion
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    void delete(FlavorList *head, int pos)
    {
        while(pos && head->next!=NULL)
        {
            head = head->next;
            pos--;
        }
        //If the pos is still not 0 and the list is out of range, we quit!
        if(pos!=0 || head->next == NULL)
        {
            errx(1, "Index out of range!");
        }
        FlavorList * tmp = head->next;
        if(head->next->next!=NULL)
        {
            head->next = head->next->next;
        }
        else
        {
            head->next = NULL;
        }
        free(tmp->name);
        free(tmp);
    }
    
    
    void display(FlavorList* head)
    {
       printf("List of cake names\n");
       int cpt = 1;
       while(head->next)
       {
           head = head->next;
           printf("Cake name at position: %d is %s\n",cpt,(head->name));
           cpt++;
       }
       printf("\n");
    }
    
    //Frees the allocated node
    void FlavorListFree(FlavorList* head)
    {
        FlavorList* previous;
        while (head)
        {
            previous = head;
            head = head->next;
            free(previous->name);
            free(previous);
        }
    }
    
    
    int main()
    {
        FlavorList * head = FlavorListBuildSentinel();
    
        printf("--------------------------------\n");
        printf("-----------Cake Shop------------\n");
        printf("--------------------------------\n");
        addAtfront(head);
        addAtback(head);
        addInbetween(head,2);
        display(head);
        printf("Deleting cake at position 1: \n");
        delete(head,1);
        printf("Present of now:\n");
        display(head);
        FlavorListFree(head);
    }

我已经测试了所有功能以及边缘案例和例外,它们工作正常,请随时提出任何问题!
此致,

So here is the way I have approached your code.
I will be completing and explaining this answer but just so that you have something to look at I will give the whole code

MAX_NAME_SIZE

This macro will allow you to quickly change the maximum allowed size for the name parameter.

Changes to the Linked List:

Let's start with the way I have changed your struct, I have converted it to a typedef so it will be easier to do allocations (this way we don't have to type struct each time) and I have allowed any size to be given to the name parameter.

Sentinel

Now the more important change that will allow you to simply the code even further. Here is a wiki page to learn what it is Sentinel, but to put it simply,is serves as the head node, that contains a value that we never want to read and therefore allowing to pass the head as a simple pointer and not a double pointer!

err.h

I have added this header to allow for a more clean way to manage error exceptions, like for example when the index at which you want to delete a node is out of range.

Free the linked list

I have implemented a simple free function that will free your linked list once you have done all the needed operations with it, otherwise we will be facing a memory leak!


    #include <stdio.h>
    #include <stdlib.h>
    #include <err.h>
    
    #define MAX_NAME_SIZE 100
    
    typedef struct FlavorList
    {
        char *name;
        struct FlavorList *next;
    }FlavorList;
    
    FlavorList * FlavorListBuildSentinel()
    {
        FlavorList *cake_sentinel = malloc(sizeof(FlavorList));
        cake_sentinel->name = malloc(sizeof(char));
        cake_sentinel->next = NULL;
        return cake_sentinel;
    }
    
    void addAtfront(FlavorList *head)
    {
        printf("Adding a new cake in the front!\n");
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    
    void addAtback(FlavorList *head)
    {
        printf("Adding a new cake in the back!\n");
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        while(head->next!=NULL)
        {
            head = head->next;
        }
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    void addInbetween(FlavorList *head,int pos)
    {
        printf("Adding a new cake at pos: %d!\n",pos);
        while(pos && head->next!=NULL)
        {
            head = head->next;
            pos--;
        }
        //If the pos is still not 0 and the list is out of range, we quit!
        if(pos!=0)
        {
            errx(1, "Index out of range!");
        }
        //If not do the insertion
        FlavorList *new_cake = malloc(sizeof(FlavorList));
        char * new_name = malloc(sizeof(char)*(MAX_NAME_SIZE+1));
        scanf("%s",new_name);
        new_cake->name = new_name;
        new_cake->next = head->next;
        head->next = new_cake;
    }
    
    void delete(FlavorList *head, int pos)
    {
        while(pos && head->next!=NULL)
        {
            head = head->next;
            pos--;
        }
        //If the pos is still not 0 and the list is out of range, we quit!
        if(pos!=0 || head->next == NULL)
        {
            errx(1, "Index out of range!");
        }
        FlavorList * tmp = head->next;
        if(head->next->next!=NULL)
        {
            head->next = head->next->next;
        }
        else
        {
            head->next = NULL;
        }
        free(tmp->name);
        free(tmp);
    }
    
    
    void display(FlavorList* head)
    {
       printf("List of cake names\n");
       int cpt = 1;
       while(head->next)
       {
           head = head->next;
           printf("Cake name at position: %d is %s\n",cpt,(head->name));
           cpt++;
       }
       printf("\n");
    }
    
    //Frees the allocated node
    void FlavorListFree(FlavorList* head)
    {
        FlavorList* previous;
        while (head)
        {
            previous = head;
            head = head->next;
            free(previous->name);
            free(previous);
        }
    }
    
    
    int main()
    {
        FlavorList * head = FlavorListBuildSentinel();
    
        printf("--------------------------------\n");
        printf("-----------Cake Shop------------\n");
        printf("--------------------------------\n");
        addAtfront(head);
        addAtback(head);
        addInbetween(head,2);
        display(head);
        printf("Deleting cake at position 1: \n");
        delete(head,1);
        printf("Present of now:\n");
        display(head);
        FlavorListFree(head);
    }

I have tested all the functions as well as edge cases and exceptions, and they work just fine, please don't hesitate to ask any questions!
Best Regards,

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文