Question

it is c programming problem. help me to do this problem 1. write a insertion function...

it is c programming problem. help me to do this problem

1. write a insertion function and deletion function for a double-linked list

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Here is code:

#include <stdio.h>

#include <stdlib.h>

struct Node

{

int data;

struct Node *next;

struct Node *prev;

};

void deleteNode(struct Node **head_ref, struct Node *del)

{

if (*head_ref == NULL || del == NULL)

return;

if (*head_ref == del)

*head_ref = del->next;

// if its not last element

if (del->next != NULL)

del->next->prev = del->prev;

// if its not first element

if (del->prev != NULL)

del->prev->next = del->next;

free(del);

return;

}

void insertNode(struct Node **head_ref, int new_data)

{

// create new node

struct Node *new_node = (struct Node *)malloc(sizeof(struct Node));

// adding data

new_node->data = new_data;

new_node->prev = NULL;

new_node->next = (*head_ref);

//if not the first node

if ((*head_ref) != NULL)

(*head_ref)->prev = new_node;

// add to new node

(*head_ref) = new_node;

}

// print node

void printList(struct Node *node)

{

while (node != NULL)

{

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

node = node->next;

}

}

int main()

{

struct Node *head = NULL;

insertNode(&head, 2);

insertNode(&head, 6);

insertNode(&head, 9);

insertNode(&head, 10);

printf("\n Linked list \t\t\t: ");

printList(head);

deleteNode(&head, head); // delete 10

deleteNode(&head, head->next->next); // delete 2

printf("\n After removed Linked list \t: ");

printList(head);

}

Output:

Add a comment
Know the answer?
Add Answer to:
it is c programming problem. help me to do this problem 1. write a insertion function...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Would you please help me to do this problem? this is c programming. 1. write figures of inserting...

    Would you please help me to do this problem? this is c programming. 1. write figures of inserting 3 elements into a stack linked list and a queue linked list with code fragments and then delete 2 elements from each with associated code fragments. thank you.

  • This is a c programming problem. Would you please help me to write this problem??? I...

    This is a c programming problem. Would you please help me to write this problem??? I really appreciate it if you add comments for explanation step by step. Thank you. Reverse a Doubly linked list using recursion: Given a doubly linked list. Reverse it using recursion. Original Doubly linked list: next pointer - DDHIHI Null prev painter Reversed Doubly linked list: next pointer Start Pointer Null prev pointer Include: a) A struct for a node of the doubly linked list....

  • Could anyone help me solve this problem in C++? Write a function that sorts a linked...

    Could anyone help me solve this problem in C++? Write a function that sorts a linked list. (Use any sort algorithm, make sure to state which one your going to use.)

  • Write a function to implement linked list consisting of five nodes. Store the data in the...

    Write a function to implement linked list consisting of five nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming George, Paul, Ross, Joe, Elaine, Robert1 Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes in the following order. (You have...

  • Write a function to implement linked list consisting of six nodes. Store the data in the...

    Write a function to implement linked list consisting of six nodes. Store the data in the nodes in the order given below. Also, write a function to display the data stored in the implemented linked list. in C++ programming use these as names as nodes George, Paul, Ross, Joe, Elaine, Robert1. Insert three nodes in between second node and third node in the linked list that you implemented in problem 1. Store the following data in the new (inserted) nodes...

  • C programming Write an iterative and recursive version of a function to print out a linked...

    C programming Write an iterative and recursive version of a function to print out a linked list from head to tail and then do the same for printing a linked list from tail to head. Assume a singly linked list in all cases and access only to a head pointer at the time of the function call. struct node; typedef struct node Node; struct node int data; Node next;

  • Can someone help me with a simple C programming problem? How do I make some code...

    Can someone help me with a simple C programming problem? How do I make some code like this into its own function? Currently it's pat of the main function, but I want to be able to call it. If you could help me with the prototype, function call, and definition, I'd really appreciate it. Thanks!    if(some_input=='r'){ someone = 0; } else if(some_input=='p'){ someone = 1; } else if(some_input=='s'){ someone = 2; } else if(some_input=='q' || some_input=='Q'){ break; // Breaks...

  • C Programming Language on Linux - Word Frequency Program Please write a Program in C that...

    C Programming Language on Linux - Word Frequency Program Please write a Program in C that will accept a text file name as a command-line argument via a main program that will do the following: First, read the file (first pass) and create a linked list of words (in their order of occurrence), with the frequency of each word set to 0. Then, read the file (second pass) and for each word identified, search the linked list, and when found,...

  • USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array...

    USING C++ and PYTHON Please Help me: 1. Write a function that takes in an array of integers (and the size of the array in the C/C++ version). The function will reverse the array of integers and return the reversed array. Print the array that is returned from the function. How you "return" the array is up to you, but do not overwrite the original array. Note: in Python, the term list is more appropriate, see https://docs.python.org/3.5/tutorial/datastructures.html a) Example input:...

  • I need help writing these functions in C programming. Write a C function that checks if...

    I need help writing these functions in C programming. Write a C function that checks if an array is sorted or not using an iterative approach. Write a C function that checks if an array is sorted or not using a recursive approach Write a C function to reverse the elements of an array. Note you are not allowed to use an 1. additional array to do that Write a C function to find the sum of the elements of...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT