Answer to Question #295820 in C for erica

Question #295820

create a doubly linked list in c , in which each node stores a string . on inserting a node , node previous to it , current node and next node should be printed . also write a function to delete a specified node . write a function to return node which is n steps forward and n steps backward to current node .


1
Expert's answer
2022-02-11T08:50:57-0500
#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>


struct Node 
{
    char * str;


    struct Node* next;
    struct Node* prev;
};








void set_node(struct Node * node ,const char* str);
void print_node(struct Node* node);
void add_node_after(struct Node* after_node, struct Node* node_to_add);
void add_node_after_(struct Node* after_node, const char * str);
void delete_node(struct Node* node);
struct Node* get_node_n_steps_forward(struct Node* current_node, int n);
struct Node* get_node_n_steps_backward(struct Node* current_node, int n);




int main()
{
    struct Node* head = malloc(sizeof(struct Node));
    head->prev = NULL;


    struct Node* last = malloc(sizeof(struct Node));
    last->next = NULL;


    head->next = last;
    last->prev = head;


       


    set_node(head, "HEAD");
    set_node(last, "LAST");






    for (int i = 0; i < 10; i++)
    {
        char str[10];
        for (int i = 0; i < 9; i++)
        {
            str[i] = (rand() % (90-65)) +64 ;
        }
        str[9] = '\0';


        add_node_after_(head, str);
    }


    int count = 0;
    for (struct Node* current = head; current!=NULL; current = current->next)
    {
        count++;
        printf("%d.String : %s\n",count, current->str);
    }
    
    struct Node* tmp1 = get_node_n_steps_forward(head, 3);
    struct Node* tmp2 = get_node_n_steps_backward(last, 3);


    printf("\n\n3 steps forward from head : %s", tmp1->str);
    printf("\n\n3 steps backward from last : %s\n", tmp2->str);


    delete_node(tmp1);
    delete_node(tmp2);
   


    printf("\nlist after delete \n\n");


     count = 0;
    for (struct Node* current = head; current != NULL; current = current->next)
    {
        count++;
        printf("%d.String : %s\n", count, current->str);
    }


    
    
  


    getc(stdin);
    return 0;
}
void print_node(struct Node* node)
{
    if (node->prev != NULL)
    {
        printf("previous node string : %s\n", node->prev->str);
    }
    else
    {
        printf("previous node is NULL\n");
    }
    printf("current node string : %s\n", node->str);
    if (node->next != NULL)
    {
        printf("next node string : %s\n", node->next->str);
    }
    else
    {
        printf("next node is NULL\n");
    }
   
}
void add_node_after(struct Node* after_node, struct Node* node_to_add)
{
    after_node->next->prev = node_to_add;
    node_to_add->next = after_node->next;
    after_node->next = node_to_add;
    node_to_add->prev = after_node;
    
    
    print_node(after_node);
}
void delete_node(struct Node* node)
{
    free(node->str);
    node->prev->next = node->next;
    node->next->prev = node->prev;
}
struct Node* get_node_n_steps_forward(struct Node* current_node, int n)
{
    struct Node* tmp = current_node;
    for (int i = 0; i < n; i++)
    {
        if (tmp == NULL)
        {
            return NULL;
        }
        tmp = tmp->next;
    }
    return tmp;
}
struct Node* get_node_n_steps_backward(struct Node* current_node, int n)
{
    struct Node* tmp = current_node;
    for (int i = 0; i < n; i++)
    {
        if (tmp == NULL)
        {
            return NULL;
        }
        tmp = tmp->prev;
    }
    return tmp;
}
void set_node(struct Node* node, const char* str)
{
    node->str = (char *)malloc(strlen(str)+1);
    strcpy(node->str, str);
}
void add_node_after_(struct Node* after_node, const char* str)
{
    struct Node *tmp = malloc(sizeof(struct Node));
    set_node(tmp, str);
    add_node_after(after_node, tmp);
}

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