###################################### My code is below, I just want someone can
ID: 3747352 • Letter: #
Question
######################################
My code is below, I just want someone can varify and fix my code if I was wrong.
######################################
#include <iostream>
using namespace std;
struct Node {
int data;
Node *previous, *next;
};
void insertAfter(Node *, int);
int main(){
Node *current;
insertAfter(current, 5)
break;
}
void insertAfter(Node *current,int value)
{
struct Node *newNode;
newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data = value;
if(current->previous = NULL)
{
newNode->previous = NULL;
newNode->next = current;
current->previous = newNode;
}
}
Explanation / Answer
#include <iostream>
#include <stdlib.h>
using namespace std;
// Creates a structure for Node
struct Node
{
// To store data
int data;
// To point to next and previous node
Node *previous, *next;
};// End of structure
// To insert a node after the current node
void insertAfter(Node *current, int value)
{
// Checks if the given current node is NULL
if (current == NULL)
{
cout<<" ERROR: The given current node cannot be NULL";
return;
}
// Dynamically allocate memory to newNode
struct Node* newNode =(struct Node*) malloc(sizeof(struct Node));
// Assigns parameter value to new node data part
newNode->data = value;
// Make next of new node as next of current node
newNode->next = current->next;
// Move the next of current node as new node
current->next = newNode;
// Make current as previous of new_node
newNode->previous = current;
// Change previous of new_node's next node
if (newNode->next != NULL)
newNode->next->previous = newNode;
}// End of function
// Function to display list information
void display(struct Node *node)
{
// Loops till end of the list
while (node != NULL)
{
// Displays each node value with comma
cout<<node->data<<", ";
// Move next
node = node->next;
}// End of while loop
}// End of function
// Function to add a node at the end
void addFront(struct Node** head, int value)
{
// Dynamically allocates memory to new node
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
// Assigns parameter value to new node data part
newNode->data = value;
// New node next points to head
newNode->next = (*head);
// Assigns NULL to new node previous
newNode->previous = NULL;
// Checks if head is not NULL
if ((*head) != NULL)
// Assigns new node to head previous
(*head)->previous = newNode;
// Assigns new node to head
(*head) = newNode;
}// End of function
// main function to new node
int main()
{
// Creates a head node assigns NULL to it
struct Node* head = NULL;
// Calls the function to add at front
addFront(&head, 66);
addFront(&head, 26);
addFront(&head, 16);
addFront(&head, 6);
// Calls the function to display the list
display(head);
// Creates a current node which is points to head next node
struct Node *current = head->next;
cout<<" Display from current: ";
// Calls the function to display the list
display(current);
// Calls the function to inset a node after the current node
insertAfter(current, 5);
cout<<" Display from had inserting 5 after current: ";
// Calls the function to display the list
display(head);
return 0;
}// End of main function
Sample Output:
6, 16, 26, 66,
Display from current: 16, 26, 66,
Display from had inserting 5 after current: 6, 16, 5, 26, 66,
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.