-1

I Have a Problem in following code

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


struct linked
{
    int val;
    struct linked *before;
    struct linked *next;
};

void get(struct linked *var);
void printforward(struct linked *var);
void printbackward(struct linked *var);



int main()
{
    struct linked *ptr,*temp;
    ptr=(struct linked*)malloc(sizeof(struct linked));
    if(ptr==NULL)
    {
        printf("NOT ENOUGH MEMOREY");
        exit(2);
    }
    ptr->before=NULL;
    get(ptr);
    printf("\nForward:\n");
    printforward(ptr);
    for(temp=ptr;temp->next;temp=temp->next)
    {
        temp->next->before=(struct linked*)temp;
    }
    printf("\nBackward:\n");
    printbackward(temp->before);
 }


void get(struct linked *var)
 {
    printf("Enter the number (-99) to quit:");
    scanf("%d",&var->val);
    if(var->val==-99)
    {
         var->next=NULL;
         return;
     }
   else
   {
        var->next=(struct linked*)malloc(sizeof(struct linked));
        if(var->next==NULL)
        {
            printf("NOT ENOUGH MEMOREY");
            exit(2);
         }
         get(var->next);
   }
}

void printforward(struct linked *var)
{
    if(var->next==NULL)
   {
       return;
   }
   else
    {
        printf("\n%d",var->val);
        printforward(var->next);
    }
}

 void printbackward(struct linked *var)
 {
     if(var->before==NULL)
     {
         printf("\n%d",var->val);
         return;
     }
     else
     {
         printf("\n%d",var->val);
         printforward(var->before);
     }
    }

output:

 Enter the number (-99) to quit:1
 Enter the number (-99) to quit:2
 Enter the number (-99) to quit:3
 Enter the number (-99) to quit:4
 Enter the number (-99) to quit:5
 Enter the number (-99) to quit:6
 Enter the number (-99) to quit:7
 Enter the number (-99) to quit:8
 Enter the number (-99) to quit:9
 Enter the number (-99) to quit:0
 Enter the number (-99) to quit:-99

Forward:

1
2
3
4
5
6
7
8
9
0
Backward:

0
9
0
Process returned 0 (0x0)   execution time : 22.297 s
Press any key to continue.

Expected output:

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:6
Enter the number (-99) to quit:7
Enter the number (-99) to quit:8
Enter the number (-99) to quit:9
Enter the number (-99) to quit:0
Enter the number (-99) to quit:-99

Forward:

1 
2
3
4
5
6
7
8
9
0
Backward:
0
9
8
7
6
5
4
3
2
1

Let me know What's problem in the code,I am learning linked list in c language,I want to write a code for double way linked list But I a have a logical error in above problem,I did not get clear idea Why The above code did not work,so I am request to clear the doubt.

  • Use printBackward in printBackward... – Antti Haapala -- Слава Україні Nov 17 '19 at 11:10
  • 1
    Also, `var->next->before = (struct linked*)&var->before` is a definite bug. That address-of operator is the reason you're masking the warning/error you were getting with a hard cast that has no place here, or anywhere else in this code if it's proper. There shouldn't be even one single cast in this code, and it should compile cleanly. – WhozCraig Nov 17 '19 at 11:12
  • I removed this line from my code,var->next->before = (struct linked*)&var->before iam getting same error –  Nov 17 '19 at 11:26

2 Answers2

1

Why you're doing this with recursion, I have no idea, but in that spirit, consider this.

  • You're invoking printforward from printbackward, which makes no sense.
  • Your casts are not helping, and in fact, they're masking the real problems. Memory allocations, nor like-pointer or to/from void-pointer casting is required, nor recommended, in C. Read here for why

All four operations can be done recursively, and in fact, you don't even need the "before" pointer but I kept it nonetheless. You can:

  • Build your list
  • Forward-print your list
  • Backward-print your list
  • Cleanup your list

... all using recursion (I leave why you would want to as a different issue).


Code

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

struct linked
{
    int val;
    struct linked *before;
    struct linked *next;
};

struct linked *get(struct linked *before);
void printforward(struct linked const *var);
void printbackward(struct linked const *var);
void cleanup(struct linked *lst);

int main()
{
    struct linked *ptr = get(NULL);

    printf("Forward: ");
    printforward(ptr);
    fputc('\n', stdout);

    printf("Backward: ");
    printbackward(ptr);
    fputc('\n', stdout);

    cleanup(ptr);
}


