Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

It needs to be a .cc file(C source file): Also if you can explain each part a li

ID: 669920 • Letter: I

Question

It needs to be a .cc file(C source file): Also if you can explain each part a little bit that would be great.

include "linked_list.h"

#include

namespace thilenius {
namespace external {
namespace csci2270 {
namespace assignments {
namespace linked_list {

// The constructor. It needs to set 'head' to 'nullptr'. There are 2 ways to do
// this in a constructor, you can use either.

LinkedList::LinkedList() {
  
   }

// Should return the size of the linked list. You'll need to walk the linked
// list and count nodes to do this.
int LinkedList::Size() { return -1; }

// Inserts an element at index (so that once inserted it is at that index) into
// the linked list. This is one of the more tricky ones. You'll need to talk the
// list until you find the index you want to insert at (maybe one before that,
// hint hint) and 'wire' in the new element. Whiteboard this one out!!
bool LinkedList::InsertAtIndex(int value, int index) {
std::cout << "Don't forget, you can use cout to debug things! The output "
"apears ABOVE the test output" << std::endl;
return true;
}

// Adds an element to the front of the linked list. If this one takes you more
// than one or two lines of code then you clearly didn't read the tips above :p
void LinkedList::PushFront(int value) {}

// Adds an element to the end of the linked list. If this one takes you more
// than one or two lines of code then you clearly didn't read the tips above :p
void LinkedList::PushBack(int value) {}

// This is the subscript operator. It allows the user of your code to access the
// nth element by just using the array operator, like this:
// std::cout << "The third element in the list is: " << my_linked_list[2];
// It takes in the index of the item the user wants to get at, and returns the
// item at that index (as a reference).
int& LinkedList::operator[](int index) {}

// Removes an item at the given index. This means you'll need to walk the list
// until you get to that index (or one before it, hint hint), and remove that
// item by 'wiring' the next_node to skip that item. You'll also need to call
// delete on that item to free the memory back to the OS.
bool LinkedList::RemoveAtIndex(int index) { return true; }

// Should remove all items from the list. If this one takes you more than a few
// lines of code then you clearly didn't read the tips above. Hint hint.
void LinkedList::Clear() {}

// Extra Credit (worth 5 points)
// Removes all occurrences of 'value' from the list. For example, if I have the
// linked list [0, 1, 1, 1, 2] and I call RemoveAll(1), it should remove the
// middle 3 items, leaving only [0, 2]
void LinkedList::RemoveAll(int value) {}

} // namespace linked_list
} // namespace assignments
} // namespace csci2270
} // namespace external
} // namespace thilenius

Explanation / Answer

#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
/*
* Node Declaration
*/
struct node
{
int info;
struct node *next;
}*start;

/*
* Class Declaration
*/
class single_node
{
public:
node* create_node(int);
void PushFront();
void insert_AtIndex();
void PushBack();
void Remove_AtIndex();
void sort();
void search();
void update();
void reverse();
void display();
single_node()
{
start = NULL;
}
};

main()
{
int choice, nodes, element, position, i;
single_node sl;
start = 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 position"<<endl;
cout<<"4.Delete a Particular Node"<<endl;
cout<<"5.Clear Linked List"<<endl;
cout<<"6.Display Linked List"<<endl;
cout<<"7.Size of Linked List"<<endl;
count<<"8.Removing all occurrences"<< endl;
cout<<"9.Exit "<<endl;
cout<<"Enter your choice : ";
cin>>choice;
switch(choice)
{
case 1:
cout<<"Inserting Node at Beginning: "<<endl;
sl.PushFront();
cout<<endl;
break;
case 2:
cout<<"Inserting Node at Last: "<<endl;
sl.PushBack();
cout<<endl;
break;
case 3:
cout<<"Inserting Node at a given position:"<<endl;
sl.insert_AtIndex();
cout<<endl;
break;
case 4:
cout<<"Delete a particular node: "<<endl;
sl.Remove_AtIndex();
break;

case 5:
cout<<"Clear Link List: "<<endl;
sl.Clear();
cout<<endl;
break;
case 6:
cout<<"Display elements of link list"<<endl;
sl.display();
cout<<endl;
break;
case 7:
cout<<"Size of Link List"<<endl;
sl.Size();
cout<<endl;
break;
case 8:
cout<<"Remove occurencces in Link List"<<endl;
sl.deleteNodesWithValue();
cout<<endl;
break;
case 9:
cout<<"Exiting..."<<endl;
exit(1);
break;
default:
cout<<"Wrong choice"<<endl;
}
}
}

