what is the problem with this
it is printing only the first node data :( .some one pls help me...

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

struct node
{
        struct node *prev,*next;
        int data;
};


void create_list(struct node *list,int n)
{
        struct node *n1,*n2;
        int i;
        n1=list;
        printf("enter the data for the 1 node:");
        scanf("%d",&list->data);
        for(i=1;i<n;i++)
        {
                list->next=(struct node *)malloc(sizeof(struct node));
                if(list->next==NULL)
                {
                        printf("malloc not done");
                        exit(0);
                }
                n2=list;
                list=list->next;
                list->prev=n2;
                printf("enter the data for the %d node",i+1);
                scanf("%d",&list->data);
        }
        list->next=n1;
        n1->prev=list;
}


void print_list(struct node *list)
{
        struct node *temp;
        temp=list;
        if(list->next=temp)
        {
                printf("%d",list->data);
        }
        if(list->next!=temp)
        {
                printf("%d",list->data);
                if(list->next->next==temp)
                printf("%d",list->next->data);
                else
                print_list(list->next);
        }
}




int main()
{
        int n;
        struct node *head;
        head=(struct node *)malloc(sizeof(struct node));
        if(head==NULL)
        {
                printf("space unallocated");
                exit (0);
        }
        printf("enter the no of nodes:");
        scanf("%d",&n);
        create_list(head,n);
        print_list(head);
return 0;
}

Recommended Answers

All 3 Replies

Member Avatar for Mouche

On line 41, you used = instead of == in your if statement.

if(list->next=temp)

This assigns temp to list->next, so the following if statement will always fail and it only prints out the data of the first list item.

With that fixed, your print_list() function causes a Segmentation Fault when I try it with more than 2 nodes.

Your logic is this:

print_list():
if the next node is the current node, print the data of the current node

if the next node is not the current node, print the data
    AND if the node two ahead in the list is the current node, print its data
        else run print_list() on the next node

For one node, the first if statement (list->next == temp) evaluates to true and it prints out the first node's data, and the second if statement evaluates to false.

For two nodes, the first if statement evaluates to false, so the node's data is not printed there. The second if statement evaluates to true, so the node's data is printed and then the if statement inside that block evaluates to true as well, so the next node's data is printed. The else block is skipped.

Finally, for more than two nodes, the first if statement evaluates to false, the second if statement evaluates to true, and then the nested if statement evaluates to false. The result is that the current node's data is printed, and print_list() is called on the next node. Since print_list() is called on a list with more than two nodes, it does the same thing as described above, which ends with a call to print_list(). You get an infinite number of print_list() calls until the program is killed.

Member Avatar for Mouche

For a circular linked list, I'd recommend using a loop.

Here's an example that doesn't have any safety checks for null list or data values:

void print_list(struct node *list)
{
    struct node* head = list;
    struct node* current = list;
    do  
    {   
        printf("node: %d\n", current->data);
        current = current->next;
    } while (current != head);
}

yes..it works fine now thanks...:)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.