Question
With c++ application I can just add stuffs on inputs to show that my circular and double linked list is wokring A. Convert the singly
With c++ application
I can just add stuffs on inputs to show that my circular and double linked list is wokring
A. Convert the singly linked list class that we implemented in the lab -or the one under content- to have a circularly linked list.
Set the next reference of the tail node of the singly linked list to reference back to the head of the list.
Modify all the public behaviors of the singly linked list to match the modification of the circularly linked list
Define method rotate(), this method moves the first element to the end of the list
B. Implement a doubly linked list: In a doubly linked list each node keeps an explicit reference prev to the node before it and a reference next to the node after it.
Add special dummy nodes at both ends of the list, name them header & trailer: these are sentinels node, they dont store elements of the primary sequence.
When using sentinel nodes, an empty list is initialized so that the next field of the header points to the trailer, and the prev field of the trailer points to the header; the remaining fields of the sentinels are irrelevant
Make sure to implement all the following methods
size( ): Returns the number of elements in the list.
isEmpty( ): Returns true if the list is empty, and false otherwise.
first( ): Returns (but does not remove) the first element in the list.
last( ): Returns (but does not remove) the last element in the list.
addFirst(e): Adds a new element to the front of the list.
addLast(e): Adds a new element to the end of the list.
removeFirst( ): Removes and returns the first element of the list.
removeLast( ): Removes and returns the last element of the list.
Dont forget to implement the Display() and toString() functions.
Add comments Write the main function that shows the usage of both lists
So far What i have
#include
#include
#include "LinkedList.h"
#include "Node.h"
using namespace std;
int main() {
// insert code here...
std::cout << "Hello, World! ";
int a = 3;
string s = "M";
cout << s << a;
s = s + to_string(a);
cout << s;
LinkedList myList;
cout << myList.size();
myList.deleteHead();
myList.add("1");
myList.add("2");
myList.add("3");
myList.add("4");
myList.addHead("11");
myList.addHead("22");
myList.addHead("33");
myList.add_pos(-1, "55");
cout << " Size..." << myList.size()<< endl;
myList.add_pos(5, "55");
cout << myList.to_string();
cout << " Delelte Head.. " << myList.deleteHead();
cout << " Size..." << myList.size()<< endl;
cout << myList.to_string();
cout << " Delelte Tail.. " << myList.deleteTail();
cout << " Size..." << myList.size()<< endl;
cout << myList.to_string();
cout << " Delelte at position... "<< myList.delete_pos(3);
cout << " Size..." << myList.size()<< endl;
cout << myList.to_string();
system("pause");
return 0;
}
#pragma once
#include "Node.h"
#include
#include
using namespace std;
#ifndef LINKEDLIST_H
#define LINKEDLIST_H
class LinkedList {
private:
Node *head, *tail;
public:
LinkedList();
~LinkedList();
int size();
void add(string);
void addHead(string value);
void add_pos(int pos, string value);
string deleteHead();
string deleteTail();
string delete_pos(int pos);
void display();
string to_string();
};
#endif
#include "LinkedList.h"
#include
#include
using namespace std;
LinkedList::LinkedList() {
head = NULL;
tail = NULL;
}
LinkedList::~LinkedList() {
Node *n = this->head, *current = NULL; //initialization part
while(n) //start cleanup of nodes of the list
{
current = n;
n=n->getNextPtr();
delete(current);
}
head = tail = NULL;
}
int LinkedList::size() {
Node *cur = new Node;
cur = head;
int size = 0;
while (cur != NULL) {
size++;
cur = cur->getNextPtr();
}
return size;
}
void LinkedList::add(string value) {
Node *temp = new Node;
temp->setData(value);
temp->setNextPtr(NULL);
if (head == NULL) {
head = temp;
tail = temp;
temp = NULL;
} else
{
tail->setNextPtr(temp);
tail = temp;
}
}
void LinkedList::addHead(string value) {
Node *newest = new Node;
newest->setData(value);
newest->setNextPtr(head);
head = newest;
}
void LinkedList::add_pos(int pos, string value) {
if (pos<0 or pos>size())
cout << "out of range ... can't insert";
else {
Node *pre = new Node;
Node *cur = new Node;
Node *newest = new Node;
cur = head;
for (int i = 1; i
pre = cur;
cur = cur->getNextPtr();
}
newest->setData(value);
pre->setNextPtr(newest);
newest->setNextPtr(cur);
}
}
string LinkedList::deleteHead() {
string a = "";
if (size() == 0)
//we have a problem here.. if the list is empty
//I am still returning an integer value
cout << "Empty List, nothing to delete";
else{
Node *temp=new Node;
a = head->getData();
temp=head;
head=head->getNextPtr();
delete temp;
}
return a;
}
string LinkedList::deleteTail() {
//check if empty
Node *current = new Node;
Node *previous = new Node;
current = head;
while (current->getNextPtr() != NULL) {
previous = current;
current = current->getNextPtr();
}
tail = previous;
previous->setNextPtr(NULL);
string a = current->getData();
delete current;
return a;
}
string LinkedList::delete_pos(int pos) {
//we have a problem here.. if the list is empty
//I am still returning an integer value
string a = "";
if (pos<0 or pos>size())
cout << "out of range ... nothing to delete";
else {
Node *current = new Node;
Node *previous = new Node;
current = head;
for (int i = 1; i
previous = current;
current = current->getNextPtr();
}
a = current->getData();
previous->setNextPtr(current->getNextPtr());
delete current;
}
return a;
}
void LinkedList::display() {
Node *cur = new Node;
cur = head;
while (cur != NULL) {
cout << cur->getData() << "\t";
cur = cur->getNextPtr();
}
}
string LinkedList::to_string() {
string str = "";
Node *temp = new Node;
temp = head;
while (temp != NULL) {
//in java I can str + int
str = str + temp->getData() + "\t";
temp=temp->getNextPtr();
}
return str;
}
#pragma once
#include
#include
#include"Node.h"
using namespace std;
Node::Node() {
data = "";
nextPtr = NULL;
}
Node::Node(string d) {
data = d;
nextPtr = NULL;
}
void Node::setData(string s) {
data = s;
}
string Node::getData() const {
return data;
}
void Node::setNextPtr(Node* ptr) {
nextPtr = ptr;
}
Node* Node::getNextPtr()const {
return nextPtr;
}
#pragma once
#include
#include
#include
using namespace std;
#ifndef NODE_H
#define NODE_H
class Node{
private:
string data;
Node * nextPtr;
public:
Node();
Node(string d);
void setData(string);
string getData()const;
void setNextPtr(Node*);
Node* getNextPtr()const;
};
#endif
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started