Complete the BOLD parts #pragma once #ifndef NODE_HPP #define NODE_HPP #include
ID: 3747603 • Letter: C
Question
Complete the BOLD parts
#pragma once
#ifndef NODE_HPP
#define NODE_HPP
#include
#include
/**
* @class Node
* @brief A node in a linked list
*/
template
struct Node {
/** The data element **/
T data;
/** A pointer to the next Node **/
Node* next;
};
/**
* Insert an element into a linked list at a given position
* @pre The position is within the bound of the list, that is,
* 1 <= position <= length(head)+1
* @param head the head of the linked list.
* @param position the list position at which to insert element.
* @param element the element to insert into the list.
* @return the head of the modified list
*/
template
Node* insert(Node* head, int position, T element) {
Node *newNode = new Node();
newNode->data = element;
if (position == 1) {
newNode->next = head;
head = newNode;
}
else {
Node* previousNode = head;
for (int p = 1; p < position - 1; p++) {
previousNode = previousNode->next;
}
newNode->next = previousNode->next;
previousNode->next = newNode;
}
return head;
}
/**
* Display a string representation of the list to standard out.
* @param head the head of the linked list.
*/
template
void show(Node* head) {
Node* currentNode = head;
while (currentNode != nullptr) {
std::cout << currentNode->data;
std::cout << " -> ";
currentNode = currentNode->next;
}
std::cout << "NULL" << std::endl;
}
/**
* Checks whether the list is empty.
* @param head the head of the linked list.
* @return True if the list is empty, otherwise false.
*/
template
bool isEmpty(Node* head) {
return head == nullptr;
}
// TODO document and complete the implementation of the following function that
// returns the number of elements in a linked list.
template
int length(Node* head) {
return 0;
}
// TODO document and complete the implementation of the following function that
// returns a boolean indicating whether the parameter element is contained
// within the linked list.
template
bool contains(Node* head, T element) {
return false;
}
// TODO document and complete the implementation of the following function that
// returns the element at the desired position of the list. Note that the
// positions start at one, unlike a C++ array, where the indices start at zero.
// You can assume that the caller of the function will pass in a valid
// position, but that assumption must be documented appropriately.
template
T getElement(Node* head, int position) {
return head->data;
}
// TODO document and complete the implementation of the following function that
// returns the position of the first instance of the element passed in as a
// parameter. If the element is not present in the list, then return -1.
template
int getPosition(Node* head, T element) {
return -1;
}
// TODO document and complete the implementation of the following function that
// replaces the element at the desired position of the list and returns the
// head of the modified list. Note that the positions start at one, unlike a
// C++ array, where the indices start at zero. You can assume that the caller
// of the function will pass in a valid position, but that assumption must be
// documented appropriately.
template
Node* replace(Node* head, int position, T element) {
return head;
}
// TODO document and complete the implementation of the following function that
// removes the Node at the desired position of the list and returns the head of
// the modified list. Note that the positions start at one, unlike a C++ array,
// where the indices start at zero. You can assume that the caller of the
// function will pass in a valid position, but that assumption must be
// documented appropriately.
template
Node* remove(Node* head, int position) {
return head;
}
#endif
Explanation / Answer
here is the complete working code of kinked list with additional functions
please comment below for any modifications needed thank you
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int daata;
struct node *nextt;
}*startt;
class single_Linked_list
{
public:
node* creating(int);
void insert_At_Begin();
void insert_At_Pos();
void insert_At_Last();
void delete_At_pos();
void sorting();
void search();
void update();
void reverse();
void display();
single_Linked_list()
{
startt = NULL;
}
};
main()
{
int choic, nodes, ele, postion, i;
single_Linked_list sl;
startt = NULL;
while (1)
{
cout<<endl<<"---------------------------------"<<endl;
cout<<endl<<"Operations on singly linked list"<<endl;
cout<<endl<<"---------------------------------"<<endl;
cout<<"1.Insert Node at beginning"<<endl;
cout<<"2.Insert node at last"<<endl;
cout<<"3.Insert node at postion"<<endl;
cout<<"4.Sort Link List"<<endl;
cout<<"5.Delete a Particular Node"<<endl;
cout<<"6.Update Node Value"<<endl;
cout<<"7.Search Element"<<endl;
cout<<"8.Display Linked List"<<endl;
cout<<"9.Reverse Linked List "<<endl;
cout<<"10.Exit "<<endl;
cout<<"Enter your choic : ";
cin>>choic;
switch(choic)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.insert_At_Begin();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.insert_At_Last();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given postion:"<<endl;
sl.insert_At_Pos();
cout<<endl;
break;
case 4:
cout<<"Sort Link List: "<<endl;
sl.sorting();
cout<<endl;
break;
case 5:
cout<<"Delete a particular node: "<<endl;
sl.delete_At_pos();
break;
case 6:
cout<<"Update Node Value:"<<endl;
sl.update();
cout<<endl;
break;
case 7:
cout<<"Search ele in Link List: "<<endl;
sl.search();
cout<<endl;
break;
case 8:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 9:
cout<<"Reverse elements of Link List"<<endl;
sl.reverse();
cout<<endl;
break;
case 10:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choic"<<endl;
}
}
}
node *single_Linked_list::creating(int valuee)
{
struct node *temporary, *s;
temporary = new(struct node);
if (temporary == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temporary->daata = valuee;
temporary->nextt = NULL;
return temporary;
}
}
void single_Linked_list::insert_At_Begin()
{
int valuee;
cout<<"Enter the valuee to be inserted: ";
cin>>valuee;
struct node *temporary, *p;
temporary = creating(valuee);
if (startt == NULL)
{
startt = temporary;
startt->nextt = NULL;
}
else
{
p = startt;
startt = temporary;
startt->nextt = p;
}
cout<<"Element Inserted at beginning"<<endl;
}
void single_Linked_list::insert_At_Last()
{
int valuee;
cout<<"Enter the valuee to be inserted: ";
cin>>valuee;
struct node *temporary, *s;
temporary = creating(valuee);
s = startt;
while (s->nextt != NULL)
{
s = s->nextt;
}
temporary->nextt = NULL;
s->nextt = temporary;
cout<<"Element Inserted at last"<<endl;
}
void single_Linked_list::insert_At_Pos()
{
int valuee, postion, counter = 0;
cout<<"Enter the valuee to be inserted: ";
cin>>valuee;
struct node *temporary, *s, *ptr;
temporary = creating(valuee);
cout<<"Enter the postion at which node to be inserted: ";
cin>>postion;
int i;
s = startt;
while (s != NULL)
{
s = s->nextt;
counter++;
}
if (postion == 1)
{
if (startt == NULL)
{
startt = temporary;
startt->nextt = NULL;
}
else
{
pntr = startt;
startt = temporary;
startt->nextt = pntr;
}
}
else if (postion > 1 && postion <= counter)
{
s = startt;
for (i = 1; i < postion; i++)
{
pntr = s;
s = s->nextt;
}
pntr->nextt = temporary;
temporary->nextt = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}
void single_Linked_list::sorting()
{
struct node *pntr, *s;
int valuee;
if (startt == NULL)
{
cout<<"The List is empty"<<endl;
return;
}
pntr = startt;
while (pntr != NULL)
{
for (s = pntr->nextt;s !=NULL;s = s->nextt)
{
if (pntr->daata > s->daata)
{
valuee = pntr->daata;
pntr->daata = s->daata;
s->daata = valuee;
}
}
pntr = pntr->nextt;
}
}
void single_Linked_list::delete_At_pos()
{
int postion, i, counter = 0;
if (startt == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the postion of valuee to be deleted: ";
cin>>postion;
struct node *s, *pntr;
s = startt;
if (postion == 1)
{
startt = s->nextt;
}
else
{
while (s != NULL)
{
s = s->nextt;
counter++;
}
if (postion > 0 && postion <= counter)
{
s = startt;
for (i = 1;i < postion;i++)
{
pntr = s;
s = s->nextt;
}
pntr->nextt = s->nextt;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}
void single_Linked_list::update()
{
int valuee, postion, i;
if (startt == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the node postion to be updated: ";
cin>>postion;
cout<<"Enter the new valuee: ";
cin>>valuee;
struct node *s, *pntr;
s = startt;
if (postion == 1)
{
startt->daata = valuee;
}
else
{
for (i = 0;i < postion - 1;i++)
{
if (s == NULL)
{
cout<<"There are less than "<<postion<<" elements";
return;
}
s = s->nextt;
}
s->daata = valuee;
}
cout<<"Node Updated"<<endl;
}
void single_Linked_list::search()
{
int valuee, postion = 0;
bool flag = false;
if (startt == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the valuee to be searched: ";
cin>>valuee;
struct node *s;
s = startt;
while (s != NULL)
{
postion++;
if (s->daata == valuee)
{
flag = true;
cout<<"Element "<<valuee<<" is found at postion "<<postion<<endl;
}
s = s->nextt;
}
if (!flag)
cout<<"Element "<<valuee<<" not found in the list"<<endl;
}
void single_Linked_list::reverse()
{
struct node *ptr1, *ptr2, *ptr3;
if (startt == NULL)
{
cout<<"List is empty"<<endl;
return;
}
if (startt->nextt == NULL)
{
return;
}
ptr1 = startt;
ptr2 = ptr1->nextt;
ptr3 = ptr2->nextt;
ptr1->nextt = NULL;
ptr2->nextt = ptr1;
while (ptr3 != NULL)
{
ptr1 = ptr2;
ptr2 = ptr3;
ptr3 = ptr3->nextt;
ptr2->nextt = ptr1;
}
startt = ptr2;
}
void single_Linked_list::display()
{
struct node *temporary;
if (startt == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temporary = startt;
cout<<"Elements of list are: "<<endl;
while (temporary != NULL)
{
cout<<temporary->daata<<"->";
temporary = temporary->nextt;
}
cout<<"NULL"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.