I will try this again and hope for better results! I have been working on a C++
ID: 3747740 • Letter: I
Question
I will try this again and hope for better results! I have been working on a C++ project for the last couple of days and have managed to create a significant portion of the code, but I am still unable to figure out how to write part of the program. I will include screenshots of the files in an attempt to make them easier to read. I'm sorry it's so long, but I felt this would be the easiest way to represent what I have and what is still needed.
LINKEDLIST.H
#ifndef _LINKED_LIST
#define _LINKED_LIST
#include "Node.h"
template<typename T>
class LinkedList
{
private:
Node<T>* headPtr;
int itemCount;
Node<T>* getNodeAt(int position) const;
public:
LinkedList();
//LinkedList(const LinkedList<T>& aList);
~LinkedList();
virtual bool isEmpty() const = 0;
virtual int getLength() const = 0;
virtual void insert(int newPosition, T newEntry) = 0;
virtual void remove(int position) = 0;
virtual void clear() = 0;
virtual T getEntry(int position) = 0;
virtual void setEntry(int position, T newEntry) = 0;
}; // end LinkedList
#endif
LINKEDLIST.HPP
#include "LinkedList.h" // Header file
#include <cassert>
#include <iostream>
template<typename T>
LinkedList<T>::LinkedList(){
headPtr(nullptr), itemCount(0);
}// end default constructor
template<typename T>
LinkedList<T>::~LinkedList(){
clear();
} // end destructor
template<typename T>
bool LinkedList<T>::isEmpty() const{
return itemCount == 0;
} // end isEmpty
template<typename T>
int LinkedList<T>::getLength() const{
return itemCount;
} // end getLength
template<typename T>
void LinkedList<T>::insert(int newPosition, T newEntry){
bool ableToInsert = (newPosition >= 1) && (newPosition <= itemCount + 1);
if (ableToInsert)
{
// Create a new node containing the new entry
Node<T>* newNodePtr = new Node<T>(newEntry);
// Attach new node to chain
if (newPosition == 1)
{
// Insert new node at beginning of chain
newNodePtr->setNext(headPtr);
headPtr = newNodePtr;
}
else
{
// Find node that will be before new node
Node<T>* prevPtr = getNodeAt(newPosition - 1);
// Insert new node after node to which prevPtr points
newNodePtr->setNext(prevPtr->getNext());
prevPtr->setNext(newNodePtr);
} // end if
itemCount++; // Increase count of entries
}
else
std::cout << "Couldn't be found ";
} // end insert
template<typename T>
void LinkedList<T>::remove(int position){
bool ableToRemove = (position >= 1) && (position <= itemCount);
if (ableToRemove)
{
Node<T>* curPtr = nullptr;
if (position == 1)
{
// Remove the first node in the chain
curPtr = headPtr; // Save pointer to node
headPtr = headPtr->getNext();
}
else
{
// Find node that is before the one to delete
Node<T>* prevPtr = getNodeAt(position - 1);
// Point to node to delete
curPtr = prevPtr->getNext();
// Disconnect indicated node from chain by connecting the
// prior node with the one after
prevPtr->setNext(curPtr->getNext());
} // end if
// Return node to system
curPtr->setNext(nullptr);
delete curPtr;
curPtr = nullptr;
itemCount--; // Decrease count of entries
}
else
std::cout << "Cannot remove item ";
} // end remove
template<typename T>
void LinkedList<T>::clear(){
while (!isEmpty())
remove(1);
} // end clear
template<typename T>
T LinkedList<T>::getEntry(int position){
// Enforce precondition
bool ableToGet = (position >= 1) && (position <= itemCount);
if (ableToGet)
{
Node<T>* nodePtr = getNodeAt(position);
return nodePtr->getItem();
}
else
{
std::string message = "getEntry() called with an empty list or ";
message = message + "invalid position.";
} // end if
} // end getEntry
template<typename T>
Node<T>* LinkedList<T>::getNodeAt(int position) const{
// Debugging check of precondition
assert( (position >= 1) && (position <= itemCount) );
// Count from the beginning of the chain
Node<T>* curPtr = headPtr;
for (int skip = 1; skip < position; skip++)
curPtr = curPtr->getNext();
return curPtr;
}// end getNodeAt
// End of implementation file.
NODE.H
#ifndef NODE_H
#define NODE_H
template <typename T>
class Node // A node in a singly linked list
{
private:
T item;
Node* next; // we can only go "forward" along our chain
public:
Node(T anItem) : item(anItem), next(nullptr) {}
Node(T anItem, Node* nextNode) : item(anItem), next(nextNode) {}
T getItem() const { return item; }
Node* getNext() const { return next; }
void setItem(T newItem) { item = newItem; }
void setNext(Node* nextNode) { next = nextNode; }
};
#endif
WEBBROWSERINTERFACE.H
#include "LinkedList.h"
#include <string>
#ifndef WEBBROWSERINTERFACE_H
#define WEBBROWSERINTERFACE_H
class WebBrowserInterface
{
public:
/**
* @post All memory allocated by the implementing class should be freed.
* This, as with all virtual destrucors, is an empty definition since we
* have no knowledge of specific implementation details.
*/
virtual ~WebBrowserInterface(){}
/**
* @pre none
* @post the browser navigate to the given url
* @param url, a string representing a URL
*/
virtual void navigateTo(std::string url) = 0;
/**
* @pre none
* @post if possible, the browser navigates forward in the history otherwise it keeps focus
* on the current URL
*/
virtual void forward() = 0;
/**
* @pre none
* @post if possible, the browser navigates backwards in the history otherwise it keeps focus
* on the current URL
*/
virtual void back() = 0;
/**
* @return the current URL
*/
virtual std::string currentURL() const = 0;
/**
* @pre The list being passed in is empty
* @post The current browser history is copied into the given list
* @param destination, an empty list of strings that will have a copy of current history copied into
*/
virtual void copyCurrentHistory(ListInterface<string>& destination) = 0;
};
Explanation / Answer
A connected rundown is an information structure that can store an uncertain measure of things. These things are associated utilizing pointers in a consecutive way.
There are two sorts of connected rundown; independently connected rundown, and doubly-connected rundown. In an independently connected rundown, each component contains a few information and a connection to the following component. Then again, every hub in a doubly-connected rundown contains a few information, a connection to the following hub and a connection to the past hub.
The components of a connected rundown are known as the hubs. A hub has two fields i.e. information and next. The information field contains the information being put away in that particular hub. It can't simply be a solitary variable. There might be numerous factors introducing the information segment of a hub. The following field contains the address of the following hub. So this is where the connection between hubs is set up.
#include "stdio.h"
#include "conio.h"
#include "iostream.h"
using namespace std;
int insertion(int x);
void display();
void del(int x);
void rev();
int search(int x);
int cmp(int a,int b)
{
if(a>b)
return 1;
else if(b>a)
return -1;
}
int cmp_num=1;
struct node
{
int info;
node *prv;
node *nxt;
};
node *top = NULL;
int main()
{
int choice,d,y;
char ans='y';
while(ans=='y')
{
cout<<" 1.Insert 2. Delete 3.Reverse 4.EXIT Enter Choice : ";
cin>>choice;
if(choice==1)
{
cout<<"Enter An Element To be inserted : ";
cin>>d;
d=insertion(d);
display();
}
else if(choice==2)
{
cout<<"Enter Element To Be Deleted : ";
cin>>d;
del(d);
display();
}
else if(choice==3)
rev();
else
return 0;
}
return 0;
}
int search(int x)
{
int cnt=0;
node *searchele=top;
while( searchele!=NULL)
{
if(cmp(x,searchele->info)==cmp_num)
{
searchele=searchele->nxt;
cnt+=1;
}
else
break;
}
return cnt;
}
int insertion(int x)
{
if(top==NULL)
{
top=new node;
top->info=x;
top->nxt=NULL;
top->prv=NULL;
}
else if(cmp(top->info ,x)==cmp_num)
{
node *n=new node;
n->info=x;
n->nxt=top;
n->prv=NULL;
top->prv=n;
top=n;
}
else
{
int c=search(x);
node *insertele=top;
for(int i=0;i<c-1;i++)
insertele=insertele->nxt;
node *n=new node;
n->info=x;
node *b=insertele->nxt;
node *N =insertele;
n->prv=insertele;
n->nxt=b;
insertele->nxt=n;
if(b!=NULL)
b->prv=n;
}
}
void display()
{
cout<<"Element In The Linked List Are : ";
node *disply=top;
while(disp!=NULL)
{
cout<<" "<<disp->info;
if(disp->nxt==NULL)
{
break;
}
disp=disp->nxt;
}
}
void del(int x)
{
node *del=top;
if(del->info == x)
{
if(del->nxt==NULL && del->prv==NULL)
{
top=NULL;
return;
}
del->nxt->prv=NULL;
top=del->nxt;
}
else
{
node *delsuc=del->nxt;
if(del==NULL)
{
cout<<" Element Not Found ";
return;
}
if(delsuc==NULL)
{
cout<<" Element Not Found ";
return;
}
while(delsuc->info != x)
{
del=del->nxt;
delsuc=delsuc->nxt;
if(del==NULL)
{
cout<<" Element Not Found ";
return;
}
if(delsuc==NULL)
{
cout<<" Element Not Found ";
return;
}
}
del->nxt=delsuc->nxt;
if(delsuc->nxt!=NULL)
delsuc->nxt->prv=del;
}
}
void rev()
{
node *a=top;
node *b,*c,*d;
while(a!=NULL)
{
d=a;
c=a->nxt;
b=a->prv;
a->prv=a->nxt;
a->nxt=b;
a=c;
}
top=d;
cout<<"After Reversing the linked list";
display();
cmp_num*=-1;
}