首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用memcmp删除链表的元素

使用memcmp删除链表的元素
EN

Stack Overflow用户
提问于 2011-05-06 15:41:51
回答 1查看 160关注 0票数 0

在这段代码中,我想删除列表中的任何元素,这是由delpos完成的,我使用memcmp来做这件事,我这样做是因为我必须在另一个程序中使用相同的逻辑,在那里我得到一个值,我必须比较链表中存在的值,(我将在那里比较结构,所以只有一个整数)谁能告诉我在delpos中做了什么错误,同时显示它显示了一些垃圾值。

代码语言:javascript
复制
#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);
            }            
        }
    }
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-05-06 15:56:38

您的删除条件似乎不是正确的方式:

代码语言:javascript
复制
     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.
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5908272

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档