/*
* Creating Node
*/
node *single_node::create_node(int value)
{
struct node *temp, *s;
temp = new(struct node);
if (temp == NULL)
{
cout<<"Memory not allocated "<<endl;
return 0;
}
else
{
temp->info = value;
temp->next = NULL;   
return temp;
}
}

/*
* Inserting element in beginning
*/
void single_node::PushFront()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *p;
temp = create_node(value);
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
p = start;
start = temp;
start->next = p;
}
cout<<"Element Inserted at beginning"<<endl;
}

/*
* Inserting Node at last
*/
void single_node::PushBack()
{
int value;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s->next != NULL)
{   
s = s->next;
}
temp->next = NULL;
s->next = temp;
cout<<"Element Inserted at last"<<endl;
}

/*
* Insertion of node at a given position
*/
void single_node::insert_AtIndex()
{
int value, pos, counter = 0;
cout<<"Enter the value to be inserted: ";
cin>>value;
struct node *temp, *s, *ptr;
temp = create_node(value);
cout<<"Enter the postion at which node to be inserted: ";
cin>>pos;
int i;
s = start;
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos == 1)
{
if (start == NULL)
{
start = temp;
start->next = NULL;
}
else
{
ptr = start;
start = temp;
start->next = ptr;
}
}
else if (pos > 1 && pos <= counter)
{
s = start;
for (i = 1; i < pos; i++)
{
ptr = s;
s = s->next;
}
ptr->next = temp;
temp->next = s;
}
else
{
cout<<"Positon out of range"<<endl;
}
}

void single_node::Remove_AtIndex()
{
int pos, i, counter = 0;
if (start == NULL)
{
cout<<"List is empty"<<endl;
return;
}
cout<<"Enter the position of value to be deleted: ";
cin>>pos;
struct node *s, *ptr;
s = start;
if (pos == 1)
{
start = s->next;
}
else
{
while (s != NULL)
{
s = s->next;
counter++;
}
if (pos > 0 && pos <= counter)
{
s = start;
for (i = 1;i < pos;i++)
{
ptr = s;
s = s->next;
}
ptr->next = s->next;
}
else
{
cout<<"Position out of range"<<endl;
}
free(s);
cout<<"Element Deleted"<<endl;
}
}

void LinkedList::Clear()
{
struct node *temp;
temp1= start->next;
while(temp1!=NULL)
{   
start->next = temp1->next;
temp1->next = NULL;
free(temp1);
temp1 = start->next;
}
}

void single_node::display()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
cout<<"Elements of list are: "<<endl;
while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
}
cout<<"NULL"<<endl;
}

int single_node::Size()
{
struct node *temp;
if (start == NULL)
{
cout<<"The List is Empty"<<endl;
return;
}
temp = start;
int count=0;

while (temp != NULL)
{
cout<<temp->info<<"->";
temp = temp->next;
count++;
}
cout<<"Size of list is: "<<count<< endl;
return count;
}

node* deleteNodesWithValue(int value, node* head)
{
node *head=NULL;
node *prev=NULL;
int size=Size();
  
if (!head)
{
return head;
}

and current node in the list*/
node* prev = NULL;
node* current = head;

while (current && current->data == value)
{
prev = current;
current = current->next;
head = current;
delete prev;
}
  
while (current)
{
if (current->data == value)
{
prev->next = current->next;
delete current;
current = prev->next;
}
else
{
prev = current;
current = current->next;
}
}
return head;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote