Can someone explain me AddAtHead,AddAtTail And AddInOrder functions #include <iostream> #include <fstream> #include <ctime> #include <cstdlib> using namespace std; template <class T> class DNode{ public: T data; DNode *next,*prev; DNode(){ next=prev=NULL;} DNode(T d, DNode *n=NULL, DNode *p=NULL){ data=d; next=n; prev=p;} }; template <class T> ostream& operator<<(ostream &out, const DNode<T> &n){ out<<n.data<<' '; return out; } template <class T> class HCDLL{ DNode<T> *head; public: HCDLL(){ head=new DNode<T>; head->next=head->prev=head;} void addAtHead(T d){ head->next=head->next->prev=new DNode<T>(d, head->next, head); } void addAtTail(T d){ head->prev=head->prev->next=new DNode<T>(d, head, head->prev); } void addInOrder(T d){ DNode<T> *t; for (t=head->next;t!=head && t->data<d;t=t->next); t->prev=t->prev->next=new DNode<T>(d, t, t->prev); } void show(){ for (DNode<T> *t=head->next;t!=head;t=t->next) cout<<*t<<' '; cout<<'\n'; } }; int main() { //srand((unsigned int)time(0)); HCDLL <int> list; list.addAtHead(5); list.addAtHead(3); list.addAtHead(1); list.show(); list.addAtTail(7); list.addAtTail(9); list.show(); list.addInOrder(8); list.addInOrder(10); list.show(); return 0; }
Doubly Circular Linked List (CDLL)
Two consecutive elements are linked by previous and next pointer and the last node points to first node by next pointer and also the previous pointer of the head node points to the tail node.
Lets see some basic operation on this data structure
Insertion at the beginning of the list: To insert a node at the beginning of the list, create a node(Say T) with data = 5, T next pointer points to first node of the list, T previous pointer points to last node the list, last node’s next pointer points to this T node, first node’s previous pointer also points this T node
Insertion in between the nodes of the list: To insert a node in between the list, two data values are required one after which new node will be inserted and another is the data of the new node.
Method |
Operation |
addAtHead |
Insertion at the beginning of the list |
addAtTail |
Insertion at the end of the list |
addInOrder |
Insertion in between the nodes of the list First find correct position and insert |
I have added and simplified the program with comments for better understanding
#include <iostream>
#include <fstream>
#include <ctime>
#include <cstdlib>
using namespace std;
template <class T>
class DNode{
public:
T data;
DNode *next,*prev;
DNode()
{
next=prev=NULL;
}
DNode(T d, DNode *n=NULL, DNode *p=NULL)
{
data=d;
next=n;
prev=p;
}
};
template <class T>
ostream& operator<<(ostream &out, const
DNode<T> &n){
out<<n.data<<' ';
return out;
}
template <class T>
class HCDLL{
DNode<T> *head;
public:
HCDLL()
{
head=new DNode<T>;
//head->next = head->prev =
head;
//simplifying the above one line
statment in multiline
head->prev = head;
head->next =
head->prev;
}
void addAtHead(T d){
//head->next=head->next->prev=new DNode<T>(d,
head->next, head);
//simplifying the above
one line statment in multiline
/*
Special
attentation to this function
new
DNode<T>(d, head->next, head);
it says new node
with data d and its next pointer head->next and prevois pointer
head
i.e. 1st
agrument it data 2nd is its next and 3rd is it prevois
pointer
and newly added
node in head now
*/
/*
new DNode<T>(d, head->next, head);
*/
DNode<T> *temp = new
DNode<T>(d, head->next, head);
head->next->prev =
temp;
head->next =
head->next->prev;
}
void addAtTail(T d)
{
//head->prev =
head->prev->next = new DNode<T>(d, head,
head->prev);
//simplifying the above
one line statment in multiline
head->prev->next =
new DNode<T>(d, head, head->prev);
head->prev =
head->prev->next;
}
void addInOrder(T d)
{
DNode<T> *t;
for
(t=head->next;t!=head &&
t->data<d;t=t->next);
//loop is doing nothing
see semicolon at end
//this find the correct
posiotn to insert
//t->prev=t->prev->next=new DNode<T>(d, t,
t->prev);
//simplifying the above one line
statment in multiline
t->prev->next=new
DNode<T>(d, t, t->prev);
t->prev=t->prev->next;
}
void show(){
for (DNode<T>
*t=head->next;t!=head;t=t->next)
cout<<*t<<' ';
//end of loop
cout<<'\n';
}
};
int main() {
//srand((unsigned int)time(0));
HCDLL <int> list;
list.addAtHead(5);
list.addAtHead(3);
list.addAtHead(1);
list.show();
list.addAtTail(7);
list.addAtTail(9);
list.show();
list.addInOrder(8);
list.addInOrder(10);
list.show();
return 0;
}
comment if doubt
Can someone explain me AddAtHead,AddAtTail And AddInOrder functions #include <iostream> #include <fstream> #include <ctime> #include <cstdlib>...
Getting this error. [Error] expected ';' after class definition #include <iostream> #include <cstdlib> #include <ctime> #include <conio.h> using namespace std; int sum=0; class Coin { int sideUp; public: Coin(int value) { toss(value); }; void toss(int value); } void Coin::toss(int value) { if(rand() %100<=50) { sum+=value; cout<<"\n You got a head. Value "<<value<<" added!!"; } else { cout<<"\n You got a tail. No value added"; } } int main() { int temp; cout<<"\n Quarter = 25 cents\n Dime = 10 cents...
Rewrite this code in Java please #include <iostream> #include <fstream> #include <cstdlib> #include <ctime> using namespace std; long length = 1000; const long max_length = 100000; int list[max_length]; void read() { ifstream fin("random.dat", ios::binary); for (long i = 0; i < length; i++) { fin.read((char*)&list[i], sizeof(int)); } fin.close(); } void bubbleSort() { int temp; for(long i = 0; i < length; i++) { for(long j = 0; j< length-i-1; j++)...
c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node { T data;//data field Node * next;//link field Node(T data) { this->data = data; } }; template<class T> class linked_list{ private: Node<T> *head,*current; public: linked_list(){//constructor, empty linked list head = NULL; current = NULL; } ~linked_list(){ current = head; while(current != NULL) { ...
Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: typedef int data_t; node *next; data_t data; node(data_t d) { next = NULL; data = d; } }; class linked_list { private: node *head; public: linked_list() { head = NULL; } // Get the...
Find and fix errors #include <iostream> #include <cstdlib> #include <ctime> using namespace std; const int MIN = 1; const int MAX = 10; int getRandom(int low, int high); int main() { int random_num = 0; int player_num; int tries; int seed = static_cast<int>(time(0)); bool guessed = false; srand(seed); // call the getRandom function below tries = 4; while ( tries > 0 && !guessed ) { cout << "Enter a number within the range 1 to...
moviestruct.cpp #include <iostream> #include <fstream> #include <cstdlib> #include <ostream> #include <fstream> #include <cstdlib> #include <cstring> using namespace std; typedef struct{ int id; char title[250]; int year; char rating[6]; int totalCopies; int rentedCopies; }movie; int loadData(ifstream &infile, movie movies[]); void printAll(movie movies[], int count); void printRated(movie movies[], int count); void printTitled(movie movies[], int count); void addMovie(movie movies[],int &count); void returnMovie(movie movies[],int count); void rentMovie(movie movies[],int count); void saveToFile(movie movies[], int count, char *filename); void printMovie(movie &m); int find(movie movies[], int...
The goal is to simulate the C heap manager in C++ use -std=c++11 to compile ONLY EDIT THE MemoryManager.cpp file in the sections marked in bold please and thank you • A runtime module used to allocate and de-allocate dynamic memory . • The "heap" is a large "pool" of memory set aside by the runtime system • The two main functions are – malloc, used to satisfy a request for a specific number of consecutive blocks; – free, used...
Hello, I have this code but its not running like it should: #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: typedef int data_t; node *next; data_t data; node(data_t d) { next = NULL; data = d; } }; class linked_list { private: node *head; public: linked_list() { head = NULL; } int size() { node *tptr = head; int c = 0; while (tptr) { c++; tptr = tptr->next; } return c; } void add(int...
#include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node { Contact item; NodePtr next; friend class ContactList; }; class ContactList { public: ContactList(char* clfile) ; ~ContactList(); void display (ostream & output) const; int insert (Contact record_to_insert); int insert (ContactList contact_list); int remove (Contact record_to_delete); int size () const; int save () const; void find_by_lname (ostream & output, string lname) const; void...
in c++ please include all of the following " template class, template function, singly linked list, the ADT stack, copy constructor, operator overloading, "try catch"and pointers Modify the code named "Stack using a Singly Linked List" to make the ADT Stack that is a template class has the code of the destructor in which each node is directly deleted without using any member function. As each node is deleted, the destructor displays the address of the node that is being...