Hello, I have this code but its not running like it should:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
class node
{
public:
typedef int data_t;
node *next;
data_t data;
node(data_t d) { next = NULL; data = d; }
};
class linked_list
{
private:
node *head;
public:
linked_list()
{
head = NULL;
}
int size()
{
node *tptr = head;
int c = 0;
while (tptr)
{
c++;
tptr = tptr->next;
}
return c;
}
void add(int n, node::data_t d)
{
node *tptr = head, *prev = head, *curr;
// cout << "add: data = " << d <<"," << "n
= " << n << endl;
int c = 0;
if (n < 0)
{
return;
}
if (n == 0)
{
add_first(d);
return;
}
if (size() == n)
{
add_last(d);
return;
}
while (tptr)
{
if (c == n)
{
curr = new node(d);
prev->next = curr;
curr->next = tptr;
break;
}
c++;
prev = tptr;
tptr = tptr->next;
}
}
void add_last(node::data_t d)
{
node *tptr = head;
if (!head)
{
head = new node(d);
return;
}
while (tptr->next)
{
tptr = tptr->next;
}
tptr->next = new node(d);
}
void add_first(node::data_t d)
{
node *newptr = new node(d);
newptr->next = head;
head = newptr;
}
node::data_t get(int n)
{
node *tptr = head;
int c = 0;
if (n == 0)
{
return get_first();
}
if (n == size())
{
return get_last();
}
while (tptr)
{
if (n == c)
{
return tptr->data;
}
tptr = tptr->next;
c++;
}
return tptr->data ;
}
node::data_t get_first()
{
if (!head)
{
cout << "null list\n";
return - 1;
}
return head->data;
}
node::data_t get_last()
{
node *tptr = head;
if (!head)
{
return -1;
}
while (tptr)
{
if (tptr->next == NULL)
{
return tptr->data;
}
tptr = tptr->next;
}
}
void remove(int n)
{
node *tptr = head, *prev = head;
int c=0;
if(n==0)
{
remove_first();
return;
}
if(n==size())
{
remove_last();
return;
}
while(tptr)
{
if(c == n)
{
prev->next = tptr->next;
return;
}
c++;
prev=tptr;
tptr=tptr->next;
}
}
void remove_first()
{
node *new tptr = head;
head = head->next;
delete tptr;
// if(!head)
// {
// return;
// }
// head = head->next;
}
void remove_last()
{
node *tptr = head, *prev = head;
if(!head)
{
return;
}
while(tptr->next != NULL)
{
prev = tptr;
tptr = tptr->next;
}
prev->next = NULL;
}
void dump()
{
node *tptr;
cout << " DUMP: (size = " << size() << ", first = " << get_first() << ", last = " << get_last() << ")\n";
if (head == NULL)
{
cout << " DUMP: head = NULL\n\n";
return;
}
tptr = head;
while (tptr)
{
cout << " DUMP: data = : " << tptr->data <<
endl;
tptr = tptr->next;
}
cout << endl;
}
};
int main(void)
{
linked_list ll;
string cmd;
int i, d;
while (cin >> cmd >> i >> d)
{
cout << "MAIN: cmd = " << cmd << ", i = "
<< i
<< ", d = " << d << endl;
if (cmd == "add")
ll.add(i, d);
else if (cmd == "addf")
ll.add_first(d);
else if (cmd == "addl")
ll.add_last(d);
else if (cmd == "rem")
ll.remove(i);
else if (cmd == "remf")
ll.remove_first();
else if (cmd == "reml")
ll.remove_last();
else if (cmd == "get") {
d = ll.get(i);
cout << "get returns: " << d << endl;
} else if (cmd == "getf") {
d = ll.get_first();
cout << "getf returns: " << d << endl;
} else if (cmd == "getl") {
d = ll.get_last();
cout << "getl returns: " << d << endl;
} else if (cmd == "dump")
ll.dump();
else if (cmd == "quit")
exit(0);
}
}
And this is what it should print out, please help, and thank you in
advance:

