Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C++
#include <stdio.h>
#include <stdlib.h>

struct node{
int data;
struct node* next;
};
struct node* head; //GLOBAL DECLARATION//
void insert(int x);
void print();
int main()
{    int i,n,x;
    head=NULL; //EMPTY LIST//
    printf("how many number?");
     scanf("%d",&n);
    for(i=0;i<n;i++){>
    printf("ENTER THE NUMBER:\n");
    scanf("%d",&x);
    insert(x);
    print();
    } getch();
}
void insert(int x)
{
   struct node* temp=(struct node*)malloc(sizeof(struct node));
    temp->data=x;
    temp->next=head ;
    head=temp;
   }
void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
	printf("%d",temp->data);
	if(temp->next->next==NULL)
	printf("%d",temp->next->data);
	print(temp->next);
    }
    print("\n");

}


[edit]Code block added - OriginalGriff[/edit]
Posted
Updated 11-Jan-15 1:35am
v2

Um.
Are you sure you want to call print from within itself?
C#
void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    print(temp->next);   <<<<<--- What does this call?
    }
    print("\n");  <<<<<--- What does this call?

}
Try this:
C#
void print()
{
    struct node* temp=head; //TEMP IS LOCAL VARIABLE//
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    printf(temp->next);   
    }
    printf("\n");
}
Try this:
Your print function still doesn't work (it needs a loop) but at least it wonlt give you stack overflow... :laugh:
 
Share this answer
 
Comments
Member 11366679 11-Jan-15 8:15am    
thanks and i have inserted a loop also in my print function.! :D
OriginalGriff 11-Jan-15 8:23am    
Excellent!
You don;'t need to be recursive in your print function (because you don't pass a node through to the function anyway), so a loop is a better solution.
Did it look anything like this?

void print()
{
struct node* temp=head;
printf("LIST IS:\n");
while (temp != NULL)
{
printf("%d\n",temp->data);
temp = temp->next;
}
}
Member 11366679 11-Jan-15 11:59am    
Yes exactly I have also posted solution 😉😃
There are two problems I can see here:
1. Your last print call inside the print() function should have been printf
2. You are running print() in a recursive loop but temp always initialized to head so in ever recursion you start from the beginning and not from the last already printed...
Try this:
C++
int main()
{
    int i,n,x;
    head=NULL; //EMPTY LIST//
    printf("how many number?");
    scanf("%d",&n);
    for(i=0;i<n;i++){>
    printf("ENTER THE NUMBER:\n");
    scanf("%d",&x);
    insert(x);
    print(head); // !!! change here !!!
    } getch();
}

C++
void print(node* root)
{
    struct node* temp = root; //TEMP IS LOCAL VARIABLE// // !!! change here !!!
    printf("LIST IS:");
    if (temp->next!=NULL)
    {
    printf("%d",temp->data);
    if(temp->next->next==NULL)
    printf("%d",temp->next->data);
    print(temp->next);
    }
    printf("\n"); // !!! change here !!!
}
 
Share this answer
 
#include
#include

struct node{
int data;
struct node* next;
};
struct node* head; //GLOBAL DECLARATION//
void insert(int x);
void print();
int main()
{ int i,n,x;
head=NULL; //EMPTY LIST//
printf("how many number?");
scanf("%d",&n);
for(i=0;i
printf("ENTER THE NUMBER:\n");
scanf("%d",&x);
insert(x);
print();
} getch();
}
void insert(int x)
{
struct node* temp=(struct node*)malloc(sizeof(struct node));
temp->data=x;
temp->next=head ;
head=temp;
}
void print()
{
struct node* temp=head; //TEMP IS LOCAL VARIABLE//
printf("LIST IS:");
while(temp!=NULL)
{ printf("%d",temp->data);
temp=temp->next;
}
printf("\n");
}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900