Answered step by step
Verified Expert Solution
Link Copied!

Question

00
1 Approved Answer

Given my current c++ code, how could I add these three functions to execute as soon as the user enters data? 1) Arrange the nodes

Given my current c++ code, how could I add these three functions to execute as soon as the user enters data?

1) Arrange the nodes in ascending order

2) reverse the node sequence

3) deleting duplicate values

4) adding every 2nd node

main.cpp

#include using namespace std;

struct node { int data; node *next; };

class list { private: node *head, *tail; public: list() { head = NULL; tail = NULL; } void get() { int input; cout << "|Enter a sequence of numbers. -1 to finish. "; while(1) { cin >> input; if(input!=-1) { createNode(input); } else if(input == -1) { display(); return; } } } void createNode(int val) { node *temp=new node; temp->data = val; temp->next = NULL; if(head == NULL) { head = temp; tail = temp; temp = NULL; } else { tail->next = temp; tail = temp; } } int sum() { int total = 0; node *temp = head; while(temp!=NULL) { total += temp->data; temp=temp->next; } return total; } int count() { int num=0; node *temp = head; while(temp!=NULL) { num++; temp=temp->next; } return num; } int avg() { if (count()>0) { return sum()/count(); } else { return 0; } } void display() { cout << "|Current Sequence:\t"; node *temp = head; while(temp!=NULL) { cout << temp->data << " "; temp=temp->next; } cout << endl; cout << "|Sum:\t" << sum() << endl; cout << "|#Node:\t" << count() << endl; cout << "|Avg:\t" << avg() << endl; } void delFirst() { cout << "|Deleting first value..." << endl; if(head == NULL) { cout << "There is no value to delete" << endl; } else { node *temp = head; head = head->next; delete temp; display(); } } void delLast() { cout << "|Deleting last value..." << endl; if(head == NULL) { cout << "|There is no value to delete" << endl; } else { node *current, *prev; current = head; while(current->next!=NULL) { prev= current; current=current->next; } tail = prev; prev->next=NULL; delete current; display(); } } bool search(int val) { node *temp = head; while(temp!=NULL) { if(val!=temp->data) { temp=temp->next; } else if (val == temp->data) { return true; } } return false; } void del(int num) { if(search(num)==true) { if(num == head->data) { delFirst(); } else if(num == tail->data) { delLast(); } else { node *temp = new node; temp = head; while (temp!=NULL) { if(num == temp->data) { head = head->next; delete temp; display(); } else { temp->next; } } } } else { cout << "|\tNumber doesn't exist "; } } };

int main() { int num; list l; l.get(); cout << "|Enter a number to verify:\t"; cin >> num; if(l.search(num)==true) { cout << "|\tNumber Exists!" << endl; } else { cout << "|\tNumber Doesn't Exist!" << endl; } l.delFirst(); l.delLast(); cout << "|Enter a value to delete:\t"; cin >> num; l.del(num); return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access with AI-Powered Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Students also viewed these Databases questions