The objective of this assignment is to implement several basic methods for a sin
ID: 3607744 • Letter: T
Question
The objective of this assignment is to implement several basic methods for a singly linked list. I have provided declaration files for the Node and LinkedList classes. ( see node.h and linkedlist.h ).
Implement the following LinkedList methods in linkedlist.cpp:
void add ( int N ) Add N to the front of the linked list.
bool remove ( int N ) Remove the first instance of N from the list, then return true. If the N was not found, return false.
int find ( int N ) Find the first instance of N in the list and return its index. Return -1 if N was not found.
int count ( int N ) Return a count of the instances of N in the list.
int at ( int N ) Return the value stored in the node at index N.
int len() Return the current length of the list.
You can use main.cpp for your own internal testing. Make sure you do not alter the order of elements in the list.
IMPORTANT: Do not submit your code at this time. The unit tests are not working yet.
inkedlist.cpp file
Current file: main.cpp 1 #include 2 #include "linkedlist.h" 3 using namespace std; 4 5 int main() 7Test your implementation here *Explanation / Answer
#include<iostream>
#include<stdlib.h>
using namespace std;
class NODE
{
int data;
NODE *next;
public:
NODE* Creation(NODE*head);
void ADD_Begining(NODE*head);
void ADD_END(NODE*head);
void ADD_INBETWEEN(NODE*head);
void disp(NODE*head);
void remove(NODE*head);
void search(NODE*head);
void reverse(NODE*head);
};
NODE* NODE::Creation(NODE*head)
{
head=new NODE;
head->data=0;
head->next=NULL;
disp(head);
return(head);
}
void NODE:: ADD_Begining(NODE*head)
{
NODE*nw;
nw=new NODE;
cout<<"Enter data for new NODE :"; cin>>nw->data;
nw->next=NULL;
nw->next=head->next;
head->next=nw;
head->data++;
disp(head);
}
void NODE:: ADD_END(NODE*head)
{
NODE*nw,*temp;
nw=new NODE;
cout<<"Enter data for new NODE :"; cin>>nw->data;
nw->next=NULL;
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=nw;
head->data++;
disp(head);
}
void NODE:: ADD_INBETWEEN(NODE*head)
{
NODE*nw,*temp;
int i,loc;
nw=new NODE;
cout<<"Enter data for new NODE :"; cin>>nw->data;
nw->next=NULL;
cout<<"Enter the Location :"; cin>>loc;
temp=head;
i=1;
while(i!=loc)
{
temp=temp->next;
i++;
}
nw->next=temp->next;
temp->next=nw;
head->data++;
disp(head);
}
void NODE:: disp(NODE*head)
{
NODE*temp;
temp=head;
cout<<"Linked List : Head-->";
temp=head;
while(temp->next!=NULL)
{
cout<<temp->data<<"->";
temp=temp->next;
}
cout<<temp->data<<"->NULL";
}
void NODE:: remove(NODE*head)
{
NODE*temp,*t1;
int target;
cout<<"Enter data to delete :"; cin>>target;
temp=head;
while(temp->next!=NULL)
{
if(temp->next->data==target)
break;
else
temp=temp->next;
}
if(temp->next==NULL)
cout<<"NODE is not Found";
else
{
t1=temp->next;
temp->next=t1->next;
t1->next=NULL;
delete(t1);
}
head->data--;
disp(head);
}
void NODE:: search(NODE*head)
{
NODE*temp;
int target,loc=1;
cout<<"Enter data for search operation :"; cin>>target;
temp=head;
while(temp->next!=NULL)
{
if(temp->next->data==target)
{
cout<<"NODE is Found at "<<loc<<" Location";
break;
}
else
{
temp=temp->next;
loc++;
}
}
if(temp->next==NULL)
cout<<"No NODE is Found...";
}
void NODE:: reverse(NODE*head)
{
NODE*p,*q,*r;
p=head->next;
q=NULL;
while(p!=NULL)
{
r=q;
q=p;
p=p->next;
q->next=r;
}
head->next=q;
disp(head);
}
int main()
{
NODE*head,n;
head=NULL;
int ch;
while(1)
{
cout<<" -------------------MENUS--------------------- 1.Create 2.InsertAtBegin 3.InsertAtEnd 4.InsertInBet 5.Remove 6.Search 7.Reverse 8.Exit ENTER UR CHOICE :";
cin>>ch;
switch(ch)
{
case 1:
head=n.Creation(head); break;
case 2:
n.ADD_Begining(head); break;
case 3:
n.ADD_END(head); break;
case 4:
n.ADD_INBETWEEN(head); break;
case 5:
n.remove(head); break;
case 6:
n.search(head); break;
case 7:
n.reverse(head); break;
case 8:
exit(0);
}
}return 0;
}
//Here is the code for the operations you can perform on a linked list.
//I have added few more operations for you with required operations.
//You can get length of linkedlist easily by printing the head element, which stores the length of list.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.