struct linked *get(struct linked *before)
{
    printf("Enter the number (-99) to quit:");
    int value = -99;
    if (scanf("%d", &value) != 1 || value == -99)
        return NULL;

    struct linked *p = malloc(sizeof *p);
    if (p != NULL)
    {
        p->before = before;
        p->val = value;
        p->next = get(p);
    }
    return p;
}

void printforward(struct linked const *var)
{
    if (var)
    {
        printf("%d ", var->val);
        printforward(var->next);
    }
}

void printbackward(struct linked const *var)
{
    if (var)
    {
        printbackward(var->next);
        printf("%d ", var->val);
    }
}

void cleanup(struct linked *lst)
{
    if (!lst)
        return;

    cleanup(lst->next);
    free(lst);
}

Console

Enter the number (-99) to quit:1
Enter the number (-99) to quit:2
Enter the number (-99) to quit:3
Enter the number (-99) to quit:4
Enter the number (-99) to quit:5
Enter the number (-99) to quit:-99
Forward: 1 2 3 4 5
Backward: 5 4 3 2 1
WhozCraig
  • 65,258
  • 11
  • 75
  • 141
0

here is an example of a simple double linked list construction with basic operation on it, such as addition and insertion of a node, and iteration in both directions. Basically, node is simple structure with two pointers on previous and next node in list, and simple data variable in form of an integer. Key point on operations is proper manipulation of prev-next pointers in a node and head-last pointers in list structure.

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


typedef struct tagNode
{
   int data;
   struct tagNode *next; /* pointer to previous node */
   struct tagNode *prev; /* pointe to next node */
} NODE;

/* head of list */
typedef struct tagList
{
    NODE *head;   /* pointer to first node in list */
    NODE *last;   /* pointer to last node in list */
} LIST;



/* func proto's */
NODE* create_node(int data);
NODE* insert_node(LIST* l,NODE *p);
NODE* add_node(LIST* l, NODE *p);
void print_node(NODE *p);


int main()
{
  LIST list = {NULL,NULL}; /* init list with NULLs */

    /* add some nodes to list */
    for( int i = 0; i < 6 ; i++ )
    {
       add_node(&list, create_node(i*5) );
    }

    /* print forward */
    printf("Forward:\n");
    NODE* p = list.head;
    for( ;p != NULL; p = p->next )
    {
       print_node(p);
    }

    /* print backward */
    printf("Backward\n");
    for(p = list.last;p != NULL; p = p->prev )
    {
       print_node(p);
    }


    /* insert some nodes */
    printf("Insert some nodes\n");
    insert_node(&list, create_node(33));
    insert_node(&list, create_node(23));
    insert_node(&list, create_node(13));

    /* print forward -list after inserts */
    printf("Print forward after insert\n");
    for(p = list.head; p != NULL; p = p->next )
    {
       print_node(p);
    }
    /* print backward */
    printf("print backward after insert\n");
    for( p = list.last;p != NULL; p = p->prev )
    {
       print_node(p);
    }


}

NODE* create_node(int data )
{
  NODE *p = malloc(sizeof(NODE));
  p->next = NULL;
  p->prev = NULL;
  p->data = data;
}

/* add node to end of list */
NODE* add_node(LIST* list, NODE* p)
{
  if(list->last == NULL )
  {
     printf("add first node  into list\n");
     list->last = p;
     list->head = p;
     p->next = NULL;
     p->prev = NULL;
  }
  else
  {
    printf("add num %d\n", p->data);
    list->last->next = p;
    p->prev = list->last;
    list->last = p;
    p->next = NULL; /* terminate list */
  }
  return list->last;
}

void print_node(NODE *p)
{
    printf("node data: %d\n",p->data);
}

/* insert a node into list,
 * position depends on value of data
 **/
NODE* insert_node(LIST* l, NODE *q)
{
    /* scan list ... */
    for( NODE* p = l->head;p != NULL; p = p->next )
    {
       if( p->next != NULL )
       {
          if( p->next->data > q->data )
          {
            q->next = p->next;
            q->prev = p;
            p->next->prev = q;
            p->next = q;
            return q;  /* indicate success */
          }
       }
    }
    return NULL;  /* indicate failure */
}
risbo
  • 188
  • 7