c - 堆栈头部和尾部更新为相同的值但仅在 head-C 编程上调用更新

标签 c pointers memory stack

我正在编写一个程序来添加两个整数字符串并将它们压入堆栈以将它们相加。

出于某种原因,当我调用 push 方法时,它只是将数字的最后一位压入堆栈。

#define list_data(l) ((l)->data)

typedef struct ListElmt_{ 
    void *data; //void pointer data because we cant point it to whatever
                //kind of datatype that we want
    struct ListElmt_ *next; //next pointer which points to next element

}ListElmt;

/*
 * Singly-linked list
 */
typedef struct List_{
    int size; //number of elements that we want in the list

    int (*match)(const void *key1, const void *key2);
    void(*destroy)(void *data); //destroy is a pointer to a function that 
                                //takes a void pointer and returns void

    ListElmt *head;
    ListElmt *tail;

}List;

/*
 Public interface

 */

void list_init(List *list, void (*destroy)(void *data));
//a constructor for the list

int list_ins_next(List *list, ListElmt *element, const void *data);
//takes a list parameter, the pointer to element and a constant void parameter
//if return type is 0 then it is success

int list_rem_next(List *list, ListElmt *element, void **data);
//pointer to list, second parameter is the pointer to the next element
//third parameter is a pointer to a pointer allows caller to have a pointer to the pointer for the data that got removed


#define list_size(list)((list)->size)
//creates a function to access the size of the list

#define list_head(list)((list)->head)
//creates a function to access the head pointer of the list

#define list_tail(list)((list)->tail)
//creates a function to access the tail pointer

#define list_is_head(list, element)((element)==(list)->head ? 1 :0)
//returns true or false if the element is the head of the list

#define list_is_tail(element)((element)->next == NULL ? 1:0)
//returns true or false if the element is tail of the list if nextpointer is Null

#define list_is_data(element)((element)->data)
//takes an element pointer and gets the data in the element

#define list_next(element)((element)->next)
//takes an element and gets the next element

列表初始化函数:

    number->size = 0; //initializes size and a destroy function
    number->destroy = destroy;
    number->head = NULL;//initializes head and tail pointer
    number->tail = NULL;

}

typedef List Stack;

#define stack_init list_init

#define stack_destroy list_destroy

int stack_push(Stack *stack, const void *data);

int stack_pop(Stack *stack, void **data);

#define stack_peek(stack)((stack)->head==NULL ? NULL: (stack)->head->data)

#define stack_size list_size

为栈插入函数:


    ListElmt *new_element;
    //allocate storage for the element;
    if(((new_element) = (ListElmt *)malloc(sizeof(ListElmt))) == NULL){
        return -1;
    }

        //insert the element into the list
    new_element->data = (void *)data;
    if(element == NULL){
        /*Handle insertion at the head of the list*/
        if(list_size(stack)==0){
            printf("The list size is 0: \n");//if list is empty then new head is also tail
            stack->tail = new_element; //sets tail as new element
            printf("The tail is this jeint: %d\n",*(int *)stack->tail->data);
            new_element ->next = stack->head;//new element's next pointer is old head
            stack ->head = new_element;
            printf("This is the head of the stack: %d\n",*(int *)list_head(stack)->data);
            printf("This is the tail of the stack: %d\n",*(int *)list_tail(stack)->data);
        }else{
            new_element ->next = stack->head;//new element's next pointer is old head
            stack ->head = new_element;
            printf("This is the head of the stack: %d\n",*(int *)list_head(stack)->data);
            printf("This is the tail of the stack: %d\n",*(int *)list_tail(stack)->data);
        }


        //lists head points to new element
        //lists head and tail are the same, head has a next poitner to null

    }else{
        printf("We made it to the else: ");
        if (element->next == NULL){
            stack->tail = new_element;

        }
        new_element->next = element->next;
        element->next = new_element;

    }

    stack ->size++;
    return 0;

}

列表删除函数:

int list_rem_next(Stack *stack, ListElmt *element, void **data){

    ListElmt *old_element;

    if(stack_size(stack)==0){
        return -1; //checks if the size if 0 because you can't remove anything
    }
    /*remove the element from the list*/
    if(element == NULL){
        //handle removal from the head of list
        *data = stack->head->data;
        old_element = stack->head;
        stack->head = stack->head->next;

        if(stack_size(stack)==1){
            stack->tail = NULL;
        }
    }else{
     //handle removal from somewhere other than the head
        if(element ->next == NULL){
            return -1;

        }
        *data = element->next->data;
        old_element = element ->next;
        element->next = element ->next->next;
        if(element->next == NULL){
            stack->tail = element;
        }

    }
    //Free the storage allocated by the abstract data type
    free(old_element);

    //Adjust the size of the list to account for the removed element
    stack->size --;
    return 0;
}

推送方式

int stack_push(Stack *stack, const void *data){
    return list_ins_next(stack, NULL, data);
}

弹出函数

