Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory

Your task is to complete the following function/functions: 1. Given a position in the linked list, delete the node at that position.(Silver problem - Mandatory ) 2. Print the sum of all negative elements in the linked list.(Gold problem) If you want, you can refer to the the previous recitation manual (which was on Linked Lists) to implement node deletion.

#include

using namespace std;

//----------- Define Node --------------------------------------------------- struct Node{ int key; Node *next; };

//----------- Define Linked List --------------------------------------------------- class LinkedList { private: Node *head;

public: LinkedList(){ head = NULL; } void insert_begin(int newKey); void insert_end(int newKey); int negativeSum(); bool deleteAtIndex(int index); bool deleteAtHead(); void printList(); };

void LinkedList::insert_begin(int newKey){ //1. ALLOCATE NODE Node* newNode = new Node;

//2. PUT IN THE DATA newNode->key = newKey;

//3. Make next of newNode as head newNode->next = head;

//4. Change the head to newNode head = newNode; }

void LinkedList::insert_end(int newKey){ //1. ALLOCATE NODE Node* newNode = new Node;

//2. PUT IN THE DATA newNode->key = newKey;

//3. Make next of newNode as NULL newNode->next = NULL;

//4. Check if head is not Null if(head == NULL){ head = newNode; return; }

//4. Traverse the LinkedList till end Node* temp = head;

while(temp->next != NULL){ temp = temp->next; } temp->next = newNode; return; }

//TODO int LinkedList::negativeSum(){ if(head == NULL) return 0;

int total = 0; int count = 0;

// TODO Complete this function

cout<<"There is(are) "<

}

// Use deleteAtHead to delete the first item in the list bool LinkedList::deleteAtHead() { bool isDeleted = false;

if(head == NULL){ cout<< "List is already empty"<

Node *temp = head; head = temp->next; delete temp; isDeleted = true;

return isDeleted; }

//TODO bool LinkedList::deleteAtIndex(int n) { bool isDeleted = false;

if(head == NULL){ cout<< "List is already empty"<

Node *pres = head; Node *prev = NULL;

//TODO Complete this function //isDeleted = true;

return isDeleted; }

void LinkedList::printList(){ Node* temp = head;

while(temp->next != NULL){ cout<< temp->key <<" -> "; temp = temp->next; }

cout<key<

int main() { LinkedList li; cout<<"Adding nodes to List:"<2 li.insert_begin(-1); li.printList(); // -1->2->-7 li.insert_end(-7); li.printList(); // -1->2->-7->10 li.insert_end(10); li.printList(); // -1->2->-7->10->3 li.insert_end(3); li.printList(); cout<

cout<<"Running delete function."<

/******************************* Silver Problem - Implement the deleteAtIndex function ********************************/

cout<<"Deleting node at index:"<

if(!li.deleteAtHead()) { cout<<"Delete failed!"<

cout<<"Get the sum of negatives"<

/******************************* Gold Problem - Implement the negativeSum function ********************************/ int negsum; negsum = li.negativeSum(); cout<<"Sum of all the negative elements is "<

}

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored 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

Recommended Textbook for

Database Publishing With Filemaker Pro On The Web

Authors: Maria Langer

1st Edition

0201696657, 978-0201696653

More Books

Students also viewed these Databases questions