3 Linked List The following exercises deal with coding a LinkedList. The Linkedl
ID: 3751707 • Letter: 3
Question
3 Linked List The following exercises deal with coding a LinkedList. The Linkedlist is composed of generic Node objects The Linkedlist contains a NodecE>head referencing the first node in the list. The Linkedlist contains a NodecExtail referencing the last node in the list The LinkedList contains a method sizel) that returns the number of elements in the list The Node objects are doubly-linked, and contain public variables next and prev, which reference the next and previous nodes in the list respectively 12. (3 points) Write a method called deletelist(), which removes all the items in a list, making it empty //again, this is an instance method inside LinkedList / so you have access to head, tail (optionall, and the Node class public void deleteList) ( Page 7 of 14Explanation / Answer
struct node { int data; node *next; };
class list {
Private:
node *head, *tail;
public:
list()
{ head=NULL; tail=NULL; } };
void createnode(int value)
{
node *temp=new node;
temp->data=value;
temp->next=NULL;
if(head==NULL)
{
head=temp;
tail=temp;
temp=NULL;
}
else
{
tail->next=temp;
tail=temp;
}
}
void display()
{
node *temp=new node;
temp=head;
while(temp!=NULL)
{
cout<<temp->data<<" ";
temp=temp->next;
}
}
void insert_start(int value)
{
node *temp=new node;
temp->data=value;
temp->next=head;
head=temp;
}
void insert_position(int pos, int value)
{
node *pre=new node;
node *cur=new node;
node *temp=new node;
cur=head;
for(int i=1;i<pos;i++)
{
pre=cur;
cur=cur->next;
}
temp->data=value;
pre->next=temp;
temp->next=cur;
}
void delete_first()
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
void delete_last()
{
node *current=new node;
node *previous=new node;
current=head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
void delete_position(int pos)
{
node *current=new node;
node *previous=new node;
current=head;
for(int i=1;i<pos;i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
}
int size(struct Node* head)
{
int count = 0; // Initialize count
struct Node* current = head; // Initialize current
while (current != NULL)
{
count++;
current = current->next;
}
return count;
}
/* Function to delete the entire linked list */
public void deleteList(struct Node** head_ref)
{
/* deref head_ref to get the real head */
struct Node* current = *head_ref;
struct Node* next;
while (current != NULL)
{
next = current->next;
free(current);
current = next;
}
/* deref head_ref to affect the real head back
in the caller. */
*head_ref = NULL;
}
public int count(struct Node* head, int search_for)
{
struct Node* current = head;
int count = 0;
while (current != NULL)
{
if (current->data == search_for)
count++;
current = current->next;
}
return count;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.