在C中更改struct的成员值

时间:2017-08-03 22:00:24

标签: c pointers

给出以下带有指向LinkedList的指针的函数:

int pop_front(struct LinkedList *linkedList){
    struct LinkedListNode *head = linkedList->head;
    linkedList->head = linkedList->head->next;
    return head->data;
}

我想更新我的LinkedList而不显式返回它。使用以下代码调用上述代码似乎无法正常工作:

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

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

struct LinkedList {
    int size;
    struct LinkedListNode *head;
    struct LinkedListNode *tail;
};

struct LinkedList create_linked_list(){
    struct LinkedList linkedList;

    linkedList.size = 0;
    linkedList.head = NULL;
    linkedList.tail = NULL;

    return linkedList;
}

struct LinkedListNode *malloc_linkedlistnode(){
    struct LinkedListNode *linkedListNode;
    linkedListNode = (struct LinkedListNode*)malloc(sizeof(struct LinkedListNode));
    return linkedListNode;
}

struct LinkedList append(struct LinkedList *linkedList, int data){
    struct LinkedListNode *linkedListNode = malloc_linkedlistnode();
    linkedListNode->data = data;
    linkedList->tail->next = linkedListNode;
    linkedListNode->next = NULL;

    linkedList->tail = linkedListNode;

    if(linkedList->head == NULL){
        linkedList->head = linkedListNode;
    }

    linkedList->size++;
    return *linkedList;
}

struct LinkedList prepend(struct LinkedList *linkedList, int data){
    struct LinkedListNode *linkedListNode = malloc_linkedlistnode();
    linkedListNode->data = data;
    linkedListNode->next = linkedList->head;

    linkedList->head = linkedListNode;

    if(linkedList->tail == NULL){
        linkedList->tail = linkedListNode;
    }

    linkedList->size++;
    return *linkedList;
}

bool empty(struct LinkedList *linkedList){
    return linkedList->size != 0;
}

int value_at(struct LinkedList *linkedList, int index){
    struct LinkedListNode head = *linkedList->head;
    struct LinkedListNode next;

    for(int i = 0; i <= linkedList->size; i++){
        if(i != 0){
            head = *head.next;
        }

        if(i == index){
            return head.data;
        }
    }

    return -1;
}

int pop_front(struct LinkedList *linkedList){
    struct LinkedListNode *head = linkedList->head;

    linkedList->head = linkedList->head->next;

    return head->data;
}

struct LinkedList insert_at(struct LinkedList *linkedList, int index, int data){
    struct LinkedListNode *head = linkedList->head;

    for(int i = 0; i <= linkedList->size; i++){
        if(i+1 == index){
            struct LinkedListNode *linkedListNode = malloc_linkedlistnode();
            linkedListNode->data = data;
            linkedListNode->next = head->next;
            head->next = linkedListNode;
            linkedList->size++;

            return *linkedList;
        }

        head = head->next;
    }

    return *linkedList;
}

int front(struct LinkedList *linkedList){
    return linkedList->head->data;
}

int back(struct LinkedList *linkedList){
    return linkedList->tail->data;
}

int main(){
    struct LinkedList linkedList;
    linkedList = create_linked_list();

    printf("List is %s\n", (linkedList.size ? "not empty" : "empty"));

    for(int i = 0; i <= 10; i++){
        linkedList = prepend(&linkedList, i * 2);
    }

    linkedList = append(&linkedList, 100);

    struct LinkedListNode head = *linkedList.head;
    struct LinkedListNode tail = *linkedList.tail;

    printf("List is %s\n", (linkedList.size ? "not empty" : "empty"));

    linkedList = insert_at(&linkedList, 3, 1000);

    printf("Value at 1: %d\n", value_at(&linkedList, 1));
    printf("Value at 5: %d\n", value_at(&linkedList, 5));
    printf("Value at 3: %d\n", value_at(&linkedList, 3));
    printf("Front: %d\n", front(&linkedList));
    printf("Back: %d\n", back(&linkedList));

    printf("Pop front %d\n", pop_front(&linkedList));

    for(int i = 0; i <= linkedList.size; i++){
        if(head.next == NULL){
            printf("Size of list: %d\n", linkedList.size);
            exit(0);
        }

        if(i != 0){
            head = *head.next;
        }

        printf("Index %d: %d\n", i, head.data);
    }
}

输出:

Pop front 20
Index 0: 20
Index 1: 18
Index 2: 16
Index 3: 1000
Index 4: 14
Index 5: 12
Index 6: 10
Index 7: 8
Index 8: 6
Index 9: 4
Index 10: 2
Index 11: 0
Index 12: 100
Size of list: 13

它确实返回正确的值(正在弹出的项目的值)。

但重新指向头部似乎没有用。

linkedList->head = linkedList->head->next;

1 个答案:

答案 0 :(得分:3)

a = 0 b = 0 c = 0 d = 0 e = 0 f = 0 justTesting = [[a, b], [c, d], [e, f]] for item in justTesting: something = justTesting.index(item) print (something) 函数中的

head不是指向原始列表的指针,它是一个副本:

main()

在此行之后,struct LinkedListNode head = *linkedList.head; head的地址不同的地址

您想要改为使用指针:

*linkedList.head

然后更新此指针将更新您想要更新的列表。

  • 此外,无论遇到此问题,请注意您收到的评论中的所有提示,并避免出现下一个错误。