how to create duplicate link list using C???



how to create duplicate link list using C???..

Answer / Mukesh Kumar Prajapat

First, you should define a structure for the node. Here's an example:nn```cnstruct Node {n int data;n struct Node *next;n};n```nTo create a new node, allocate memory and initialize its fields.nn```cnstruct Node* createNode(int data) {n struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));n newNode->data = data;n newNode->next = NULL;n return newNode;n}n```nTo create a duplicate link list, traverse the original list and create new nodes with the same data.nn```cnvoid duplicateList(struct Node* head) {n struct Node *current = head, *newHead = NULL, *newCurrent;nn while (current != NULL) {n newCurrent = createNode(current->data);n if (newHead == NULL) {n newHead = newCurrent;n newCurrent->next = NULL;n } else {n newCurrent->next = newHead->next;n newHead->next = newCurrent;n }n current = current->next;n }n}n```

Is This Answer Correct ?    0 Yes 0 No

Post New Answer

More C Interview Questions

CAN WE DEFINE ANY FUNCTION WITHIN A FUNCTION.

15 Answers  


Why data types in all programming languages have some range? Why ritche have disigned first time likethat?Why not a single data type can support all other types?

2 Answers   Excel,


here is a link to download Let_Us_C_-_Yashwant_Kanetkar

3 Answers   Microsoft,


What is the restrict keyword in C?

2 Answers  


Explain what are its uses in c programming?

1 Answers  


Write a C program to print 1 2 3 ... 100 without using loops?

15 Answers   Hindalco,


what is the c.

3 Answers   IBM, TCS,


what is difference between getchar,putchar functions and printf and scanf function? does putchar show output only when input given to it

5 Answers   DIT,


What does c in a circle mean?

1 Answers  


what is c

1 Answers  


When is a void pointer used?

1 Answers  


Write a program that takes a 5 digit number and calculates 2 power that number and prints it.

5 Answers   TCS, Vimukti Technologies,


Categories