SlideShare a Scribd company logo
Data Structures in C++
I am really new to C++, so links are really hard topic for me. It would be nice if you can provide
explanations of what doubly linked lists are and of some of you steps... Thank you
In this assignment, you will implement a doubly-linked list class, together with some list
operations. To make things easier, you’ll implement a list of int, rather than a template class.
Solution
A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a.
Now, if we have collection of integers, we need some representation to identify them. We call it
array. For ex: int arr[5]
This array is nothing but a Data Structure.
So, a Data Structure is a way to group the data.
There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack,
Queue, etc.
Doubly-Linked list are the ones where you can traverse from the current node both in left and
right directions.
Why so many different types of Data Structures are required ?
Answer is very simple, grouping of data, storage of data and accessing the data is different.
For example, in case of Arrays we store all the data in contiguous locations.
What if we are not able to store the data in contiguous locations because we have huge data.
Answer is go for Linked List/Doubly-Linked list.
Here we can store the data anywhere and link the data through pointers.
I will try to provide comments for the code you have given. May be this can help you.
#pragma once
/*
dlist.h
Doubly-linked lists of ints
*/
#include
class dlist {
public:
dlist() { }
// Here we are creating a NODE, it has a integer value and two pointers.
// One pointer is to move to next node and other to go back to previous node.
struct node {
int value;
node* next;
node* prev;
};
// To return head pointer, i.e. start of the Doubly-Linked list.
node* head() const { return _head; }
// To return Tail pointer, i.e. end of the Doubly-Linked list.
node* tail() const { return _tail; }
// **** Implement ALL the following methods ****
// Returns the node at a particular index (0 is the head).
node* at(int index){
int cnt = 0;
struct node* tmp = head();
while(tmp!=NULL)
{
if (cnt+1 == index)
return tmp;
tmp = tmp->next;
}
}
// Insert a new value, after an existing one
void insert(node *previous, int value){
// check if the given previous is NULL
if (previous == NULL)
{
printf("the given previous node cannot be NULL");
return;
}
// allocate new node
struct node* new_node =(struct node*) malloc(sizeof(struct node));
// put in the data
new_node->data = new_data;
// Make next of new node as next of previous
new_node->next = previous->next;
// Make the next of previous as new_node
previous->next = new_node;
// Make previous as previous of new_node
new_node->prev = previous;
// Change previous of new_node's next node
if (new_node->next != NULL)
new_node->next->prev = new_node;
}
// Delete the given node
void del(node* which){
struct node* head_ref = head();
/* base case */
if(*head_ref == NULL || which == NULL)
return;
/* If node to be deleted is head node */
if(*head_ref == which)
*head_ref = which->next;
/* Change next only if node to be deleted is NOT the last node */
if(which->next != NULL)
which->next->prev = which->prev;
/* Change prev only if node to be deleted is NOT the first node */
if(which->prev != NULL)
which->prev->next = which->next;
/* Finally, free the memory occupied by which*/
free(which);
return;
}
// Add a new element to the *end* of the list
void push_back(int value){
// allocate node
struct node* new_node = (struct node*) malloc(sizeof(struct node));
struct node* head_ref = head();
struct node *last = *head_ref;
// put in the data
new_node->data = value;
// This new node is going to be the last node, so make next of it as NULL
new_node->next = NULL;
// If the Linked List is empty, then make the new node as head
if (*head_ref == NULL)
{
new_node->prev = NULL;
*head_ref = new_node;
return;
}
// Else traverse till the last node.
while (last->next != NULL)
last = last->next;
// Change the next of last node
last->next = new_node;
// Make last node as previous of new node
new_node->prev = last;
return;
}
// Add a new element to the *beginning* of the list
void push_front(int value){
//allocate node
struct node* new_node = (struct node*) malloc(sizeof(struct node));
//put in the data
new_node->data = value;
// Make next of new node as head and previous as NULL
struct node* head_ref = head();
new_node->next = (*head_ref);
new_node->prev = NULL;
//change prev of head node to new node
if((*head_ref) != NULL)
(*head_ref)->prev = new_node ;
// move the head to point to the new node
(*head_ref) = new_node;
}
// Remove the first element
void pop_front(){
struct node *toDelete;
struct node *head = head();
if(head == NULL)
{
printf("Unable to delete. List is empty. ");
}
else
{
toDelete = head;
head = head->next; //Move head pointer to 2 node
head->prev = NULL; //Remove the link to previous node
free(toDelete); //Delete the first node from memory
printf("SUCCESSFULLY DELETED NODE FROM BEGINNING OF THE LIST. ");
}
}
// Remove the last element
void pop_back(){
struct node * toDelete;
struct node *last = head();
while(last.next!=NULL)
{
last = last->next;
}
if(last == NULL)
{
printf("Unable to delete. List is empty. ");
}
else
{
toDelete = last;
last = last->prev; //Move last pointer to 2nd last node
last->next = NULL; //Remove link to of 2nd last node with last node
free(toDelete); //Delete the last node
printf("SUCCESSFULLY DELETED NODE FROM END OF THE LIST. ");
}
}
// Get the size of the list
int size(){
int cnt = 0;
struct node* head_ref = head();
while(head_ref!=NULL)
{
cnt++;
head_ref = head_ref->next;
}
return cnt;
}
// Returns true if the list is empty
bool empty(){
struct node* head_ref = head();
if(head_ref == NULL)
return true;
else
return false;
}
private:
node* _head = nullptr;
node* _tail = nullptr;
};
// **** Implement ALL the following functions ****
/* out << l
Prints a list to the ostream out. This is mostly for your convenience in
testing your code; it's much easier to figure out what's going on if you
can easily print out lists!
*/
std::ostream& operator<< (std::ostream& out, dlist& l);
/* a == b
Compares two lists for equality, returning true if they have the same
elements in the same positions. (Hint: it is *not* enough to just compare
pointers! You have to compare the values stored in the nodes.)
*/
bool operator== (dlist& a, dlist& b);
/* a + b
Returns a new list consisting of all the elements of a, followed by all the
elements of b (i.e., the list concatenation).
*/
dlist operator+ (dlist& a, dlist& b);
/* reverse(l)
Returns a new list that is the *reversal* of l; that is, a new list
containing the same elements as l but in the reverse order.
*/
dlist reverse(dlist& l);

