Question

In: Computer Science

/* *fix the below C Program to Display the Nodes of a Linked List in Reverse...

/*
 *fix the below C Program to Display the Nodes of a Linked List in Reverse
 */
 
#include <stdio.h>
#include <stdlib.h>
 
struct node
{
    int visited;
    int a;
    struct node *next;
};

int main()
{
    struct node *head = NULL;
 
    generate(head);
    printf("\nPrinting the list in linear order\n");
    linear(head);
    printf("\nPrinting the list in reverse order\n");
    display(head);
    delete(head);
 
    return 0;
}
 
void display(struct node *head)
{
    struct node *temp = head, *prev = head;
 
    while (temp->visited == 0)
    {
        while (temp->next != NULL && temp->next->visited == 0)
        {
            temp = temp->next;
        }
        printf("%d  ", temp->a);
        temp->visited = 1;
        temp = head;
    }    
}
 
void linear(struct node *head)
{
    while (head != NULL)
    {
        printf("%d  ", head->a);
        head = head->next;
    }
    printf("\n");
}
 
void generate(struct node **head)
{
    int num, i;
    struct node *temp;
 
    printf("Enter length of list: ");
    scanf("%d", num);
    for (i = num; i > 0; i--)
    {
        temp = (struct node *)malloc(sizeof(struct node));
        temp->a = i;
        temp->visited = 0;
        if (head == NULL)
        {
            head = temp;
            head->next = NULL;
        }
        else
        {
            temp->next = head;
            head = temp;
        }
    }
}
 
void delete(struct node **head)
{
    struct node *temp;
    while (head != NULL)
    {
        temp = head;
        head = head->next;
        free(temp);
    }
}

Solutions

Expert Solution

// do comment if any problem arises

//code

#include <stdio.h>

#include <stdlib.h>

struct node

{

    int visited;

    int a;

    struct node *next;

};

void display(struct node *head);

void linear(struct node *head);

void generate(struct node **head);

void Delete(struct node *head);

int main()

{

    struct node *head = NULL;

    generate(&head);

    printf("\nPrinting the list in linear order\n");

    linear(head);

    printf("\nPrinting the list in reverse order\n");

    display(head);

    Delete(head);

    return 0;

}

void display(struct node *head)

{

    struct node *temp = head;

    while (temp->visited == 0)

    {

        while (temp->next != NULL && temp->next->visited == 0)

        {

            temp = temp->next;

        }

        printf("%d  ", temp->a);

        temp->visited = 1;

        temp = head;

    }

}

void linear(struct node *head)

{

    while (head != NULL)

    {

        printf("%d  ", head->a);

        head = head->next;

    }

    printf("\n");

}

void generate(struct node **head)

{

    int num, i;

    struct node *temp;

    printf("Enter length of list: ");

    scanf("%d", &num);

    for (i = num; i > 0; i--)

    {

        temp = (struct node *)malloc(sizeof(struct node));

        temp->a = i;

        temp->visited = 0;

        if (*(head) == NULL)

        {

            *head = temp;

            (*head)->next=NULL;

        }

        else

        {

            temp->next = *head;

            *head = temp;

        }

    }

}

void Delete(struct node *head)

{

    struct node *temp;

    while (head != NULL)

    {

        temp = head;

        head = head->next;

        free(temp);

    }

}

Output:


Related Solutions

In C++, Write a function to reverse the nodes in a linked list. You should not...
In C++, Write a function to reverse the nodes in a linked list. You should not create new nodes when you reverse the the linked list. The function prototype:          void reverse(Node*& head); Use the following Node definition: struct Node {    int data;    Node *next; }
I need a MIPS Assembly program that "Display the elements of the linked list in reverse...
I need a MIPS Assembly program that "Display the elements of the linked list in reverse order." It needs subprogram and those subprogram does not have t registers.
1.Please write a C++ program that counts the nodes in a linked list with the first...
1.Please write a C++ program that counts the nodes in a linked list with the first node pointed to by first. Also please explain. 2. Write a program to determine the average of a linked list of real numbers with the first node pointed to by first. 3. Determine the computing times of the algorithms in question 1 and 4. Write a program to insert a new node into a linked list with the first node pointed to by first...
Write down a C program which will create a list (simple linear linked list) of nodes....
Write down a C program which will create a list (simple linear linked list) of nodes. Each node consists of two fields. The first field is a pointer to a structure that contains a student id (integer) and a grade-point average (float). The second field is a link. The data are to be read from a text file. Your program should read a file of 10 students (with student id and grade point average) and test the function you wrote...
Write a Java program to implement a double-linked list with addition of new nodes at the...
Write a Java program to implement a double-linked list with addition of new nodes at the end of the list. Add hard coded nodes 10, 20, 30, 40 and 50 in the program. Print the nodes of the doubly linked list.
how to do circular linked list in c++ (inset and delet and display)
how to do circular linked list in c++ (inset and delet and display)
C++: Write a reverse function that receives a reference to a integer linked list and reverses...
C++: Write a reverse function that receives a reference to a integer linked list and reverses the order of all the elements in it. For example, if the input linked list is 1 -> 4-> 2-> 3-> 6-> 5}, after processing by this function, the linked list should become 5-> 6-> 3-> 2-> 4-> 1. You need to write a main file to insert elements into the linked list and call the reverseLinkedList() function which takes the reference of first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first...
Write a subroutine named swap in C thatswaps two nodes in a linked list. The first node should not be able to change places. The nodes are given by: Struct nodeEl { int el; struct nodeEl * next; }; typdef struct nodeEl node; The list header (of type node *) is the first parameter of the subroutine. The second and third parameters consist of integers and are the places in the list where the nodes are to change places. The...
Given a linked list of integers, remove any nodes from the linked list that have values...
Given a linked list of integers, remove any nodes from the linked list that have values that have previously occurred in the linked list. Your function should return a reference to the head of the updated linked list. (In Python)
Create a C++ integer linked list program that performs the following methods below: Please create these...
Create a C++ integer linked list program that performs the following methods below: Please create these three source files: intList.h, intList.cpp, & intListTest.cpp. Implement recursive routines in the intList class to do the following: Print the list in reverse order Return the value in the middle node Return the average of all the odd values Remove every node containing an odd value Modify main (in IntListTest.cpp) so it does the following: Insert the numbers 1, 3, 4, 6, 7, 10,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT