Question

currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to...

currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to a linked list and a value, and if the value is in the linked list, it is deleted and the function returns true. If it is not in the list, the function returns false. 8. Expand your menu to test your three new functions. When choosing to test the find or findAndDelete functions, the user will be first asked to enter the value to find or to find and delete. Depending on whether the functions returned true or false, the main program should write an appropriate message to the user. 9. Finally write a function called deleteList that will delete all nodes in the linked list and return the parameter head set to nullptr. You can add it to the menu for testing, but also put it at the end of the program to release all dynamic memory you used.

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

output

copyable code

#include<iostream>

#include<cstdio>

#include<cstdlib>

using namespace std;

class NodeType {

public:

NodeType( int = 0 );

int info;

NodeType * nextPtr;

};

NodeType::NodeType( int data )

{

info = data;

nextPtr = 0;

}

/*

* insert_node element

*/

void insert_node(NodeType **headnode, int val)

{

NodeType *current_ptr;

current_ptr = new(NodeType);

if (current_ptr == NULL)

{

cout<<"Memory not allocated "<<endl;

return;

}

else

{

current_ptr->info = val;

current_ptr->nextPtr = *headnode;

*headnode = current_ptr;

}

cout<<"Element insert_nodeed at beginning"<<endl;

}

//delete

void deleteFirst(NodeType **headnode)

{

NodeType *tempnode = *headnode;

*headnode = tempnode->nextPtr;

delete(tempnode);

tempnode = NULL;

}

//delete list

void delete_list(NodeType **headnode)

{

NodeType *tempnode = *headnode;

while (*headnode) {

*headnode = tempnode->nextPtr;

delete(tempnode);

tempnode = *headnode;

}

}

/*

*printlist list of element

*/

void printlist(NodeType *headnode)

{

NodeType *tempnode;

if (headnode == NULL) {

cout<<"Empty List"<<endl;

return;

}

tempnode = headnode;

cout<<"lilst of elements are: "<<endl;

while (tempnode != NULL)

{

cout<<tempnode->info<<"->";

tempnode = tempnode->nextPtr;

}

cout<<"NULL"<<endl;

}

/*

* Search node

*/

bool find(NodeType *headnode, int val)

{

bool flag = false;

if (headnode == NULL) {

cout<<"empty list"<<endl;

return flag;

}

NodeType *s;

s = headnode;

while (s != NULL)

{

if (s->info == val)

{

flag = true;

cout<<"Element "<<val<<" is found"<<endl;

break;

}

s = s->nextPtr;

}

if (!flag)

cout<<"Element "<<val<<" not found in the list"<<endl;

return flag;

}

void findanddelete(NodeType **headnode, int val)

{

NodeType *prev = NULL;

NodeType *curr = *headnode;

if (find(*headnode, val)) {

while (curr) {

if ((curr == *headnode) && (curr->info == val)) {

*headnode = curr->nextPtr;

delete(curr);

cout<<"Element "<<val<<" deleted element list"<<endl;

return;

}

if (curr->info == val) {

prev->nextPtr = curr->nextPtr;

delete(curr);

cout<<"Element "<<val<<" deleted from the list"<<endl;

return;

}

prev = curr;

curr = curr->nextPtr;

}

delete(curr);

cout<<"Element "<<val<<" deleted from the list"<<endl;

return;

}

cout<<"val to deleted not found on the list"<<endl;

return;

}

int main()

{

int opt, val;

NodeType *headnode = NULL;

NodeType *current_ptr;

while (1) {

cout<<"1.Add"<<endl;

cout<<"2.printlist"<<endl;

cout<<"3.Delete First"<<endl;

cout<<"4.Find & delete"<<endl;

cout<<"5.Delete List"<<endl;

cout<<"6.Quit "<<endl;

cout<<"Enter your option : ";

cin>>opt;

switch(opt) {

case 1:

cout<<"Enter the val to be insert_nodeed: ";

cin>>val;

cout<<"insert_node at Beginning: "<<endl;

insert_node(&headnode, val);

cout<<endl;

break;

case 2:

cout<<"Display link list element"<<endl;

printlist(headnode);

cout<<endl;

break;

case 3:

cout<<"Deleting Node at the begining: "<<endl;

deleteFirst(&headnode);

cout<<endl;

break;

case 4:

cout<<"Enter the node val to deleted: "<<endl;

cin>>val;

findanddelete(&headnode, val);

break;

case 5:

cout<<"Delete list:"<<endl;

delete_list(&headnode);

cout<<endl;

break;

case 6:

cout<<"Quiting..."<<endl;

delete_list(&headnode);

exit(1);

break;

default:

cout<<"Wrong option"<<endl;

}

}

}

Add a comment
Know the answer?
Add Answer to:
currentPtr. Use your code to make a boolean function findAndDelete that is given a pointer to...
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
  • In C++ syntax please Write a program that implements and demonstrates a linked list using functions....

    In C++ syntax please Write a program that implements and demonstrates a linked list using functions. Your program should first dehne a node that stores an integer and then your program will include the following functions appendo- This function accepts the head pointer (by reference) of a linked list and an integer as it's only arguments. It then creates a node, stores the integer argument in the node, and adds it to the end of the list. findo-This function accepts...

  • C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supp...

    C++ program, inventory.cpp implementation Mostly need the int load(istream&) function. Implementation: You are supposed to write three classes, called Item, Node and Inventory respectively Item is a plain data class with item id, name, price and quantity information accompanied by getters and setters Node is a plain linked list node class with Item pointer and next pointer (with getters/setters) Inventory is an inventory database class that provides basic linked list operations, delete load from file / formatted print functionalities. The...

  • can someone please double check my code here are the requirements please help me fulfill the...

    can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...

  • linked list operation /*************************************************************************************** This function creates a new node with the information give as a...

    linked list operation /*************************************************************************************** This function creates a new node with the information give as a parameter and looks for the right place to insert it in order to keep the list organized ****************************************************************************************/ void insertNode(string first_name, string last_name, string phoneNumber) { ContactNode *newNode; ContactNode *nodePtr; ContactNode *previousNode = nullptr; newNode = new ContactNode; /***** assign new contact info to the new node here *****/ if (!head) // head points to nullptr meaning list is empty { head = newNode;...

  • 2) (10 pts) Write a function that takes in a pointer to a linked list of...

    2) (10 pts) Write a function that takes in a pointer to a linked list of nodes storing integers and a variable named value, and returns the number of nodes in the list storing that value. For example, if a list pointed to by listPtr stores 2, 6, 2, 3, 4, 2, 6, and 6 and value = 6, your function should return 3, since 6 appears in the list 3 times. Please use the struct and function prototype provided...

  • Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary...

    Write a function, swapSubtrees, that swaps all of the left and right subtrees of a binary tree. Add this function to the class binaryTreeType and create a program to test this function. #include <iostream> using namespace std; //Definition of the Node template <class elemType> struct nodeType { elemType info; nodeType<elemType> *lLink; nodeType<elemType> *rLink; }; //Definition of the class template <class elemType> class binaryTreeType { public: //Overload the assignment operator. const binaryTreeType<elemType>& operator=(const binaryTreeType<elemType>&) { if (this != &otherTree) //avoid self-copy...

  • Please use C programming to write the code to solve the following problem. Also, please use the i...

    Please use C programming to write the code to solve the following problem. Also, please use the instructions, functions, syntax and any other required part of the problem. Thanks in advance. Use these functions below especially: void inputStringFromUser(char *prompt, char *s, int arraySize); void songNameDuplicate(char *songName); void songNameFound(char *songName); void songNameNotFound(char *songName); void songNameDeleted(char *songName); void artistFound(char *artist); void artistNotFound(char *artist); void printMusicLibraryEmpty(void); void printMusicLibraryTitle(void); const int MAX_LENGTH = 1024; You will write a program that maintains information about your...

  • Write a function searchList that recursively searches a linked list for a specified value. The function...

    Write a function searchList that recursively searches a linked list for a specified value. The function should return a pointer to the value if it’s found; otherwise, NULL should be returned. Use your function in a test program that creates a list of integers. The program should prompt the user for a value to locate in the list. It's a C program.

  • please solve this question: Write a character Max-Heap Builder program in C++. The program should display...

    please solve this question: Write a character Max-Heap Builder program in C++. The program should display the menu below. Each item in the menu should be implemented in a function. a. Add a node. One node to be added to the max-heap. b. Delete a node. One node to be deleted from the max-heap. C. Search a node. Returns true if the node exists in the max-heap, otherwise it returns false. d. Print the tree. Prints the heap in level-order...

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
ADVERTISEMENT