//Changes have been made. There were some pointer errors and few functions were called in an inappropriate way. Updation done. Please compile and run the following updated code.
linklist.cpp
//preprocessor headers
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
//class definition of node
class node
{
public:
typedef int data_t;
node *next;
data_t data;
node(data_t d) { next = NULL; data = d; }
};
//class definition of linkedlist
class linked_list
{
private:
node *head;
public:
linked_list()
{
head = NULL;
}
int size()
{
node *tptr = head;
int c = 0;
while (tptr)
{
c++;
tptr = tptr->next;
}
return c;
}
//function definition to add the node
void add(int n, node::data_t d)
{
node *tptr = head, *prev = head, *curr;
cout << "add: data = " << d <<"," << "n = "
<< n << endl;
int c = 0;
if (n < 0)
{
return;
}
if (n == 0)
{
add_first(d);
return;
}
if (size() == n)
{
add_last(d);
return;
}
while (tptr)
{
if (c == n)
{
curr = new node(d);
prev->next = curr;
curr->next = tptr;
break;
}
c++;
prev = tptr;
tptr = tptr->next;
}
}
//Function definition to add the node at the last
void add_last(node::data_t d)
{
node *tptr = head;
if (!head)
{
head = new node(d);
return;
}
while (tptr->next)
{
tptr = tptr->next;
}
tptr->next = new node(d);
cout << "add: data = " << d << ", n = " <<
(size()-1) << endl;
}
//Function definition to add the node at the first
void add_first(node::data_t d)
{
node *newptr = new node(d);
newptr->next = head;
head = newptr;
cout << "add: data = " << d << ", n = " <<
(size()-1) << endl;
}
//function definition to get the node data
node::data_t get(int n)
{
node *tptr = head;
int c = 0;
if (n == 0)
{
return get_first();
}
if (n == size())
{
return get_last();
}
while (tptr)
{
if (n == c)
{
return tptr->data;
}
tptr = tptr->next;
c++;
}
return tptr->data ;
}
//function definition to get the first node data
node::data_t get_first()
{
if (!head)
{
cout << "null list\n";
return - 1;
}
return head->data;
}
//function definition to get the last node data
node::data_t get_last()
{
node *tptr = head;
if (!head)
{
return -1;
}
while (tptr)
{
if (tptr->next == NULL)
{
return tptr->data;
}
tptr = tptr->next;
}
}
//function definition to remove the data
void remove(int n)
{
node *tptr = head, *prev = head;
int c=0;
if(n==0)
{
remove_first();
return;
}
if(n==size())
{
remove_last();
return;
}
while(tptr)
{
if(c == n)
{
prev->next = tptr->next;
return;
}
c++;
prev=tptr;
tptr=tptr->next;
}
}
//function definition to remove the first data
void remove_first()
{
node *tptr = head;
head = head->next;
delete tptr;
// if(!head)
// {
// return;
// }
// head = head->next;
}
//function definition to remove the last node data
void remove_last()
{
node *tptr = head, *prev = head;
if(!head)
{
return;
}
while(tptr->next != NULL)
{
prev = tptr;
tptr = tptr->next;
}
prev->next = NULL;
}
//function definition to dump the data
void dump()
{
node *tptr;
cout << " DUMP: (size = " << size() << ", first =
" << get_first() << ", last = " << get_last()
<< ")\n";
if (head == NULL)
{
cout << " DUMP: head = NULL\n\n";
return;
}
tptr = head;
while (tptr)
{
cout << " DUMP: data = : " << tptr->data <<
endl;
tptr = tptr->next;
}
cout << endl;
}
};
int main(void)
{
linked_list ll;
string cmd;
int i, d;
while (cin >> cmd >> i >> d)
{
cout << "MAIN: cmd = " << cmd << ", i = "
<< i
<< ", d = " << d << endl;
if (cmd == "add")
ll.add(i, d);
else if (cmd == "addf")
ll.add_first(d);
else if (cmd == "addl")
ll.add_last(d);
else if (cmd == "rem")
ll.remove(i);
else if (cmd == "remf")
ll.remove_first();
else if (cmd == "reml")
ll.remove_last();
else if (cmd == "get") {
cout << "get returns: " << ll.get(i) <<
endl;
} else if (cmd == "getf") {
cout << "getf returns: " << ll.get_first() <<
endl;
} else if (cmd == "getl") {
cout << "getl returns: " << ll.get_last() <<
endl;
} else if (cmd == "dump")
ll.dump();
else if (cmd == "quit")
exit(0);
}
}
Output:
Compilation -

Execution -
dump , add, add_first and add_last

remove, remove_first and remove_last

get, get_first, get_last

Hello, I have this code but its not running like it should: #include <iostream> #include &l...
Hello, can someone help me solve the remove function since when I try to remove it doesn't work, please and thank you. #include <iostream> #include <cstdlib> #include <string> using namespace std; class node { public: typedef int data_t; node *next; data_t data; node(data_t d) { next = NULL; data = d; } }; class linked_list { private: node *head; public: linked_list() { head = NULL; } // Get the...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
C++ - I have a doubly linked list, but I haven't been able to get the "reverse list" option in the code to work(It's option #in the menu in the program). I received this guidance for testing: Test 4 cases by entering (in this order) c,a,z,k,l,m This tests empty list, head of list, end of list and middle of list. Then delete (in this order) a,z,l. This tests beginning, end and middle deletes. This exhaustively tests for pointer errors. #include...
In C++, change this code to a circular linked list #include <iostream> using namespace std; struct node { string data; node *next; }; class linked_list { private: node *head,*tail; public: linked_list() { head = NULL; tail = NULL; } void add_node(string n) { node *tmp = new node; tmp->data = n; tmp->next = NULL; if(head == NULL) { head = tmp; tail = tmp; } else { tail->next = tmp; tail = tail->next; } } void display() { while (tmp...
c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream> using namespace std; template<class T> struct Node { T data;//data field Node * next;//link field Node(T data) { this->data = data; } }; template<class T> class linked_list{ private: Node<T> *head,*current; public: linked_list(){//constructor, empty linked list head = NULL; current = NULL; } ~linked_list(){ current = head; while(current != NULL) { ...
Hi I got error C2660 : 'valuein' : function does not take 2 parameters in visual studio 2013 this is my code so far #include <cstdlib> #include <list> #include <iostream> using namespace std; struct node { int data; node* next; }; void initialize(node*p); void insert(int data,int n); void remove(int b,int pos); bool empty(node*); void length(); bool valuein(int c); int reportvalue(int value); void ptintlist(); int menu(); int main() { struct node*head=NULL; char commands; int value; ...
C++: I need implement this code using Double Linked List using the cosiderations 1. head point to null in an empty list 2. There is not need of a tail pointer /*This class implements the singly linked list using templates Each list has two attributes: -head: first node in the list -tail: last node in the list #include "circDLLNode.h" template class { public: //Default constructor: creates an empty list (); //Destructor: deallocate memory ~(); ...
Hello, I have some errors in my C++ code when I try to debug it.
I tried to follow the requirements stated below:
Code:
// Linked.h
#ifndef INTLINKEDQUEUE
#define INTLINKEDQUEUE
#include <iostream>
usingnamespace std;
class IntLinkedQueue
{
private: struct Node {
int data;
Node *next;
};
Node *front; // -> first item
Node *rear; // -> last item
Node *p; // traversal position
Node *pp ; // previous position
int size; // number of elements in the queue
public:
IntLinkedQueue();...
I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...
I need help and have to get this done asap: Using the following source codes provided below, create recursive functions and methods in C++ to complete the following exercises: Print the list in forward order Print the list in reverse order Print the following three lines (in this order): "The first node contains <value in first node>" "The last node contains <value in last node>" "The first node contains <value in first node>" Print the sum of all the values...