#include "LinkedList.h" using namespace std; // Add a new node to the list void LinkedList::insert(Node* prev, int newKey){ //Check if head is Null i.e list is empty if(head == NULL){ head = newNode; head->key = newKey; head->next = NULL; } // if list is not empty, look for prev and append our node there elseif(prev == NULL) { Node* newNode = newNode; newNode->key = newKey; newNode->next = head; head = newNode; } else{ Node* newNode = newNode; newNode->key = newKey; newNode->next = prev->next; prev->next = newNode; } } // TODO: SILVER PROBLEM // Delete node at a particular index bool LinkedList::deleteAtIndex(int n) { boolisDeleted = false; if(head == NULL){ cout<<"List is already empty"<next != NULL){ cout<key<<" -> "; temp = temp->next; } cout<key<key != key) { ptr = ptr->next; } returnptr; }

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question
Complete the //comments and TODOs in c++
 
#include "LinkedList.h"

using namespace std;

// Add a new node to the list
void LinkedList::insert(Node* prev, int newKey){

//Check if head is Null i.e list is empty
if(head == NULL){
head = newNode;
head->key = newKey;
head->next = NULL;
}

// if list is not empty, look for prev and append our node there
elseif(prev == NULL)
{
Node* newNode = newNode;
newNode->key = newKey;
newNode->next = head;
head = newNode;
}

else{

Node* newNode = newNode;
newNode->key = newKey;
newNode->next = prev->next;
prev->next = newNode;

}
}


// TODO: SILVER PROBLEM
// Delete node at a particular index
bool LinkedList::deleteAtIndex(int n)
{
boolisDeleted = false;

if(head == NULL){
cout<<"List is already empty"<<endl;
returnisDeleted;
}

// Special case to delete the head
if (n == 0) {
//TODO
}

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

// TODO

returnisDeleted;
}

// TODO: GOLD PROBLEM
// Swap the first and last nodes (don't just swap the values)
bool LinkedList::swapFirstAndLast()
{
boolisSwapped = false;

if(head == NULL) {
cout<<"List is empty. Cannot swap"<<endl;
returnfalse;
}

// TODO (take care of the edge case when your linkedlist has just 2 nodes)

returnisSwapped;
}

// Print the keys in your list
void LinkedList::printList(){
Node* temp = head;

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

cout<<temp->key<<endl;
}

// Search for a specified key and return a pointer to that node
Node* LinkedList::searchList(int key) {

Node* ptr = head;
while (ptr != NULL && ptr->key != key)
{
ptr = ptr->next;
}
returnptr;
}
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps with 1 images

Blurred answer
Knowledge Booster
Operations of Linked List
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education