8 Replies - 1292 Views - Last Post: 30 October 2011 - 10:05 AM Rate Topic: -----

#1 black_stallion   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-October 11

Doubly linked list print error!

Posted 30 October 2011 - 06:42 AM

Here's a very primitive code for a doubly linked list. Although its only very rudimentary, whats wrong with this code? the code's supposed to print the elements sequentially starting from the first node and later print them in reverse.
It prints the elements of the list entered properly. But it doesn't print them in the reverse order as expected from the code starting at line 56. How should I modify it to print them in reverse?(if at all a modification is absolutely necessary)





#include<stdio.h>
#include<conio.h>
 
typedef struct tagnode             
{
        int data;
        struct tagnode *next;
        struct tagnode *prev;
}node;                      
 
node* create(int);
void print(node *);
int main(void)
{
   int n;
   node *head;
    printf("Enter number of nodes to create");
    scanf("%d",&n);
    head=create(n);
    print(head);
   return 0;
}
 
 
node* create(int n)
{
 int data,i;
  
 node *localhead=(node *)malloc(sizeof(node));
 node *movinghead=localhead;
 movinghead->prev=NULL;
 movinghead->next=NULL;
 printf("\nEnter data:");
 scanf("%d",&data);
 movinghead->data=data;
 for(i=1;i<n;i++)
 {
   movinghead->next=(node *)malloc(sizeof(node));
   movinghead->next->prev=movinghead;
   movinghead=movinghead->next;
   scanf("%d",&data);
   movinghead->data=data;
   movinghead->next=NULL;
 }
return localhead;
}
 
void print(node *p)
{
   node *movinghead;
   for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
   {
    printf("\n%d",movinghead->data);
   }
    
   while(movinghead->prev!=NULL)
   {
     printf("%d",movinghead->data);
     movinghead=movinghead->prev;
}
getch();
}


Is This A Good Question/Topic? 0
  • +

Replies To: Doubly linked list print error!

#2 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Doubly linked list print error!

Posted 30 October 2011 - 07:01 AM

One of the first things wrong is that you have not included the header file required to use malloc, stdlib.h.

When I compile your code I get these errors:

Quote

main.c||In function ‘create’:|
main.c|30|error: implicit declaration of function ‘malloc’|
main.c|30|warning: incompatible implicit declaration of built-in function ‘malloc’|
||=== Build finished: 1 errors, 1 warnings ===|


After including this necessary header I get a segmentation fault on line 56 of your code.

Jim
Was This Post Helpful? 0
  • +
  • -

#3 black_stallion   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-October 11

Re: Doubly linked list print error!

Posted 30 October 2011 - 07:41 AM

View Postjimblumberg, on 30 October 2011 - 07:01 AM, said:

One of the first things wrong is that you have not included the header file required to use malloc, stdlib.h.

When I compile your code I get these errors:

Quote

main.c||In function ‘create’:|
main.c|30|error: implicit declaration of function ‘malloc’|
main.c|30|warning: incompatible implicit declaration of built-in function ‘malloc’|
||=== Build finished: 1 errors, 1 warnings ===|


After including this necessary header I get a segmentation fault on line 56 of your code.

Jim


Yeah I don't know how I forgot to include stdlib.h! Still, the program doesn't print the reverse of the entered list.I use Dev-C++ and it just says "dll.c has encountered an error...". What could be the problem?
Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Doubly linked list print error!

Posted 30 October 2011 - 08:02 AM

Maybe the segmentation fault I noted in my last post. Run the program through your debugger it will show you where the program fails.

Jim
Was This Post Helpful? 0
  • +
  • -

#5 black_stallion   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-October 11

Re: Doubly linked list print error!

Posted 30 October 2011 - 08:24 AM

View Postjimblumberg, on 30 October 2011 - 08:02 AM, said:

Maybe the segmentation fault I noted in my last post. Run the program through your debugger it will show you where the program fails.

Jim

Okay will do so and get back to you!
Was This Post Helpful? 0
  • +
  • -

#6 black_stallion   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-October 11

Re: Doubly linked list print error!

Posted 30 October 2011 - 08:34 AM

View Postblack_stallion, on 30 October 2011 - 08:24 AM, said:

View Postjimblumberg, on 30 October 2011 - 08:02 AM, said:

Maybe the segmentation fault I noted in my last post. Run the program through your debugger it will show you where the program fails.

Jim

Yeah i encountered the same segmentation error..But I have no idea what a segmentation error is. Any light that could be shredded on this?

Was This Post Helpful? 0
  • +
  • -

#7 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Doubly linked list print error!

Posted 30 October 2011 - 08:46 AM

In the following snippet:
void print(node *p)
{
   node *movinghead;
   for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
   {
    printf("\n%d",movinghead->data);
   }

   while(movinghead->prev!=NULL)
   {
     printf("%d",movinghead->data);
     movinghead=movinghead->prev;

At some point in the while movinghead is set to NULL, causing a segmentation fault.

Use your debugger to determine when and maybe why.

Jim
Was This Post Helpful? 1
  • +
  • -

#8 black_stallion   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 29-October 11

Re: Doubly linked list print error!

Posted 30 October 2011 - 09:48 AM

View Postjimblumberg, on 30 October 2011 - 08:46 AM, said:

In the following snippet:
void print(node *p)
{
   node *movinghead;
   for(movinghead=p;movinghead!=NULL;movinghead=movinghead->next)
   {
    printf("\n%d",movinghead->data);
   }

   while(movinghead->prev!=NULL)
   {
     printf("%d",movinghead->data);
     movinghead=movinghead->prev;

At some point in the while movinghead is set to NULL, causing a segmentation fault.

Use your debugger to determine when and maybe why.

Jim

Thats what I am saying! I don't know what a segmentation fault means. Is there any remedy is my question!
Was This Post Helpful? 0
  • +
  • -

#9 jimblumberg   User is offline

  • member icon

Reputation: 5916
  • View blog
  • Posts: 17,932
  • Joined: 25-December 09

Re: Doubly linked list print error!

Posted 30 October 2011 - 10:05 AM

You may want to start here.

Jim
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1