简体   繁体   English

使用memcmp删除链接列表的元素

[英]to delete the element of the linked list by using memcmp

In this code i want to delete any element in the list this is being done by delpos, i am using memcmp to do it, i am doing this as i have to use this same logic in another program where i get a value and i have to compare that value present in the linked list,(there i wll be comparing structure so strating off with just an integer) can any one please tell what mistake i have done in delpos, while displaying it is displaying some junk values. 在这段代码中,我想删除列表中由delpos完成的任何元素,我正在使用memcmp做到这一点,因为我必须在另一个程序中使用相同的逻辑,在这个程序中我得到一个值并且我有比较存在于链表中的那个值,(我将要比较的结构是一个整数),任何人都可以告诉我我在delpos中犯了什么错误,同时显示它正在显示一些垃圾值。

#include<stdio.h>
#include<stdlib.h>

void insertbeg();
void delpos();
void display();

struct node {
    int info;
    struct node *link;
} *first = NULL;

struct node *create();
int item, key;

main() {
    int choice;
    while (1) {
        printf("\nchoices are:\n");
        printf("\n1.Insertbeg\n2.delpos\n3.display\n4.exit\n");
        printf("Enter U'r choice: ");
        scanf("%d", &choice);
        switch (choice) {
            case 1: insertbeg();
                break;
            case 2: delpos();
                break;
            case 3: display();
                break;
            case 4: exit(1);
            default: printf("INVALID CHOICE TRY AGAIN\n");

        }
    }
}

struct node *create() {
    struct node *new;
    new = (struct node*) malloc(sizeof (struct node));
    return (new);
}

void insertbeg() {
    struct node *new;
    new = create();
    printf("Enter element to be inserted: ");
    scanf("%d", &item);
    if (first == NULL) {
        new->info = item;
        new->link = NULL;
        first = new;
    } else {
        new->info = item;
        new->link = first;
        first = new;
    }
}

void delpos() {
    int key;
    struct node *temp, *prev = NULL;
    int cmp_value, cmp_value1;
    if (first == NULL) {
        printf("LIST IS EMPTY\n");
        return;
    } else {
        temp = first;
        printf("Enter the KEY element which is to be deleted: ");
        scanf("%d", &key);
        while (temp->link != NULL) {
            cmp_value = memcmp(&temp->info, &key, 4);
            if (cmp_value == 0) {
                if (prev == NULL)
                    first = temp->link;
                else
                    prev->link = temp->link;
            }
            else {
                prev = temp;
                cmp_value1 = memcmp(&temp->info, &key, 4);
                temp = temp->link;
                free(temp);
            }            
        }
    }
}

It seems that your delete condition is not in the right way : 看来您的删除条件不正确:

     cmp_value = memcmp(&temp->info, &key, sizeof(int)); // sizeof(int) is better than 4
     if (cmp_value == 0)            // here, the items are the same, so you should delete it
     {
        if (prev == NULL)
           first = temp->link;
        else
           prev->link = temp->link;
           // I think you missed a free(temp); here
     }else                          // here, the items are different
     {
        prev = temp;
        cmp_value1 = memcmp(&temp->info, &key, 4);    // why are you comparing again items ??
        temp = temp->link;
        free(temp);                       // why are you freeing the item ?
     }
     // you have to change the value of your temp pointer at the end of the while loop, otherwise, you are not going to correctly check all the items.

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

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