Problem: Implement a C ++ template class for doubly linked circular lists with h
ID: 3571613 • Letter: P
Question
Problem: Implement a C ++ template class for doubly linked circular lists with head node. you must include a consturctor, and destructor as well as function to insert and delete. A bidirectional iterator must be included as well.(ps: Excerise 3 of chapter 4.10 Write the C++ function operator <<(), which outputs a sparse matrix as tri ples(I,j,air). The triples are to be output by rows and within row by columns. Show that this operation can be performed in time O(n+ra) if there are ra nonzero terms in the matrix. n is the number of rows in the matrix )
Part Il Programming Project 1. (Exercise 3 of Chapter 4.10) Implement a C++ template class for doubly linked circular lists with head node. You must include a constructor, copy constructor, and destructor as well as function to insert and delete. A bidirectional iterator must be included as well.
Explanation / Answer
#include<iostream>
#include<cstdio>
#include<cstdlib>
using namespace std;
struct node
{
int info;
struct node *next;
struct node *prev;
}*start;
class double_llist
{
public:
void create_linkedlist(int value);
void beginelem(int value);
void afterelem(int value, int position);
void delete_element(int value);
double_llist()
{
start = NULL;
}
};
void double_llist::create_linkedlist(int value)
{
struct node *s, *temp;
temp = new(struct node);
temp->info = value;
temp->next = NULL;
if (start == NULL)
{
temp->prev = NULL;
start = temp;
}
else
{
s = start;
while (s->next != NULL)
s = s->next;
s->next = temp;
temp->prev = s;
}
}
void double_llist::beginelem(int value)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *temp;
temp = new(struct node);
temp->prev = NULL;
temp->info = value;
temp->next = start;
start->prev = temp;
start = temp;
cout<<"Element Inserted"<<endl;
}
void double_llist::afterelem(int value, int pos)
{
if (start == NULL)
{
cout<<"First Create the list."<<endl;
return;
}
struct node *tmp, *q;
int i;
q = start;
for (i = 0;i < pos - 1;i++)
{
q = q->next;
if (q == NULL)
{
cout<<"There are less than ";
cout<<pos<<" elements."<<endl;
return;
}
}
tmp = new(struct node);
tmp->info = value;
if (q->next == NULL)
{
q->next = tmp;
tmp->next = NULL;
tmp->prev = q;
}
else
{
tmp->next = q->next;
tmp->next->prev = tmp;
q->next = tmp;
tmp->prev = q;
}
cout<<"Element Inserted"<<endl;
}
void double_llist::delete_element(int value)
{
struct node *tmp, *q;
if (start->info == value)
{
tmp = start;
start = start->next;
start->prev = NULL;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = start;
while (q->next->next != NULL)
{
if (q->next->info == value)
{
tmp = q->next;
q->next = tmp->next;
tmp->next->prev = q;
cout<<"Element Deleted"<<endl;
free(tmp);
return;
}
q = q->next;
}
if (q->next->info == value)
{
tmp = q->next;
free(tmp);
q->next = NULL;
cout<<"Element Deleted"<<endl;
return;
}
cout<<"Element "<<value<<" not found"<<endl;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.