简体   繁体   中英

Stack head and tail updating to same value but only calling update on head- C programming

I am writing a program to add two strings of integers and push them onto a stack to add them.

For some reason when I call the push method it is only pushing the very last digit of the number onto the stack.

#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

List Init Function:

    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

Insert Function for the stack:


    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;

}

List remove function:

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;
}

Push method

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

Pop function

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

Function to get size of string:

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

}

Function to add the numbers:

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... */
}

Main:

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



    return (EXIT_SUCCESS);
}

This is the output


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

Since this is a list of strings, you need to have a separate memory for the string per node.

You are using new_element->data = (void *)data; This tells me that you are just copying the pointers. You need to allocate new memory for each string in the insert function by using malloc or strdup

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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