简体   繁体   English

有人可以帮我调试链表上的这段代码吗

[英]Can someone help me in debugging this code on linked list

So, I tried to write a code for handling all basic operations of linked list.所以,我尝试编写一个代码来处理链表的所有基本操作。 But some functions are not working properly and are exiting the code without showing any error.但是某些功能无法正常工作并且正在退出代码而没有显示任何错误。 I have tried to do create, display, search, insert at end, insert at index, delete at data, delete at node.我试图创建、显示、搜索、在末尾插入、在索引处插入、在数据处删除、在节点处删除。 //create #include <stdio.h> #include <stdlib.h> //创建#include <stdio.h> #include <stdlib.h>

struct node
{
    int data;
    struct node *next;
};

void Display(struct node *header)
{
    struct node *p;
    p = header;
    int i = 1;
    while (p->next != NULL)
    {
        printf("Data in Node %d is: %d\n", i++, p->data);
        p = p->next;
    }
}

void search(struct node *ptr, int q,int p)
{
    int i = 1;
    while (ptr->data != q)
    {
        ptr = ptr->next;
        i++;
        if(i==p)
        {
            printf("Invalid");
            break;
        }
    }
    printf("Node %d is %d\n", i, ptr->data);
}

struct node *insert_end(struct node *head, int p,int q)  //error
{
    int i=1;
    struct node *last;
    last->next = NULL;
    last->data = p;
    while(i<q)
    {
        head=head->next;
    }
    head->next = last;
    return last;
    
}

void insert_index(struct node *head, int p,int q, int a)  //error
{
    int i=1;
    struct node *new = (struct node *)malloc(sizeof(struct node));
    if(q>a)
    printf("Invalid");
    else{
    while(i<q-1)
    {
        head=head->next;
        i++;
        printf("!");
    }
    new->data=p;
    new->next=head->next;
    head->next=new;}
    
}

void delete_data(struct node *head,int x)  //error
{
    struct node *ty;
    while(head->data!=x)
    {
        head=head->next;
    }
    ty=head->next;
    head->next=ty->next;
    
}

void delete_node (struct node *head, int index)
{
    int i = 1;
    while (i <= index - 2)
    {
        head = head->next;
        i++;
    }
    struct node *ptr = head->next;
    head->next = ptr->next;
    free(ptr);
}

    int main()
{
    int n,t=1;
    struct node *ptr=(struct node *)malloc(sizeof(struct node));
    struct node *new1 = (struct node *)malloc(sizeof(struct node));
    struct node *start=ptr;
    printf("Enter Data[Input -1 to stop]:");
    while(n!=-1)
    {        
        scanf("%d",&n);
        ptr->data=n;
        if(n!=-1)
        ptr=ptr->next;
        t++;
    }
    ptr->next=NULL;
    int z,r,s;
    pq:
    printf("Enter 1 to Display Elements:\n");
    printf("Enter 2 to Search for a Data:\n");
    printf("Enter 3 to Insert Data at the End:\n");
    printf("Enter 4 to Insert Data at any Index:\n");
    printf("Enter 5 to Delete a data:\n");
    printf("Enter 6 to Delete a node:\n");
    printf("Enter -1 to Exit:");
    scanf("%d",&z);

    switch(z)
    {
        case 1: Display(start); break;  //display
        case 2:
            Display(start);
            printf("Enter data to Search:"); // search for a data
            scanf("%d", &r);
            search(start, r,t);
            goto pq;
        case 3:
            Display(start);
            printf("Enter data:"); // insert at end
            scanf("%d", &r);
            new1=insert_end(start, r,t);
            Display(start);
            goto pq;
        case 4:
            Display(start);
            printf("Enter data:"); // insert with index
            scanf("%d", &r);
            printf("Enter Index:");
            scanf("%d", &s);
            insert_index(start, r, s, t);
            Display(start);
            goto pq;
        case 5:
            Display(start);  //deleting with given data
            printf("Enter data to delete:");
            scanf("%d",&r);
            delete_data(start,r);
            Display(start);
            goto pq;
        case 6: Display(start);  //deleting a node
            printf("Enter Index:");
            scanf("%d", &s);
            delete_node (start, s);
            Display(start);
            goto pq;
        case -1: printf("Exiting");
        default:printf("Invalid");
                break;
    }
        
    return 0;
}

Functions insert_end, insert_index and delete_data are not working.函数 insert_end、insert_index 和 delete_data 不起作用。

insert_end function does not malloc for last: insert_end function 最后没有 malloc:

struct node *insert_end(struct node *head, int p,int q)  //error
{
    int i=1;
    struct node *last;
    last->next = NULL;
    last->data = p;

on insert_index you are setting the new node to set it's next field to the head node, and then also telling the head node to set it's next field to the new node.在 insert_index 上,您正在设置新节点以将其next字段设置为头节点,然后还告诉头节点将其next字段设置为新节点。 effectively pointing to each other.有效地指向对方。

On delete, you are changing the literal value of the head pointer.在删除时,您正在更改头指针的文字值。 See below for another way.另一种方式见下文。 index will work the same as insert_end if you specify an index thats larger than the total size.如果您指定的索引大于总大小,则 index 将与 insert_end 一样工作。

void insert_index(struct node *head, int data, int index)
{
    int i = 1;
    struct node *new = (struct node *)malloc(sizeof(struct node));
    struct node *temp = head;
    while(temp->next)
    {
        if (i == index)
        {
            break;
        }
        i++;
        temp = temp->next;
    }
    new->data = p;
    new->next = NULL;
    if (i == index)
    {
        new->next = temp->next->next;
    }
    temp->next=new;
}

void delete_data(struct node *head, int value)
{
    struct node *temp = head;
    while(temp)
    {
        if(temp->data == value)
        {
            temp->data = 0;
        }
        temp=temp->next;
    }
    
}

Before inserting a node, you need to initialize the node, including allocating memory for the node pointer and initializing the members of the node.在插入节点之前,需要对节点进行初始化,包括为节点指针分配memory和初始化节点的成员。

struct node *insert_end(struct node *head, int p,int q)
{
    int i=1;
    struct node *last;
    last = malloc (sizeof(struct node));
    last->next = NULL;
    last->data = p;
}

The p and q parameters of insert_index are not initialized, and you need to explicitly locate the insertion position according to q. insert_index的p和q参数没有初始化,需要根据q显式定位插入position。 The function delete_data needs to determine whether the head is NULL before comparing the data, and the node memory needs to be released after deleting the node. function delete_data需要在比较数据前判断head是否为NULL,删除节点后需要释放节点memory。

Users in the answers have already pointed out some errors and the ways to fix them.答案中的用户已经指出了一些错误以及修复它们的方法。 But I'm afraid that the author still has much more to fix.但恐怕作者还有很多需要修正的地方。 To find out the reasons why the program doesn't work, I ran the static code analyzer against your code.为了找出程序无法运行的原因,我针对您的代码运行了 static 代码分析器。

struct node *insert_end(struct node *head, int p,int q)  //error
{
    int i=1;
    struct node *last;
    last->next = NULL;
    last->data = p;
    while(i<q)
    {
        head=head->next;
    }

Warning: V776 Potentially infinite loop.警告:V776 潜在的无限循环。 The variable in the loop exit condition 'i < q' does not change its value between iterations.循环退出条件“i < q”中的变量在迭代之间不会改变其值。

The i variable is not changed in the loop, so the loop is executed until the list runs out and the null pointer is dereferenced. i变量在循环中没有改变,所以循环一直执行,直到列表用完并且 null 指针被取消引用。

int main()
{
    int n,t=1;
    struct node *ptr=(struct node *)malloc(sizeof(struct node));
    struct node *new1 = (struct node *)malloc(sizeof(struct node));
    struct node *start=ptr;
    printf("Enter Data[Input -1 to stop]:");
    while(n!=-1)

Warning: V614 Uninitialized variable 'n' used.警告:V614 使用了未初始化的变量“n”。

The n variable is not initialized by anything. n变量没有被任何东西初始化。 The use of uninitialized variables leads to undefined behavior .使用未初始化的变量会导致未定义的行为 Practically, this means that the variable can be equal to anything and we can't predict whether the loop will execute or not.实际上,这意味着变量可以等于任何值,我们无法预测循环是否会执行。

Static code analysis may be a great tool for code studying. Static 代码分析可能是学习代码的好工具。 The analyzer certainly can't replace human assistance, but it also can point out quite many errors.分析仪当然不能代替人工协助,但它也能指出相当多的错误。 And you can immediately detect errors rather than waiting for an answer.您可以立即检测错误,而无需等待答案。 I described this idea in more detail here .我在这里更详细地描述了这个想法。 Good luck and clean code to you!祝你好运,代码干净!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM