lists in C language: 1) According to the below file, Display the list that\'s bu
ID: 675001 • Letter: L
Question
lists in C language:
1) According to the below file, Display the list that's built(What does the list look like?)
file.C :
int main()
{
int i;
int rV = 0;
List l = NULL;
char* input[] = { "06", "24", "3" };
sNode *s, *t;
for( i=0; i<3; ++i )
{
t = (sNode*)malloc( sizeof( sNode ));
if( t == NULL )
{
fprintf( stderr, "Couldn't get memory for a node! Exiting." );
rV = 1;
break;
}
t->data = input[i];
t->next = l;
l = t;
}
/* What does this list look like here? */
s = l;
while( s != NULL )
{
t = s->next;
free( s );
s = t;
}
return rV;
}
Explanation / Answer
/*C program that allocates the memory for Node and print the list of Node data values and relase the
memory allocated for nodes */
//nodelist.c
#include<stdio.h>
#include<conio.h>
#include<stddef.h>
#include<stdlib.h>
//structure Node
struct Node
{
//data value
int data;
//next Node
Node*next;
};
int main()
{
int i;
int rV = 0;
Node *l = NULL;
char *input[] = { "06", "24", "3" };
//Create two empty List
Node *s=NULL;
Node *t =NULL;
printf("Node list ");
//iterate over three char strings input, input
for( i=0; i<3; ++i )
{
//allocate memory for t using malloc
t = (Node*)malloc( sizeof(Node));
//check if t is NULL then terminate the program
if( t == NULL )
{
fprintf( stderr, "Couldn't get memory for a node! Exiting." );
rV = 1;
break;
}
//Convert string to integer value using atoi fuction
t->data = atoi(input[i]);
//print the value at node,t
printf("%d ",t->data);
//go to next node and store in l
t->next = l;
l = t;
}
/* What does this list look like here? */
/*The Node s takes the address of the node l*/
s = l;
//Iterate over the s node until the s node encountes NULL
while( s != NULL )
{
//get next node and store the node in t
t = s->next;
//release the memory of s node
free( s );
//set node, t to s to move to next node
s = t;
}
//return rV;
getch();
return 0;
}
---------------------------------------------------------------------------------------------------------------------------------------
Sample Output:
Node list
6
24
3
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.