Hi All,
As a beginner in Computer Programming, I decided to create Linked List using recursion, but I am not getting right answer. I think it is fundamentally correct, but I am stuck.
Please help me, - #include "stdio.h"
-
-
struct node
-
{
-
int n;
-
struct node* link;
-
};
-
-
struct node* node1=NULL;
-
struct node* node2=NULL;
-
-
void create();
-
-
main()
-
{
-
char c;
-
printf("Do you want to create a linked list? ");
-
scanf("%c",&c);
-
if(c=='y')
-
create();
-
//Displaying Elements of the Linked List
-
while(node2!=NULL)
-
{
-
printf("%d",node2->n);
-
node2=node2->link;
-
}
-
}
-
-
-
void create()
-
{
-
struct node a;
-
char ch;
-
if(node1==NULL)
-
{
-
printf("\nEnter the Integer Value: ");
-
scanf("%d",&(a.n));
-
a.link=NULL;
-
node1=&a;
-
node2=&a;
-
}
-
else
-
{
-
printf("\nEnter the Integer Value: ");
-
scanf("%d",&(a.n));
-
a.link=NULL;
-
node1->link=&a;
-
node1=node->link;
-
}
-
printf("\nDo you want to enter more elements in linked list? ");
-
scanf("%s",&ch);
-
if(ch=='n')
-
return;
-
else
-
create();
-
}
Thanx
Suyash.Upadhyay
6 8606
in create() -
void create()
-
{
-
struct node a;
-
a is a local variable which is lost on function exit.
make a a pointer and use malloc() to allocate storage, e.g. -
void create()
-
{
-
struct node *a=malloc(sizeof(struct node));
-
char ch;
-
if(node1==NULL)
-
{
-
printf("\nEnter the Integer Value: ");
-
scanf("%d",&(a->n));
-
a->link=NULL;
-
node1=a;
-
node2=a;
-
}
-
... etc etc
-
Thanx,
I got right Output, but can you please clarify one doubt:-
Structure is user defined data type, so when we instatiate it some space in memory should be get allocated for that variable( just like when we declare int a; some space allocates for a), then why should we explicitly use malloc function.
Please help me,
Suyash.Upadhyay
Thanx,
I got right Output, but can you please clarify one doubt:-
Structure is user defined data type, so when we instatiate it some space in memory should be get allocated for that variable( just like when we declare int a; some space allocates for a), then why should we explicitly use malloc function.
Please help me,
Suyash.Upadhyay
there are four scopes in c, in general
block, function, file and program scope. some people refer global scope as program scope.
At the end of scope usually the variable space or memory occupied by variable is freed and returned to operating system. (for this reason usually these variables are stored in stack)
Memory allocated by malloc are from heap and this persist between function calls(unless you free them using free()).
there are four scopes in c, in general
block, function, file and program scope. some people refer global scope as program scope.
At the end of scope usually the variable space or memory occupied by variable is freed and returned to operating system. (for this reason usually these variables are stored in stack)
Memory allocated by malloc are from heap and this persist between function calls(unless you free them using free()).
Thanx but I have still a problem.
In my program node2 is defined in program scope.I have printed node->n immediately after assignment of node2=&a ( where a is an instance of structure)
it gives right result, but when I tried to print node2->n just outside that block it always gives result as 0. What's the problem with it. Please have a glance at my code. - #include "stdio.h"
-
-
struct node
-
{
-
int n;
-
struct node* link;
-
};
-
-
struct node* node1=NULL;
-
struct node* node2=NULL;
-
struct node* node3;
-
-
void create();
-
-
main()
-
{
-
char c;
-
printf("Do you want to create a linked list? ");
-
scanf("%c",&c);
-
if(c=='y')
-
create();
-
}
-
-
-
void create()
-
{
-
struct node a;
-
char ch;
-
if(node1==NULL)
-
{
-
printf("\nEnter the Integer Value: ");
-
scanf("%d",&(a.n));
-
a.link=NULL;
-
node1=&a;
-
node2=&a;
-
printf("\nTesting1 %d",node2->n);
-
}
-
else
-
{
-
printf("\nEnter the Integer Value: ");
-
scanf("%d",&(a.n));
-
a.link=NULL;
-
node1->link=&a;
-
node1=node1->link;
-
printf("\nTesting2 %d",node1->n);
-
}
-
printf("\nDo you want to enter more elements in linked list? ");
-
scanf("%s",&ch);
-
printf("\nTesting2.5 %d",node2->n); //it gives output 0
-
if(ch=='n')
-
return;
-
else
-
{
-
printf("\nTesting3 %d",node2->n);
-
while(node3!=NULL)
-
{
-
printf("\nTesting4 %d",node2->n); // always prints 0
-
printf("\n");
-
node3=node3->link;
-
}
-
create();
-
}
-
}
Please help me.
Suyash.Upadhyay
in the following you read ch which is a char using conversion specification %s which is for a string -
printf("\nDo you want to enter more elements in linked list? ");
-
scanf("%s",&ch);
-
and you are corrupting memory
change the conversion specification to %c to read a char -
printf("\nDo you want to enter more elements in linked list? ");
-
getchar(); // read last newline
-
scanf("%c",&ch);
-
printf("\nTesting2.5 %d",node2->n); //it gives output 0
-
you need the getchar() to remove the newline entered after the last integer value
In my program node2 is defined in program scope.I have printed node->n immediately after assignment of node2=&a ( where a is an instance of structure)
it gives right result, but when I tried to print node2->n just outside that block it always gives result as 0. What's the problem with it.
In the function, you have created a node a with function scope. Then you have node2 point to this temporary node - during the function, node2 is properly defined and will act as planned. But as soon as the function exits, the node created by a is de-allocated. Suddenly, node2 points to de-allocated (a.k.a. useless) memory.
If you are using C++, you should create a node object for node2 using the new operator. If you are using C, you should create a node object using malloc.
Sign in to post your reply or Sign up for a free account.
Similar topics
by: M. Katz |
last post by:
I'm trying to create a heirarchical linked list of sorts, and I'm
looking for advice as to how best to set up the arrays. In other
languages, I might use a pointer array to other pointer arrays,...
|
by: Jeffrey Silverman |
last post by:
Hi, all. I have a linked list. I need an algorithm to create a tree
structure from that list.
Basically, I want to turn this:
$list = array(
array( 'id' => 'A', 'parent_id' => null, 'value'...
|
by: junky_fellow |
last post by:
Consider a singly-linked list. Each node has a structure,
struct node {
char c;
struct node *next;
};
Each of the nodes contain an alphabetic character.
Can anybody suggest a way to print...
|
by: dssuresh6 |
last post by:
Whether browsing forward or backward can be done using a singly linked
list. Is there any specific case where a doubly linked list is needed?
For people who say that singly linked list allows...
|
by: alternativa |
last post by:
Hello,
I'm a beginner in C programming and I have a problem that probably will
seem trivial to most of you, however I can't find a solution...
So, I have to write a data base - program should ask...
|
by: joshc |
last post by:
In an interview for an embedded software position recently I was asked
to write code, in C, for printing the contents of a linked list
backwards. After a few minutes I came up with the recursive...
|
by: tgnelson85 |
last post by:
Hello, C question here (running on Linux, though there should be no platform specific code).
After reading through a few examples, and following one in a book, for linked lists i thought i would...
|
by: DanielJohnson |
last post by:
Hi,
I am not seeking a solution nor am I asking a homework problem. I have
my solution but it doesn't run correctly as expected because of some
error and I am trying to understand this error....
|
by: DolphinDB |
last post by:
Tired of spending countless mintues downsampling your data? Look no further!
In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, we are pleased to welcome back...
|
by: Vimpel783 |
last post by:
Hello!
Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
|
by: jfyes |
last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
|
by: ArrayDB |
last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
|
by: PapaRatzi |
last post by:
Hello,
I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
|
by: CloudSolutions |
last post by:
Introduction:
For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
|
by: Shællîpôpï 09 |
last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
|
by: af34tf |
last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
| |