Answer to Question #256813 in C++ for itwix

Question #256813

Create a program that will accept elements from the user, based on the created

node structure above. The number of elements depends on the no. of items

given by the user. Be able to display all the elements.


1
Expert's answer
2021-10-27T00:48:04-0400
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
struct items
{
    int num;
    struct items *next;
};


struct items *head = NULL;
struct items *current = NULL;


struct items* elements(int num)
{
    printf("\n making a list with headnode as [%d]\n",num);
    struct items *ptr = (struct items*)malloc(sizeof(struct items));
    if(NULL == ptr)
    {
        printf("\n Cannot create the node \n");
        return NULL;
    }
    ptr->num = num;
    ptr->next = NULL;


    head = current = ptr;
    return ptr;
}


struct items* insert(int num, bool bottom)
{
    if(NULL == head)
    {
        return (elements(num));
    }


    if(bottom)
        printf("\n Adding node to end of list with value [%d]\n",num);
    else
        printf("\n Adding node to beginning of list with value [%d]\n",num);


    struct items *ptr = (struct items*)malloc(sizeof(struct items));
    if(NULL == ptr)
    {
        printf("\n Node creation failed \n");
        return NULL;
    }
    ptr->num = num;
    ptr->next = NULL;


    if(bottom)
    {
        current->next = ptr;
        current = ptr;
    }
    else
    {
        ptr->next = head;
        head = ptr;
    }
    return ptr;
}


struct items* search(int num, struct items **prev)
{
    struct items *ptr = head;
    struct items *tmp = NULL;
    bool found = false;


    printf("\n Searching the list for value [%d] \n",num);


    while(ptr != NULL)
    {
        if(ptr->num == num)
        {
            found = true;
            break;
        }
        else
        {
            tmp = ptr;
            ptr = ptr->next;
        }
    }


    if(true == found)
    {
        if(prev)
            *prev = tmp;
        return ptr;
    }
    else
    {
        return NULL;
    }
}


int removeE(int num)
{
    struct items *prev = NULL;
    struct items *delete = NULL;


    printf("\n Deleting value [%d] from list\n",num);


    delete = search(num,&prev);
    if(delete == NULL)
    {
        return -1;
    }
    else
    {
        if(prev != NULL)
            prev->next = delete->next;


        if(delete == current)
        {
            current = prev;
        }
        else if(delete == head)
        {
            head = delete->next;
        }
    }


    free(delete);
    delete = NULL;


    return 0;
}


void display(void)
{
    struct items *ptr = head;


    printf("\nPrinting top of the list \n");
    while(ptr != NULL)
    {
        printf("\n [%d] \n",ptr->num);
        ptr = ptr->next;
    }
    printf("\nPrinting bottom of the list\n");


    return;
}


int main(void)
{
    int i = 0, x = 0;
    struct items *ptr = NULL;


    display();


    for(i = 5; i<10; i++)
        insert(i,true);


    display();


    for(i = 4; i>0; i--)
        insert(i,false);


    display();


    for(i = 1; i<10; i += 4)
    {
        ptr = search(i, NULL);
        if(NULL == ptr)
        {
            printf("\nNo such element found%d\n",i);
        }
        else
        {
            printf("\n Search passed [val = %d]\n",ptr->num);
        }


        display();


        x = removeE(i);
        if(x != 0)
        {
            printf("\n delete [val = %d] failed, element not found\n",i);
        }
        else
        {
            printf("\n delete [val = %d]  passed \n",i);
        }


        display();
    }


    return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS