Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

This is what I have so far.. #include #include using namespace std; #ifndef NODE_H #define NODE_H class Node { private: string data; Node *nextPtr; public:

image text in transcribedimage text in transcribed

This is what I have so far..

#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

#include

#include "Node.h"

#include

using namespace std;

Node::Node()

{

}

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;

}

#ifndef LINKEDLISTNODE_H

#define LINKEDLISTNODE_H

#include "Node.h"

#include

using namespace std;

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);

bool isEmpty();

string deleteHead();

string deleteTail();

string delete_pos(int pos);

void display();

string to_string();

string first();

string last();

void addFirst(string);

void addLast(string);

void removeFirst();

};

#endif

#include "LinkedListNode.h"

#include "Node.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;

}

//at the end of the list

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 (possize())

cout

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);

}

}

bool LinkedList::isEmpty()

{

Node *cur = new Node;

cur = head;

if (head == NULL)

{

return true;

cout

}

else return false;

cout

}

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

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 (possize())cout

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 getData()

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;

}

string LinkedList::first()

{

Node *cur = new Node;

cur = head;

string str = cur->getData();

return str;

}

string LinkedList::last()

{

Node *cur = new Node;

cur = tail;

string str = cur->getData();

return str;

}

void LinkedList::addFirst(string x)

{

Node *temp = new Node;

temp->setData(x);

temp->setNextPtr(head);

head = temp;

}

void LinkedList::addLast(string x)

{

Node *temp = new Node;

temp->setData(x);

temp->setNextPtr(NULL);

if (head == NULL)

{

head = temp;

tail = temp;

temp = NULL;

}

else

{

tail->setNextPtr(temp);

tail = temp;

}

}

void LinkedList::removeFirst()

{

Node *temp = new Node;

temp = head;

head->setNextPtr(tail->getNextPtr());

delete temp;

}

#include

#include

#include "LinkedListNode.h"

using namespace std;

int main(int argc, const char * argv[]) {

// insert code here...

std::cout

int a = 3;

string s = "M";

cout

s = s + to_string(a);

cout

LinkedList myList;

cout

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

myList.add_pos(5, "55");

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

cout

myList.add("77");

cout

cout

cout

cout

cout

myList.addFirst("100");

cout

cout

myList.addLast("27");

cout

myList.removeFirst();

cout

system("pause");

return 0;

}

Project Linked Lists 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 don't 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

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Refactoring Databases Evolutionary Database Design

Authors: Scott Ambler, Pramod Sadalage

1st Edition

0321774515, 978-0321774514

More Books

Students also viewed these Databases questions