main.cpp---------------------------------------
Node.h----------------------------------------------------------------
LinkList.h------------------------------------------------------------------------------------------------------------
--------------------------------------------------------------------------
For the deletel(); function, it is supposed to delete the ith element in the list. So like either 4th element, 5th element, or second element. But when I run it, the command prompt crashes.
#include "LinkList.h" #include <iostream> using namespace std; int main() { linkedlist obj; obj.pushfront(3); obj.pushfront(6); obj.pushfront(20); obj.popback(); obj.pushfront(5); obj.pushback(12); obj.popfront(); obj.deletel(3); obj.printlistfromfront(); obj.printlistfromtail(); obj.size(); cin.get(); return 0; }
Node.h----------------------------------------------------------------
#include <cstddef> #include <iostream> using namespace std; class Node{ public: int item; Node *next; Node *prev; friend ostream& operator<< (ostream& print, const Node &value){ return print << value.item; }; Node(int data){ item = data; next=NULL; //set it = to NULL by default so it doesnt hold a random value prev =NULL; //set it = to NULL by default so it doesnt hold a random value } };
LinkList.h------------------------------------------------------------------------------------------------------------
#include "Node.h" #include <cstddef> #include<iostream> using namespace std; class linkedlist{ public: linkedlist(){ front=NULL; }; void pushfront(int item){{ if (front == NULL){ front =tail = new Node(item); front -> next = NULL; tail -> prev = NULL; } else //or else { Node *temp; temp = new Node(item); temp -> next = front; front -> prev = temp; front = temp; }} } void popfront (){ if (front == NULL) return; else { Node *temp; temp = front; front = front -> next; front -> prev = NULL; front -> next = (temp-> next)->next; delete temp; } } void pushback( int item){ if (front == NULL){ front = tail = new Node(item); front -> next = NULL; tail -> prev = NULL; } else { Node *temp = new Node(item); tail -> next = temp; temp -> prev = tail; tail = temp; } } void popback(){ Node* temp = tail; (temp -> prev) -> next = NULL; tail = temp -> prev; delete temp; } void printlistfromfront(){ Node* traverse; traverse = front; cout << "Items starting from the front of the list are:\a "; while (traverse != NULL){ cout <<" " << *traverse; traverse = traverse -> next; } cout << endl; } void printlistfromtail(){ Node *traversle; traversle = tail; cout << "Items from the end of the list, going backwards are:\a "; while (traversle != NULL){ cout << " " << *traversle; traversle = traversle -> prev; } cout << endl; } int size(){ Node* traverse; int sizel = 0; traverse = front; while (traverse != NULL){ traverse = traverse -> next; sizel++; } cout << "The size of the list is: "<<sizel<<endl; return sizel; } void deletel(int ith){ if (front == NULL){ cout<<"there are no nodes to delete."; return; } if ((front -> next == NULL) && (tail -> prev == NULL)){ front = NULL; tail = NULL;} else{ Node *traverse = front; int i = 0; while (i < ith ){ traverse = traverse -> next; i++; } (traverse -> prev) -> next = traverse -> next; (traverse -> next) -> prev = traverse -> prev; } } private: Node *front; Node *tail; };
--------------------------------------------------------------------------
For the deletel(); function, it is supposed to delete the ith element in the list. So like either 4th element, 5th element, or second element. But when I run it, the command prompt crashes.