More Related Content

Similar to Data Structures in C++I am really new to C++, so links are really .pdf

Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
AravindAnand21
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
Programming Homework Help
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
arjunenterprises1978
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
gilliandunce53776
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
kingsandqueens3
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
Programming Exam Help
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
poblettesedanoree498
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
fathimahardwareelect
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
Himadri Sen Gupta
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
vasavim9
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
Masud Parvaze
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
shalins6
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
Mani .S (Specialization in Semantic Web)
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
feelinggift
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
deepak596396
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
JUSTSTYLISH3B2MOHALI
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
ssuser0be977
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
DaniyalAli81
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
sales98
 

Similar to Data Structures in C++I am really new to C++, so links are really .pdf (20)

Unit - 2.pdf
Unit - 2.pdfUnit - 2.pdf
Unit - 2.pdf
 
C Homework Help
C Homework HelpC Homework Help
C Homework Help
 
C++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdfC++Write a method Node Nodereverse() which reverses a list..pdf
C++Write a method Node Nodereverse() which reverses a list..pdf
 
Below is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docxBelow is a depiction of a doubly-linked list implementation of the bag.docx
Below is a depiction of a doubly-linked list implementation of the bag.docx
 
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdfProblem- Describe an algorithm for concatenating two singly linked lis.pdf
Problem- Describe an algorithm for concatenating two singly linked lis.pdf
 
C Exam Help
C Exam Help C Exam Help
C Exam Help
 
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdfC++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
C++ Doubly-Linked ListsThe goal of the exercise is to implement a.pdf
 
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdfTHE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
THE CODE HAS A SEGMENTATION FAULT BUT I CANNOT FIND OUT WHERE. NEED .pdf
 
17 linkedlist (1)
17 linkedlist (1)17 linkedlist (1)
17 linkedlist (1)
 
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjhlinked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
linked List.docx vhjgvjhvgjhjhbbjkhkjhkjh
 
Linkedlist
LinkedlistLinkedlist
Linkedlist
 
Use C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdfUse C++ Write a function to merge two doubly linked lists. The input.pdf
Use C++ Write a function to merge two doubly linked lists. The input.pdf
 
