Database System Concepts
Database System Concepts
7th Edition
ISBN: 9780078022159
Author: Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher: McGraw-Hill Education
Bartleby Related Questions Icon

Related questions

bartleby

Concept explainers

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
Check Mark
Knowledge Booster
Background pattern image
Computer Science
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
Recommended textbooks for you
Text book image
Database System Concepts
Computer Science
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:McGraw-Hill Education
Text book image
Starting Out with Python (4th Edition)
Computer Science
ISBN:9780134444321
Author:Tony Gaddis
Publisher:PEARSON
Text book image
Digital Fundamentals (11th Edition)
Computer Science
ISBN:9780132737968
Author:Thomas L. Floyd
Publisher:PEARSON
Text book image
C How to Program (8th Edition)
Computer Science
ISBN:9780133976892
Author:Paul J. Deitel, Harvey Deitel
Publisher:PEARSON
Text book image
Database Systems: Design, Implementation, & Manag...
Computer Science
ISBN:9781337627900
Author:Carlos Coronel, Steven Morris
Publisher:Cengage Learning
Text book image
Programmable Logic Controllers
Computer Science
ISBN:9780073373843
Author:Frank D. Petruzella
Publisher:McGraw-Hill Education