I was practicing program problems without answer.
Not sure on how to write this program and I need help. it needs to be in C language. Thank you!
/*
Following program implemented using DEV C++
you can try with any C compiler also
*/
#include<stdio.h>
//declaration of Node
struct Node {
//consist data data and next and prev address
int data;
Node *next, *prev;
};
void removeDuplicates(Node* head)
{
Node* curNode = head;
Node* next_next;
if (curNode == NULL)
return;
while (curNode->next != NULL) {
/* Compare curNode node
with next node */
if (curNode->data ==
curNode->next->data) {
/* The sequence of steps is important*/
next_next = curNode->next->next;
curNode->next = next_next;
}
else /* This is tricky:
only advance if no deletion */
{
curNode = curNode->next;
}
}
}
void AddNode(Node** addr_head, int data)
{
//allocate memory for new born node
Node* new_node = new Node;
// add data to it
new_node->data = data;
//if list empty
if (*addr_head == NULL) {
new_node->next =
new_node;
new_node->prev =
new_node;
}
// add node at end
else {
Node* last =
(*addr_head)->prev;
new_node->next =
*addr_head;
new_node->prev =
last;
last->next =
(*addr_head)->prev = new_node;
}
*addr_head = new_node;
}
//called merge recusrsivly till list end
Node* merge(Node* fnode, Node* snode)
{
// first list is empty
if (!fnode)
return snode;
// second list is empty
if (!snode)
return fnode;
//comparing data and calling merge function recurverly
if (fnode->data < snode->data) {
fnode->next =
merge(fnode->next, snode);
fnode->next->prev
= fnode;
fnode->prev =
NULL;
return fnode;
}
//comparing data and calling merge function recurverly
else {
snode->next =
merge(fnode, snode->next);
snode->next->prev
= snode;
snode->prev =
NULL;
return snode;
}
}
//mergingg list
Node* Merge(Node* FirstHead, Node* Secondhead)
{
//fiesr list is empty
if (!FirstHead)
return Secondhead;
//second list is empty
if (!Secondhead)
return FirstHead;
//going till end of list
Node* last_node;
if (FirstHead->prev->data <
Secondhead->prev->data)
last_node =
Secondhead->prev;
else
last_node =
FirstHead->prev;
// adding null at end
FirstHead->prev->next =
Secondhead->prev->next = NULL;
//merginng sorted list
Node* newhead = merge(FirstHead,
Secondhead);
newhead->prev = last_node;
last_node->next = NULL;
// removing duplicate if any
removeDuplicates(newhead);
return newhead;
}
// method to print all node
void Display_List(Node* head)
{
Node* nodetemp = head;
while (nodetemp->next != NULL) {
printf("%d
",nodetemp->data);
nodetemp =
nodetemp->next;
}
printf("%d ",nodetemp->data);
}
int main()
{
Node *FirstHead = NULL, *Secondhead =
NULL;
// Linked List 1
AddNode(&FirstHead, 2);
AddNode(&FirstHead, 55);
AddNode(&FirstHead, 33);
AddNode(&FirstHead, 1);
//Linked List 2
AddNode(&Secondhead, 11);
AddNode(&Secondhead, 4);
AddNode(&Secondhead, 7);
AddNode(&Secondhead, 2);
Node* Head = Merge(FirstHead, Secondhead);
printf("Final Sorted List: ");
Display_List(Head);
return 0;
}
/*
output
Final Sorted List: 1 2 7 4 11
--------------------------------
Process exited after 0.06974 seconds with return value 0
Press any key to continue . . .
*/
I was practicing program problems without answer. Not sure on how to write this program and...
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....
/* I'm trying to write this program here but my problem is getting the program to save the user input to "int data" but cannot because it is part of the struct. What do I have to do in order to get this to work? (Language is C++) Write a program that prompts the user to enter numbers on the screen and keep adding those numbers to a linked list. The program stops when user enters -999. Hint: Use a...
hw3 Data Structure: Please write programs and finish the question in C language: thank you (You can do both 5 & 6 if you know both, or you can do any one of these 2 questions that you know, thank you!) 5. Generate a function for clockwise rotation each node (either char type or int type) in doubly linked list by N places, e.g. given list = NULL<-Head<=>c<=>i<=>v<=>i>=>c->NULL, from function call rotate DLL(list, 3), the output will be like this...
Please answer this problem in C++, Thank you! Read and study the sample program: "hashing with chaining using singly linked lists", as well as "hashing with chaining using doubly linked lists". Notice this program is complete except it doesn't include the remove function. Write the remove function and test it with the rest of the program including the given main program. Upload your C++ source file. ---Here is the referenced program: "hashing with chaining using singly linked lists". Below this...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
Must be written and C, and compile with MinGW. Thank
you!
am Specification For this assignment, you will write a multi-file C program to define, implement and use a dynamic linked lists. Please refer to Lab 07 for the definition of a basic linked list. In this assignment you will need to use the basic ideas of a node and of a linked list of nodes to implement a suit of functions which can be used to create and maintain...
Write a function to insert a name at the end of a linked list? (C) I have a linked list: struct node { char person[100]; struct node *next; }; int main() { struct node *new_node; new_node=malloc(sizeof(struct node)); printf("Please enter a name:\n"); scanf("%s", ); } How do I get the name from the user and write a function to add it to the end of the linked list? I'm not supposed to use the insert() library function, which is why I'm...
Doubly Linked List Is there a way to further modify/simplify/improve this program? I was thinking of maybe changing how to get size. I'm not sure. import java.util.Scanner; /* Class Node */ class Node { protected int data; protected Node next, prev; /* Constructor */ public Node() { next = null; prev = null; data = 0; } /* Constructor */ public Node(int d, Node n, Node p) { data = d; next = n; prev = p; } /* Function...
C++ NEED HELP WITH MY REVERSE STRING FUNCTION IN LINK LIST A function Reverse, that traverses the linked list and prints the reverse text to the standard output, without changing the linked list. ( Pass the linked list by value, you have the freedom to create a doubly linked list that is a copy of the original list in your program before you call the function) #include "pch.h" #include <iostream> #include <string.h> #include <string> using namespace std; #define MAX 512...