Unit II Data Structure 2hr topic - List - Operations.pptx
Unit II  Data Structure 2hr topic - List - Operations.pptxUnit II  Data Structure 2hr topic - List - Operations.pptx
Unit II Data Structure 2hr topic - List - Operations.pptx
 
How do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdfHow do you stop infinite loop Because I believe that it is making a.pdf
How do you stop infinite loop Because I believe that it is making a.pdf
 
Algo>ADT list & linked list
Algo>ADT list & linked listAlgo>ADT list & linked list
Algo>ADT list & linked list
 
Describe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdfDescribe an algorithm for concatenating two singly linked lists L and.pdf
Describe an algorithm for concatenating two singly linked lists L and.pdf
 
Write a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdfWrite a program that accepts an arithmetic expression of unsigned in.pdf
Write a program that accepts an arithmetic expression of unsigned in.pdf
 
dynamicList.ppt
dynamicList.pptdynamicList.ppt
dynamicList.ppt
 
DSA(1).pptx
DSA(1).pptxDSA(1).pptx
DSA(1).pptx
 
Consider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdfConsider a double-linked linked list implementation with the followin.pdf
Consider a double-linked linked list implementation with the followin.pdf
 

More from rohit219406

Below are the transactions and adjustments that occurred during the .pdf
Below are the transactions and adjustments that occurred during the .pdfBelow are the transactions and adjustments that occurred during the .pdf
Below are the transactions and adjustments that occurred during the .pdf
rohit219406
 
Help Please. Results not given Explain how your results for the pe.pdf
Help Please. Results not given Explain how your results for the pe.pdfHelp Please. Results not given Explain how your results for the pe.pdf
Help Please. Results not given Explain how your results for the pe.pdf
rohit219406
 
As a software developer you have been delegated with the assignment .pdf
As a software developer you have been delegated with the assignment .pdfAs a software developer you have been delegated with the assignment .pdf
As a software developer you have been delegated with the assignment .pdf
rohit219406
 
What scientist is credited with proposing the equivalency of mass and.pdf
What scientist is credited with proposing the equivalency of mass and.pdfWhat scientist is credited with proposing the equivalency of mass and.pdf
What scientist is credited with proposing the equivalency of mass and.pdf
rohit219406
 
Why do countries with high GNI and GDP are attractive for foreign in.pdf
Why do countries with high GNI and GDP are attractive for foreign in.pdfWhy do countries with high GNI and GDP are attractive for foreign in.pdf
Why do countries with high GNI and GDP are attractive for foreign in.pdf
rohit219406
 
Which of the following is NOT true about the ESCBA.It acts as an.pdf
Which of the following is NOT true about the ESCBA.It acts as an.pdfWhich of the following is NOT true about the ESCBA.It acts as an.pdf
Which of the following is NOT true about the ESCBA.It acts as an.pdf
rohit219406
 
What is 4 -4 + infinity - infinity As x approaches 3 from th.pdf
What is  4  -4  + infinity  - infinity  As x approaches 3 from th.pdfWhat is  4  -4  + infinity  - infinity  As x approaches 3 from th.pdf
What is 4 -4 + infinity - infinity As x approaches 3 from th.pdf
rohit219406
 
Use what you have learned so far to bring variety in your writing. U.pdf
Use what you have learned so far to bring variety in your writing. U.pdfUse what you have learned so far to bring variety in your writing. U.pdf
Use what you have learned so far to bring variety in your writing. U.pdf
rohit219406
 
Use C programmingMake sure everything works only uploadSol.pdf
Use C programmingMake sure everything works only uploadSol.pdfUse C programmingMake sure everything works only uploadSol.pdf
Use C programmingMake sure everything works only uploadSol.pdf
rohit219406
 
Thoroughly describe the molecular underpinnings of ONE and only one.pdf
Thoroughly describe the molecular underpinnings of ONE and only one.pdfThoroughly describe the molecular underpinnings of ONE and only one.pdf
Thoroughly describe the molecular underpinnings of ONE and only one.pdf
rohit219406
 
The Blackbeard Company Ltd provided the following information in reg.pdf
The Blackbeard Company Ltd provided the following information in reg.pdfThe Blackbeard Company Ltd provided the following information in reg.pdf
The Blackbeard Company Ltd provided the following information in reg.pdf
rohit219406
 
