
what is the problem in the display function ?
the code :
#include <iostream>
using namespace std;
class node
{
public:
int data;
node* next;
};
void display(node * head) // printing the data of the nodes
{
node* temp = head;
while (temp != NULL) {
cout << temp->data << endl;
temp = temp->next;
}
}
// add
void push(node** head, int newdata) // { 5, 10 , 20 , 30 , 40 }
{
node* newnode = new node();
newnode->data = newdata;
newnode->next = *head;
*head = newnode;
}
void append(node** head, int newdata) // { 10 , 20 , 30 , 40 }
{
node* newnode = new node();
newnode->data = newdata;
newnode->next = NULL;
node* last = *head;
if (*head == NULL) {
*head = newnode;
return;
}
while (last->next != NULL)
{
last = last->next;
}
last->next = newnode;
}
void insertafter(node* previousnode, int newdata) // { 10 , 15 , 20 , 30 , 40 }
{
if (previousnode == NULL) {
cout << "The previous node cannot be NULL";
}
node* newnode = new node();
newnode->data = newdata;
newnode->next = previousnode->next;
previousnode->next = newnode;
}
//delete
void remove(node* head, int target) // { 10 , 20 , 30 , 40 }
{
node* node2del;
node* temp;
temp = head;
if (temp != NULL) {
if (temp->data == target) {
free(temp);
head = head->next;
//delete(temp);
return;
}
while (temp->next != NULL) {
if (temp->next->data == target) {
node2del = temp->next;
temp->next = temp->next->next;
free(node2del);
return;
}
temp = temp->next;
}
if (temp->data != target) {
cout << "the item is not in the list %d" << target;
}
}
return;
}
int main()
{
node* head = new node();
node* second = new node();
node* third = new node();
node* fourth = new node();
head->data = 10;
head->next = second;
second->data = 20;
second->next = third;
third->data = 30;
third->next = fourth;
fourth->data = 40;
fourth->next = NULL;
cout << "\nDisplay the linked list items \n";
display(head);
/*cout << "\nPUSH \n";
push(&head, 5);
display(head);*/
/*cout << "\nappend \n";
append(&head, 100);
display(head);*/
//cout << "\nInsert after \n"; // { 10 , 20 , 30 , 15 , 40 }
//insertafter(head->next->next, 15);
//display(head);
remove(head, 10);
cout << "\nafter Delete \n";
display(head);
}

Step by stepSolved in 2 steps