int stack_pop(Stack *stack, void **data){
    return list_rem_next(stack,NULL,data);
}

获取字符串大小的函数:

int getsize(char *s){
    char *t;
    int size=0;
    for(t=s;*t !='\0';t++){
        size++;
    }
    return size;

}

添加数字的函数:

void addLargeNumbers(char *number1, char *number2){  
    Stack num1;
    Stack num2;
    Stack answer;

    stack_init(&num1,free);
    stack_init(&num2,free);
    stack_init(&answer,free);

    int length1 = getsize(number1);
    int length2 = getsize(number2);

    for(int i=0;i<length1;i++){

        int chartonum = number1[i]-'0';
        int *test1 = &chartonum;
        printf("This is chartonum: %d\n",chartonum);
        printf("This is test1: %d\n",*test1);
        stack_push(&num1,test1);
        printf("Just pushed: %d\n",*test1);

    }

    for(int j=0;j<length2;j++){
        printf("We are about to push %c in the second stack \n",number2[j]);
        int chartonum2 = number2[j]-'0';
        int *test2 = &chartonum2;
        stack_push(&num2,test2);

    }



    int* checking;
    stack_pop(&num1,&checking);
    printf("This is the number we just popped: %d\n",*checking);


    printf("This is the size of stack1 %d\n",num1.size);
    printf("This is the size of stack2 %d\n",num2.size);

    int carry;

    while(num1.size >0 && num2.size >0){
        int *result1, *result2;
        stack_pop(&num1, (void **) &result1);
        stack_pop(&num2, (void **) &result2);

        printf("This is the number we just popped from stack1: %d \n",*result1);
        printf("This is the number we just popped:%d \n",*result2);

        printf("result1: %d\n",*result1);
        printf("result2: %d\n", *result2);
        int sum = *result1 + *result2 + carry;
        printf("sum:%d\n",sum);
        int leastSigDig = sum % 10;

        printf("lsd: %d\n", leastSigDig);

        /* Push the least significant digit of the sum */
        stack_push(&answer, &leastSigDig);

        /* Carry the most significant digit to the next cycle */
        carry = (sum > 9) ? sum / 10 : 0;
        printf("carry: %d\n", carry);
    }

//    printf("Result: %d\n", *(int *)answer.head->data);

    printf("Stack size check: %d\n", answer.size);


    while(answer.size)
    {
        char* elem;
        stack_pop(&answer, (void**) &elem);
        printf("%s", elem);
    }
    /* Probably better to return the answer rather than print... */
}

主要内容:

int main() {
    addLargeNumbers("153","329");



    return (EXIT_SUCCESS);
}

这是输出


This is chartonum: 1
This is test1: 1
The list size is 0: 
The tail is this jeint: 1
This is the head of the stack: 1
This is the tail of the stack: 1
Just pushed: 1
This is chartonum: 5
This is test1: 5
This is the head of the stack: 5
This is the tail of the stack: 5
Just pushed: 5
This is chartonum: 3
This is test1: 3
This is the head of the stack: 3
This is the tail of the stack: 3
Just pushed: 3
We are about to push 3 in the second stack 
The list size is 0: 
The tail is this jeint: 3
This is the head of the stack: 3
This is the tail of the stack: 3
We are about to push 2 in the second stack 
This is the head of the stack: 2
This is the tail of the stack: 2
We are about to push 9 in the second stack 
This is the head of the stack: 9
This is the tail of the stack: 9
This is the number we just popped: 3
This is the size of stack1 2
This is the size of stack2 3
This is the number we just popped from stack1: 3 
This is the number we just popped:9 
result1: 3
result2: 9
sum:12
lsd: 2
The list size is 0: 
The tail is this jeint: 2
This is the head of the stack: 2
This is the tail of the stack: 2
carry: 1
This is the number we just popped from stack1: 3 
This is the number we just popped:9 
result1: 3
result2: 9
sum:13
lsd: 3
This is the head of the stack: 3
This is the tail of the stack: 3
carry: 1
Stack size check: 2

最佳答案

由于这是一个字符串列表,您需要为每个节点的字符串单独存储。

您正在使用 new_element->data = (void *)data; 这告诉我您只是在复制指针。您需要使用 mallocstrdup

为插入函数中的每个字符串分配新内存

关于c - 堆栈头部和尾部更新为相同的值但仅在 head-C 编程上调用更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57386771/

相关文章:

linux - 通过系统调用监控Linux上的内存

java - 当我进行分析时内存的奇怪行为

c - 用于嵌入式系统的简约人类可读序列化格式解析器

c - 重新分配和释放原因 "double free or corruption"

c++ - 指针算术表达式是否算作指令?

android - 在 Android 上终止 Activity 不会清除内存?

c - Linux内核: What kind of C Linux kernel is using?

c - 内存对齐(专门使用 C)

c++ - 为什么 Malloc() 关心边界对齐?

c++ - 指针释放和堆内存,c++