Thank you 1. What is responsible for anteriorposterior axis formati.pdf
Thank you 1. What is responsible for anteriorposterior axis formati.pdfThank you 1. What is responsible for anteriorposterior axis formati.pdf
Thank you 1. What is responsible for anteriorposterior axis formati.pdf
rohit219406
 
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdfTake the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
rohit219406
 
Systems analysis project 10 can you answer the 4 questions at the t.pdf
Systems analysis project 10 can you answer the 4 questions at the t.pdfSystems analysis project 10 can you answer the 4 questions at the t.pdf
Systems analysis project 10 can you answer the 4 questions at the t.pdf
rohit219406
 
Step 1. Read critically and analyze the following scenarioGeraldi.pdf
Step 1. Read critically and analyze the following scenarioGeraldi.pdfStep 1. Read critically and analyze the following scenarioGeraldi.pdf
Step 1. Read critically and analyze the following scenarioGeraldi.pdf
rohit219406
 
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdf
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdfQuestion 3 2 pts In response to the Great Recession, the Federal Rese.pdf
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdf
rohit219406
 
Prove that the T_i -property is a topological property for i = 0S.pdf
Prove that the T_i -property is a topological property for i = 0S.pdfProve that the T_i -property is a topological property for i = 0S.pdf
Prove that the T_i -property is a topological property for i = 0S.pdf
rohit219406
 
Q4.14. Which of the following species is most likely to exhibit pate.pdf
Q4.14. Which of the following species is most likely to exhibit pate.pdfQ4.14. Which of the following species is most likely to exhibit pate.pdf
Q4.14. Which of the following species is most likely to exhibit pate.pdf
rohit219406
 
Prepare a classified balance sheet. Do not show the components that .pdf
Prepare a classified balance sheet. Do not show the components that .pdfPrepare a classified balance sheet. Do not show the components that .pdf
Prepare a classified balance sheet. Do not show the components that .pdf
rohit219406
 
Microscopes and telescopes both consist of two converging lenses cont.pdf
Microscopes and telescopes both consist of two converging lenses cont.pdfMicroscopes and telescopes both consist of two converging lenses cont.pdf
Microscopes and telescopes both consist of two converging lenses cont.pdf
rohit219406
 

More from rohit219406 (20)

Below are the transactions and adjustments that occurred during the .pdf
Below are the transactions and adjustments that occurred during the .pdfBelow are the transactions and adjustments that occurred during the .pdf
Below are the transactions and adjustments that occurred during the .pdf
 
Help Please. Results not given Explain how your results for the pe.pdf
Help Please. Results not given Explain how your results for the pe.pdfHelp Please. Results not given Explain how your results for the pe.pdf
Help Please. Results not given Explain how your results for the pe.pdf
 
As a software developer you have been delegated with the assignment .pdf
As a software developer you have been delegated with the assignment .pdfAs a software developer you have been delegated with the assignment .pdf
As a software developer you have been delegated with the assignment .pdf
 
What scientist is credited with proposing the equivalency of mass and.pdf
What scientist is credited with proposing the equivalency of mass and.pdfWhat scientist is credited with proposing the equivalency of mass and.pdf
What scientist is credited with proposing the equivalency of mass and.pdf
 
Why do countries with high GNI and GDP are attractive for foreign in.pdf
Why do countries with high GNI and GDP are attractive for foreign in.pdfWhy do countries with high GNI and GDP are attractive for foreign in.pdf
Why do countries with high GNI and GDP are attractive for foreign in.pdf
 
Which of the following is NOT true about the ESCBA.It acts as an.pdf
Which of the following is NOT true about the ESCBA.It acts as an.pdfWhich of the following is NOT true about the ESCBA.It acts as an.pdf
Which of the following is NOT true about the ESCBA.It acts as an.pdf
 
What is 4 -4 + infinity - infinity As x approaches 3 from th.pdf
What is  4  -4  + infinity  - infinity  As x approaches 3 from th.pdfWhat is  4  -4  + infinity  - infinity  As x approaches 3 from th.pdf
What is 4 -4 + infinity - infinity As x approaches 3 from th.pdf
 
Use what you have learned so far to bring variety in your writing. U.pdf
Use what you have learned so far to bring variety in your writing. U.pdfUse what you have learned so far to bring variety in your writing. U.pdf
Use what you have learned so far to bring variety in your writing. U.pdf
 
