Question: Write an improved version of the code to do the following: Change the linked list to be doubly - linked: each node X has a

Write an improved version of the code to do the following:
Change the linked list to be doubly-linked: each node X has a pointer to the node
after node X and to the node before node X. A pointer root points to the head of
the queue and a pointer end points to the tail of the queue.
Every new line read from the file is stored in a new node which is inserted at the
head of the linked list (as in the original code). Pointer root points to the node with
the last line read so far.
#include
#include
#include
struct node
{
char info[256];
struct node * next;
};
int main (int argc, char * argv[])
{
FILE * fp;
char line[256];
struct node * root;
struct node * new_node;
struct node * p,*q;
if (argc ==1)
{
printf ("Error, no file provided.
") ;
return(0);
}
fp = fopen (argv[1],"r") ;
if (fp == NULL)
{
printf ("Error, file not found.
") ;
return(0);
}
root = NULL;
while (fgets (line,256, fp)!= NULL)
{
new_node = malloc (sizeof (struct node)) ;
strcpy (new_node -> info, line);
new_node -> next = root;
root = new_node;
}
for (p = root; p != NULL; p = p -> next)
{
printf ("%s", p->info) ;
}
for (p = root; p != NULL; p = q)
{
q = p -> next;
free(p);
}
return(0);
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!