0

So I wrote some code for a doubly-linked list and while making a function for adding a node at the end, I thought of making a pointer for the last node, but when I execute it for adding at last it crashes but adding at front end it works fine. Everything looks fine and it does not even show any error but just crashes.

#include <stdio.h>
#include <stdlib.h>
struct node
{
    int data;
    struct node *lptr;
    struct node *rptr;
};
typedef struct node *Node;

Node pos(Node first, Node last)
{
    Node new;
    new = (Node)malloc(sizeof(struct node));
    new->lptr = NULL;
    new->rptr = NULL;
    printf("Enter data: ");
    scanf("%d", &new->data);
    if (first == NULL)
    {
        first = new;
        last = new;
    }
    else
    {
        int p;
        printf("1) First\n2) Last\n");
        scanf("%d", &p);
        switch (p)
        {
        case 1:
            first->lptr = new;
            new->rptr = first;
            first = new;
            break;
        case 2:
            last->rptr = new;
            new->lptr = last;
            last = new;
            break;

        default:
            break;
        }
    }

    return first;
}
void dis(Node first)
{
    Node p;
    int c = 1;
    if (first == NULL)
    {
        printf("Empty");
    }
    else
    {   p=first;
        while (p != NULL)
        {
            printf("%dst element is %d\n", c, p->data);
            c++;
            p = p->rptr;
        }
    }
}
int main()
{
    int ch;
    Node first, last, t;

    first = NULL;
    last = NULL;
    for (;;)
    {
        printf("Insert: \n");
        scanf("%d", &ch);
        switch (ch)
        {

        case 1:
            first = pos(first, last);
            break;
        case 2:
            dis(first);
            break;
        default:
            printf("invalid");
            exit(0);
        }
    }
    return 0;
}

Think the problem is in this part;

case 2:
            last->rptr = new;
            new->lptr = last;
            last = new;
            break;

khoprutu
  • 11
  • 2
  • 2
    Unrelated, hiding pointer types in typedefs when learning them as a language or data structures course is literally the most non-"helpful" thing you can do. All it does is obfuscate the code, which is exactly what you do *not* want, especially now. Regarding your code, look at that while-loop in `dis` and ask yourself what value is held in `p` when the `else` clause is triggered? Hint: there is no defined value. you never initialized `p` to anything, and thus your code invokes *undefined behavior*. I stopped reading after seeing that. Crank up your warnings and treat them as errors. – WhozCraig Dec 15 '21 at 09:30
  • 1
    `show any error but just crashes.` have you tried on valgrind or to check step by step with gdb ? – Ôrel Dec 15 '21 at 09:30
  • Don't know if it is THE problem, but A problem this code has is https://stackoverflow.com/questions/13431108/changing-address-contained-by-pointer-using-function with regards to `first` and `last` in `pos`. You solve it for `first` by returning and assigning the new value, but not for `last`. –  Dec 15 '21 at 09:31

1 Answers1

0

The problem is that the function pos does not change the pointer last declared in main. It changes its local variable (parameter) last that was initialized by a copy of the value pf the pointer last declared in main. But the pointer last declared in main stays unchanged.

You should declared one more structure like for example

struct List
{
    struct node *first;
    struct node *last;
};

and use this structure as a parameter of the functions.

//...

int pos( struct List *list );

int main( void )
{
    struct List list = { .first = NULL, .last = NULL };

    pos( &list );
    //...
}

Also it is much better to split the function into two functions. The first one will add a data to the beginning of the list and the second one will add a data to the tail of the list.

For example

int push_front( struct List *list, int data );
int push_back( struct List *list, int data );
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335