Use C programmingMake sure everything works only uploadSol.pdf
Use C programmingMake sure everything works only uploadSol.pdfUse C programmingMake sure everything works only uploadSol.pdf
Use C programmingMake sure everything works only uploadSol.pdf
 
Thoroughly describe the molecular underpinnings of ONE and only one.pdf
Thoroughly describe the molecular underpinnings of ONE and only one.pdfThoroughly describe the molecular underpinnings of ONE and only one.pdf
Thoroughly describe the molecular underpinnings of ONE and only one.pdf
 
The Blackbeard Company Ltd provided the following information in reg.pdf
The Blackbeard Company Ltd provided the following information in reg.pdfThe Blackbeard Company Ltd provided the following information in reg.pdf
The Blackbeard Company Ltd provided the following information in reg.pdf
 
Thank you 1. What is responsible for anteriorposterior axis formati.pdf
Thank you 1. What is responsible for anteriorposterior axis formati.pdfThank you 1. What is responsible for anteriorposterior axis formati.pdf
Thank you 1. What is responsible for anteriorposterior axis formati.pdf
 
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdfTake the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
Take the basic Hardy-Weinberg Equilibrium equation, where there are a.pdf
 
Systems analysis project 10 can you answer the 4 questions at the t.pdf
Systems analysis project 10 can you answer the 4 questions at the t.pdfSystems analysis project 10 can you answer the 4 questions at the t.pdf
Systems analysis project 10 can you answer the 4 questions at the t.pdf
 
Step 1. Read critically and analyze the following scenarioGeraldi.pdf
Step 1. Read critically and analyze the following scenarioGeraldi.pdfStep 1. Read critically and analyze the following scenarioGeraldi.pdf
Step 1. Read critically and analyze the following scenarioGeraldi.pdf
 
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdf
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdfQuestion 3 2 pts In response to the Great Recession, the Federal Rese.pdf
Question 3 2 pts In response to the Great Recession, the Federal Rese.pdf
 
Prove that the T_i -property is a topological property for i = 0S.pdf
Prove that the T_i -property is a topological property for i = 0S.pdfProve that the T_i -property is a topological property for i = 0S.pdf
Prove that the T_i -property is a topological property for i = 0S.pdf
 
Q4.14. Which of the following species is most likely to exhibit pate.pdf
Q4.14. Which of the following species is most likely to exhibit pate.pdfQ4.14. Which of the following species is most likely to exhibit pate.pdf
Q4.14. Which of the following species is most likely to exhibit pate.pdf
 
Prepare a classified balance sheet. Do not show the components that .pdf
Prepare a classified balance sheet. Do not show the components that .pdfPrepare a classified balance sheet. Do not show the components that .pdf
Prepare a classified balance sheet. Do not show the components that .pdf
 
Microscopes and telescopes both consist of two converging lenses cont.pdf
Microscopes and telescopes both consist of two converging lenses cont.pdfMicroscopes and telescopes both consist of two converging lenses cont.pdf
Microscopes and telescopes both consist of two converging lenses cont.pdf
 

Recently uploaded

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
Delapenabediema
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
MIRIAMSALINAS13
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
Vivekanand Anglo Vedic Academy
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
Celine George
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
Vivekanand Anglo Vedic Academy
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
bennyroshan06
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
kaushalkr1407
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
Col Mukteshwar Prasad
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
Thiyagu K
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
AzmatAli747758
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
joachimlavalley1
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
Mohd Adib Abd Muin, Senior Lecturer at Universiti Utara Malaysia
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
EugeneSaldivar
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Thiyagu K
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
EduSkills OECD
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
RaedMohamed3
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
Celine George
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
Ashokrao Mane college of Pharmacy Peth-Vadgaon
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
Tamralipta Mahavidyalaya
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
Jisc
 

Recently uploaded (20)

The Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official PublicationThe Challenger.pdf DNHS Official Publication
The Challenger.pdf DNHS Official Publication
 
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXXPhrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
Phrasal Verbs.XXXXXXXXXXXXXXXXXXXXXXXXXX
 
Sectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdfSectors of the Indian Economy - Class 10 Study Notes pdf
Sectors of the Indian Economy - Class 10 Study Notes pdf
 
How to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS ModuleHow to Split Bills in the Odoo 17 POS Module
How to Split Bills in the Odoo 17 POS Module
 
