Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

The previous program refers to a C++ linked list program that contains functions

ID: 671062 • Letter: T

Question

The previous program refers to a C++ linked list program that contains functions for Adding nodes, deleting nodes, printing the list, and reversing the list

. List Search Modify the linked list class you created in the previous programming challenges to include a member function named search that returns the position of a specific value in the linked list. The first node in the list is at position 0, the second node is at position 1, and so on. If x is not found on the list, the search should return -1. Test the new member function using an appropriate driver program.

Explanation / Answer

Iterative Method
Iterate trough the linked list. In loop, change next to prev, prev to current and current to next.

Implementation of Iterative Method

#include<stdio.h>

#include<stdlib.h>

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

/* Function to reverse the linked list */

static void reverse(struct node** head_ref)

{

    struct node* prev   = NULL;

    struct node* current = *head_ref;

    struct node* next;

    while (current != NULL)

    {

        next = current->next;

        current->next = prev;  

        prev = current;

        current = next;

    }

    *head_ref = prev;

}

/* Function to push a node */

void push(struct node** head_ref, int new_data)

{

    /* allocate node */

    struct node* new_node =

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

            

    /* put in the data */

    new_node->data = new_data;

                

    /* link the old list off the new node */

    new_node->next = (*head_ref);   

        

    /* move the head to point to the new node */

    (*head_ref)    = new_node;

}

/* Function to print linked list */

void printList(struct node *head)

{

    struct node *temp = head;

    while(temp != NULL)

    {

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

        temp = temp->next;

    }

}   

/* Drier program to test above function*/

int main()

{

    /* Start with the empty list */

    struct node* head = NULL;

   

     push(&head, 20);

     push(&head, 4);

     push(&head, 15);

     push(&head, 85);     

     

     printList(head);   

     reverse(&head);                     

     printf(" Reversed Linked list ");

     printList(head);   

     getchar();

}

#include<stdio.h>

#include<stdlib.h>

/* Link list node */

struct node

{

    int data;

    struct node* next;

};

/* Function to reverse the linked list */

static void reverse(struct node** head_ref)

{

    struct node* prev   = NULL;

    struct node* current = *head_ref;

    struct node* next;

    while (current != NULL)

    {

        next = current->next;

        current->next = prev;  

        prev = current;

        current = next;

    }

    *head_ref = prev;

}

/* Function to push a node */

void push(struct node** head_ref, int new_data)

{

    /* allocate node */

    struct node* new_node =

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

            

    /* put in the data */

    new_node->data = new_data;

                

    /* link the old list off the new node */

    new_node->next = (*head_ref);   

        

    /* move the head to point to the new node */

    (*head_ref)    = new_node;

}

/* Function to print linked list */

void printList(struct node *head)

{

    struct node *temp = head;

    while(temp != NULL)

    {

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

        temp = temp->next;

    }

}   

/* Drier program to test above function*/

int main()

{

    /* Start with the empty list */

    struct node* head = NULL;

   

     push(&head, 20);

     push(&head, 4);

     push(&head, 15);

     push(&head, 85);     

     

     printList(head);   

     reverse(&head);                     

     printf(" Reversed Linked list ");

     printList(head);   

     getchar();

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote