Write a program to implement linked list data structure that will have following functions: a. Append a...

60.1K

Verified Solution

Question

Programming

Write a program to implement linked list data structure thatwill have following functions:
a. Append a node in the list
b. Insert a node in the list
c. Delete a node from the list
d. Display list
e. Find maximum value in the list
f. Find how many times a value exists in the list.
g. Search
Portion of the code is give below. You have to write code forthe items (e, f, g)

Program:
#include
#include
#include
struct node {
int data;
node* next;
};
node* head=NULL;
void appendNode(int value){
node *newNode, *curr;
newNode = new node();
newNode->data=value;
newNode->next=NULL;
if (!head)
head=newNode;
else // Otherwise, insert newNode at end
{
curr=head;
while (curr->next)
curr = curr->next;
curr->next = newNode; }
}
void insertNode(int value){
node *newNode, *curr, *previous;
}
newNode = new node();
newNode->data = value;
if (head==NULL) { head = newNode;
newNode->next = NULL;
}
else {
curr = head;
while (curr != NULL && curr->data < value)previous = curr;
curr = curr->next;
}
if (previous == NULL)
{head = newNode;
newNode->next = curr;}
else {previous->next = newNode;
newNode->next = curr;}
}
{
void displayList(void) { node *curr;
curr = head;
while (curr!=NULL)
{
std::cout << curr->data <<\" \";
curr = curr->next;

}
std::cout << \"\n\";
}
void deleteNode(int value){ node *deletedNode, *curr; if (curr== NULL){
// Deleting first node deletedNode = head; head = head->next;
} else{
// Deleting other nodes deletedNode = curr->next;curr->next = deletedNode ->next;
}
delete deletedNode;
}
int findmaxNode(){
//complete the code here
}
int countNode(){
//complete the code here
}
int search_item() {
int found;
return found; }
int main()
{
int choice;
int x;
do{
system(\"cls\");
std::cout << \"1. Append Node\"<<\"\n\"; std::cout<< \"2. Insert Node\"<<\"\n\";
std::cout << \"3. Delete Node\"<<\"\n\";
std::cout << \"4. Find Maximum Node\"<<\"\n\";std::cout << \"5. Display Linked List\"<<\"\n\";
std::cout << \"6. Search Node\"<<\"\n\";
std::cout << \"7. Count nodes \"<<\"\n\";
std::cout << \"8. Exit\"<<\"\n\";
std::cout << \"Enter your choice(1-8):\";std::cin>>choice;
switch(choice) {
case 1:
std::cout<<\"Enter a value:\";
std::cin>>x;
appendNode(x);
std::cout<<\"value is appened\n\";
break;
case 2:
std::cout<<\"Enter a value to insert:\";
std::cin>>x;
insertNode(x);
std::cout<<\"Value is inserted\n\";
break;
case 3:
std::cout<<\"complete code\n\";
std::cout<<\"Enter a value to delete:\";
std::cin>>x;
deleteNode(x);
//complete code first break;
case 4:
std::cout<<\"complete code\n\";
//int max = maxNode();
//complete code first //std::cout<<\"Maximum is:\"<
break;
case 5:
displayList();
break;
case 6:
std::cout<<\"Complete code for search\n\"; int x;
x= search_item(); if(x==0)
std::cout<<\"Not found\n\";
else
std::cout<<\"found\n\";
break;
case 7:
std::cout<<\"Complete code\n\";
//int count = count_nodes();
//std::cout<<\"Number of nodes in the listis:\"<

}
system(\"pause\");
} while(choice!=8);
}

Answer & Explanation Solved by verified expert
4.4 Ratings (605 Votes)
As per the question we have to implement linked list with given objectives as a Append a node in the list b Insert a node in the list c Delete a node from the list d Display list e Find maximum value in the list f Find how many times a value exists in the list g Search As per    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students