The French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free downloadThe French Revolution Class 9 Study Material pdf free download
The French Revolution Class 9 Study Material pdf free download
 
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptxMARUTI SUZUKI- A Successful Joint Venture in India.pptx
MARUTI SUZUKI- A Successful Joint Venture in India.pptx
 
The Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdfThe Roman Empire A Historical Colossus.pdf
The Roman Empire A Historical Colossus.pdf
 
How to Break the cycle of negative Thoughts
How to Break the cycle of negative ThoughtsHow to Break the cycle of negative Thoughts
How to Break the cycle of negative Thoughts
 
Unit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdfUnit 8 - Information and Communication Technology (Paper I).pdf
Unit 8 - Information and Communication Technology (Paper I).pdf
 
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...Cambridge International AS  A Level Biology Coursebook - EBook (MaryFosbery J...
Cambridge International AS A Level Biology Coursebook - EBook (MaryFosbery J...
 
Additional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdfAdditional Benefits for Employee Website.pdf
Additional Benefits for Employee Website.pdf
 
Chapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptxChapter 3 - Islamic Banking Products and Services.pptx
Chapter 3 - Islamic Banking Products and Services.pptx
 
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...TESDA TM1 REVIEWER  FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
TESDA TM1 REVIEWER FOR NATIONAL ASSESSMENT WRITTEN AND ORAL QUESTIONS WITH A...
 
Unit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdfUnit 2- Research Aptitude (UGC NET Paper I).pdf
Unit 2- Research Aptitude (UGC NET Paper I).pdf
 
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptxStudents, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
Students, digital devices and success - Andreas Schleicher - 27 May 2024..pptx
 
Palestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptxPalestine last event orientationfvgnh .pptx
Palestine last event orientationfvgnh .pptx
 
Model Attribute Check Company Auto Property
Model Attribute  Check Company Auto PropertyModel Attribute  Check Company Auto Property
Model Attribute Check Company Auto Property
 
Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......Ethnobotany and Ethnopharmacology ......
Ethnobotany and Ethnopharmacology ......
 
Home assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdfHome assignment II on Spectroscopy 2024 Answers.pdf
Home assignment II on Spectroscopy 2024 Answers.pdf
 
Supporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptxSupporting (UKRI) OA monographs at Salford.pptx
Supporting (UKRI) OA monographs at Salford.pptx
 

Data Structures in C++I am really new to C++, so links are really .pdf

  • 1. Data Structures in C++ I am really new to C++, so links are really hard topic for me. It would be nice if you can provide explanations of what doubly linked lists are and of some of you steps... Thank you In this assignment, you will implement a doubly-linked list class, together with some list operations. To make things easier, you’ll implement a list of int, rather than a template class. Solution A variable helps us to identify the data. For ex: int a = 5; Here 5 is identified through variable a. Now, if we have collection of integers, we need some representation to identify them. We call it array. For ex: int arr[5] This array is nothing but a Data Structure. So, a Data Structure is a way to group the data. There are many Data Structures available like Arrays, Linked List, Doubly-Linked list, Stack, Queue, etc. Doubly-Linked list are the ones where you can traverse from the current node both in left and right directions. Why so many different types of Data Structures are required ? Answer is very simple, grouping of data, storage of data and accessing the data is different. For example, in case of Arrays we store all the data in contiguous locations. What if we are not able to store the data in contiguous locations because we have huge data. Answer is go for Linked List/Doubly-Linked list. Here we can store the data anywhere and link the data through pointers. I will try to provide comments for the code you have given. May be this can help you. #pragma once /* dlist.h Doubly-linked lists of ints */ #include class dlist { public: dlist() { } // Here we are creating a NODE, it has a integer value and two pointers. // One pointer is to move to next node and other to go back to previous node.
  • 2. struct node { int value; node* next; node* prev; }; // To return head pointer, i.e. start of the Doubly-Linked list. node* head() const { return _head; } // To return Tail pointer, i.e. end of the Doubly-Linked list. node* tail() const { return _tail; } // **** Implement ALL the following methods **** // Returns the node at a particular index (0 is the head). node* at(int index){ int cnt = 0; struct node* tmp = head(); while(tmp!=NULL) { if (cnt+1 == index) return tmp; tmp = tmp->next; } } // Insert a new value, after an existing one void insert(node *previous, int value){ // check if the given previous is NULL if (previous == NULL) { printf("the given previous node cannot be NULL"); return; } // allocate new node struct node* new_node =(struct node*) malloc(sizeof(struct node)); // put in the data new_node->data = new_data;
  • 3. // Make next of new node as next of previous new_node->next = previous->next; // Make the next of previous as new_node previous->next = new_node; // Make previous as previous of new_node new_node->prev = previous; // Change previous of new_node's next node if (new_node->next != NULL) new_node->next->prev = new_node; } // Delete the given node void del(node* which){ struct node* head_ref = head(); /* base case */ if(*head_ref == NULL || which == NULL) return; /* If node to be deleted is head node */ if(*head_ref == which) *head_ref = which->next; /* Change next only if node to be deleted is NOT the last node */ if(which->next != NULL) which->next->prev = which->prev; /* Change prev only if node to be deleted is NOT the first node */ if(which->prev != NULL) which->prev->next = which->next; /* Finally, free the memory occupied by which*/ free(which); return;
  • 4. } // Add a new element to the *end* of the list void push_back(int value){ // allocate node struct node* new_node = (struct node*) malloc(sizeof(struct node)); struct node* head_ref = head(); struct node *last = *head_ref; // put in the data new_node->data = value; // This new node is going to be the last node, so make next of it as NULL new_node->next = NULL; // If the Linked List is empty, then make the new node as head if (*head_ref == NULL) { new_node->prev = NULL; *head_ref = new_node; return; } // Else traverse till the last node. while (last->next != NULL) last = last->next; // Change the next of last node last->next = new_node; // Make last node as previous of new node new_node->prev = last; return; }
  • 5. // Add a new element to the *beginning* of the list void push_front(int value){ //allocate node struct node* new_node = (struct node*) malloc(sizeof(struct node)); //put in the data new_node->data = value; // Make next of new node as head and previous as NULL struct node* head_ref = head(); new_node->next = (*head_ref); new_node->prev = NULL; //change prev of head node to new node if((*head_ref) != NULL) (*head_ref)->prev = new_node ; // move the head to point to the new node (*head_ref) = new_node; } // Remove the first element void pop_front(){ struct node *toDelete; struct node *head = head(); if(head == NULL) { printf("Unable to delete. List is empty. "); } else { toDelete = head; head = head->next; //Move head pointer to 2 node head->prev = NULL; //Remove the link to previous node
  • 6. free(toDelete); //Delete the first node from memory printf("SUCCESSFULLY DELETED NODE FROM BEGINNING OF THE LIST. "); } } // Remove the last element void pop_back(){ struct node * toDelete; struct node *last = head(); while(last.next!=NULL) { last = last->next; } if(last == NULL) { printf("Unable to delete. List is empty. "); } else { toDelete = last; last = last->prev; //Move last pointer to 2nd last node last->next = NULL; //Remove link to of 2nd last node with last node free(toDelete); //Delete the last node printf("SUCCESSFULLY DELETED NODE FROM END OF THE LIST. "); } } // Get the size of the list int size(){ int cnt = 0; struct node* head_ref = head(); while(head_ref!=NULL)
  • 7. { cnt++; head_ref = head_ref->next; } return cnt; } // Returns true if the list is empty bool empty(){ struct node* head_ref = head(); if(head_ref == NULL) return true; else return false; } private: node* _head = nullptr; node* _tail = nullptr; }; // **** Implement ALL the following functions **** /* out << l Prints a list to the ostream out. This is mostly for your convenience in testing your code; it's much easier to figure out what's going on if you can easily print out lists! */ std::ostream& operator<< (std::ostream& out, dlist& l); /* a == b Compares two lists for equality, returning true if they have the same elements in the same positions. (Hint: it is *not* enough to just compare pointers! You have to compare the values stored in the nodes.) */ bool operator== (dlist& a, dlist& b); /* a + b Returns a new list consisting of all the elements of a, followed by all the elements of b (i.e., the list concatenation).
  • 8. */ dlist operator+ (dlist& a, dlist& b); /* reverse(l) Returns a new list that is the *reversal* of l; that is, a new list containing the same elements as l but in the reverse order. */ dlist reverse(dlist& l);