- In C, using malloc to allocate memory for a linked list uses which memory allocation scheme? Heap allocation Static allocationarrow_forward1. Stack Implementation Write a method called insert for the class Stack. The method shall be implemented using an array-based structure. You can assume that String[] stack is defined in the class, which is responsible to store the elements and responsible to enforce the order of first-in last-out, a.k.a., FIFO. Additionally, you can assume there is a pointer called top, that indicates the position of the top of the stack, pointing to the next available position to insert. The method shall: • take a String s as a parameter, and shall add it at the top of the stack. shall return true if the element s was added successfully at the top of the stack, false otherwise. . . The method must check boundaries of capacity and limitation of the Stack. In case the method is invoked to insert an element of the top of the stack that exceeds its current capacity, the method shall handle the situation properly. Do not provide the entire Stack implementation, only the code solution of the method.arrow_forwardmain.cc file #include <iostream>#include <memory> #include "customer.h" int main() { // Creates a line of customers with Adele at the front. // LinkedList diagram: // Adele -> Kehlani -> Giveon -> Drake -> Ruel std::shared_ptr<Customer> ruel = std::make_shared<Customer>("Ruel", 5, nullptr); std::shared_ptr<Customer> drake = std::make_shared<Customer>("Drake", 8, ruel); std::shared_ptr<Customer> giveon = std::make_shared<Customer>("Giveon", 2, drake); std::shared_ptr<Customer> kehlani = std::make_shared<Customer>("Kehlani", 15, giveon); std::shared_ptr<Customer> adele = std::make_shared<Customer>("Adele", 4, kehlani); std::cout << "Total customers waiting: "; // =================== YOUR CODE HERE =================== // 1. Print out the total number of customers waiting // in line by invoking TotalCustomersInLine. //…arrow_forward
- struct remove_from_front_of_vector { // Function takes no parameters, removes the book at the front of a vector, // and returns nothing. void operator()(const Book& unused) { (/// TO-DO (12) ||||| // // // Write the lines of code to remove the book at the front of "my_vector". // // Remember, attempting to remove an element from an empty data structure is // a logic error. Include code to avoid that. //// END-TO-DO (12) /||, } std::vector& my_vector; };arrow_forward#include <iostream>using namespace std;class st{private:int arr[100];int top;public:st(){top=-1;}void push(int ItEM) {top++;arr[top]=ItEM; }bool ise() {return top<0;} int pop(){int Pop;if (ise()) cout<<"Stack is emptye ";else{Pop=arr[top];top--;return Pop;}}int Top(){int TOP;if (ise()) cout<<" empty ";else {TOP=arr[top];return TOP;}}void screen(){ for (int i = 0; i <top+1 ; ++i) {cout<<arr[i];} }};int main() {st c;c.push(1);c.push(2);c.push(3);c.push(4);c.pop();c.push(5);c.screen();return 0;} alternative to this code??arrow_forward#ifndef LLCP_INT_H#define LLCP_INT_H #include <iostream> struct Node{ int data; Node *link;};void DelOddCopEven(Node*& headPtr);int FindListLength(Node* headPtr);bool IsSortedUp(Node* headPtr);void InsertAsHead(Node*& headPtr, int value);void InsertAsTail(Node*& headPtr, int value);void InsertSortedUp(Node*& headPtr, int value);bool DelFirstTargetNode(Node*& headPtr, int target);bool DelNodeBefore1stMatch(Node*& headPtr, int target);void ShowAll(std::ostream& outs, Node* headPtr);void FindMinMax(Node* headPtr, int& minValue, int& maxValue);double FindAverage(Node* headPtr);void ListClear(Node*& headPtr, int noMsg = 0); // prototype of DelOddCopEven of Assignment 5 Part 1 #endifarrow_forward
- I need to convert this code using C++, that currently works with structs into classes, with the main class being called myGraph and a node class (if necessary) using composition. #include <stdio.h>#include <stdlib.h>#include <iostream>using namespace std; struct AdjListNode //node class{ int dest; struct AdjListNode* next;}; struct AdjList //node class{ struct AdjListNode *head; }; struct Graph //myGraph class{ int V; struct AdjList* array;}; struct AdjListNode * newAdjListNode(int dest){ struct AdjListNode * newNode = new AdjListNode; newNode->dest = dest; newNode->next = NULL; return newNode;} struct Graph* createGraph(int V) //constructor{ struct Graph* graph = new Graph; graph->V = V; graph->array = new AdjList; int i; for (i = 0; i < V; ++i) graph->array[i].head = NULL; return graph;} void addEdge(struct Graph* graph, int src, int dest){ struct AdjListNode* newNode =…arrow_forwardQuestions: There needs to be a dynamic array of Child that opposes with SCALE and is assigned to "familyTree". The createFamilyTree() function needs to be defined. Please see the C++ code.arrow_forwardC++ Data Structure:Create an AVL Tree C++ class that works similarly to std::map, but does NOT use std::map. In the PRIVATE section, any members or methods may be modified in any way. Standard pointers or unique pointers may be used.** MUST use given Template below: #ifndef avltree_h#define avltree_h #include <memory> template <typename Key, typename Value=Key> class AVL_Tree { public: classNode { private: Key k; Value v; int bf; //balace factor std::unique_ptr<Node> left_, right_; Node(const Key& key) : k(key), bf(0) {} Node(const Key& key, const Value& value) : k(key), v(value), bf(0) {} public: Node *left() { return left_.get(); } Node *right() { return right_.get(); } const Key& key() const { return k; } const Value& value() const { return v; } const int balance_factor() const {…arrow_forward
- void fun(int i) { do { if (i % 2 != 0) cout =1); cout << endl; } int main() { int i = 1; while (i <= 8) { fun(i); it; } cout <arrow_forward#include <stdio.h>#include <stdlib.h>#include <time.h> struct treeNode { struct treeNode *leftPtr; int data; struct treeNode *rightPtr;}; typedef struct treeNode TreeNode;typedef TreeNode *TreeNodePtr; void insertNode(TreeNodePtr *treePtr, int value);void inOrder(TreeNodePtr treePtr);void preOrder(TreeNodePtr treePtr);void postOrder(TreeNodePtr treePtr); int main(void) { TreeNodePtr rootPtr = NULL; srand(time(NULL)); puts("The numbers being placed in the tree are:"); for (unsigned int i = 1; i <= 10; ++i) { int item = rand() % 15; printf("%3d", item); insertNode(&rootPtr, item); } puts("\n\nThe preOrder traversal is: "); preOrder(rootPtr); puts("\n\nThe inOrder traversal is: "); inOrder(rootPtr); puts("\n\nThe postOrder traversal is: "); postOrder(rootPtr);} void insertNode(TreeNodePtr *treePtr, int value) { if (*treePtr == NULL) { *treePtr = malloc(sizeof(TreeNode)); if (*treePtr != NULL) { (*treePtr)->data = value;…arrow_forward#include <iostream>#include <vector>using namespace std; void PrintVectors(vector<int> numsList) { unsigned int i; for (i = 0; i < numsList.size(); ++i) { cout << numsList.at(i) << " "; } cout << endl;} int main() { vector<int> numsList; int userInput; int i; for (i = 0; i < 3; ++i) { cin >> userInput; numsList.push_back(userInput); } numsList.erase(numsList.begin()+1); numsList.insert(numsList.begin()+1, 102); numsList.insert(numsList.begin()+1, 100); PrintVectors(numsList); return 0;} Not all tests passed clearTesting with inputs: 33 200 10 Output differs. See highlights below. Special character legend Your output 33 100 102 10 Expected output 100 33 102 10 clearTesting with inputs: 6 7 8 Output differs. See highlights below. Special character legend Your output 6 100 102 8 Expected output 100 6 102 8 Not sure what I did wrong but the the 33…arrow_forward
- Computer Networking: A Top-Down Approach (7th Edi...Computer EngineeringISBN:9780133594140Author:James Kurose, Keith RossPublisher:PEARSONComputer Organization and Design MIPS Edition, Fi...Computer EngineeringISBN:9780124077263Author:David A. Patterson, John L. HennessyPublisher:Elsevier ScienceNetwork+ Guide to Networks (MindTap Course List)Computer EngineeringISBN:9781337569330Author:Jill West, Tamara Dean, Jean AndrewsPublisher:Cengage Learning
- Concepts of Database ManagementComputer EngineeringISBN:9781337093422Author:Joy L. Starks, Philip J. Pratt, Mary Z. LastPublisher:Cengage LearningPrelude to ProgrammingComputer EngineeringISBN:9780133750423Author:VENIT, StewartPublisher:Pearson EducationSc Business Data Communications and Networking, T...Computer EngineeringISBN:9781119368830Author:FITZGERALDPublisher:WILEY





