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 size of linked list
int size()
{
node *tptr = head;
int c = 0;
while (tptr)
{
c++;
tptr =
tptr->next;
}
return c;
}
void add(int n, node::data_t d)
{
/*
tptr ->
Iterate though the existing list
prev -> Holds
the previous node
curr -> Holds
the newly created node
c -> Position
of current node
*/
node *tptr = head, *prev = head,
*curr;
int c = 1;
// Exit on invalid
position
if (n < 1)
return;
// Add at first position
if (n == 1)
{
add_first(d);
return;
}
// Add at last position
// Example: Let's say the list
already has 2 nodes, this handles if user tries to add new node at
position 3
if (size() == n - 1)
{
add_last(d);
return;
}
// Iterate through the list and
add new node at given position
while (tptr)
{
if (c ==
n)
{
curr = new node(d);
prev->next = curr;
curr->next = tptr;
break;
}
c++;
prev =
tptr; // Hold the current node as
previous node before traversing to next node
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 = 1;
// Get details if requested for
first node
if (n == 1)
return
get_first();
// Get details if requested for
last node
if (n == size())
return
get_last();
// Iterate and get the details
of requested node position
while (tptr)
{
if (n ==
c)
return tptr->data;
tptr =
tptr->next;
c++;
}
// Return negative if giving
position doesn't exists
return -1;
}
node::data_t get_first()
{
// Return negative if no list is
found
if (!head)
return -1;
return head->data;
// Return first node details
}
node::data_t get_last()
{
node *tptr = head;
// Return negative if no list is
found
if (!head)
return -1;
// Iterate through to get the last
node
while (tptr)
{
// If last node,
return the data
if
(tptr->next == NULL)
return tptr->data;
tptr =
tptr->next;
}
}
void remove(int n)
{
/*
tptr -> Holds
current node
prev -> Holds
previous node
c -> Position
of current node
*/
node *tptr = head, *prev =
head;
int c = 1;
// Handle remove for first
node
if (n == 1)
{
remove_first();
return;
}
// Handle remove for last
node
if (n == size())
{
remove_last();
return;
}
// Handle removal at given
node
while (tptr)
{
if (c ==
n)
{
prev->next = tptr->next; //
Assign previous node to next node
return;
}
c++;
prev =
tptr;
tptr =
tptr->next;
}
}
void remove_first()
{
// Exit if head as no nodes
if (!head)
return;
// Remove the first node
head = head->next;
}
void remove_last()
{
/*
tptr -> Holds
current node
prev -> Holds
previous node
*/
node *tptr = head, *prev =
head;
// Exit if head as no
nodes
if (!head)
return;
// Iterate to ge the last
node
while (tptr->next != NULL)
{
prev =
tptr;
tptr =
tptr->next;
}
// Remove last node
prev->next = NULL;
}
void dump()
{
node *tptr;
cout << " DUMP: (size = " << size() << ", first = " << get_first() << ", last = " << get_last() << ") ";
if (head == NULL) {
cout << "
DUMP: head = NULL ";
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);
}
}
Code:
#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) // code to add element at given
index
{
node *pre=new node;
node *cur=new node;
node *temp=new node;
cur=head;
for(int i=1;i<n;i++)
{
pre=cur;
cur=cur->next;
}
temp->data=d;
pre->next=temp;
temp->next=cur;
}
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::data_t get_first();
node::data_t get_last();
{
node* tptr = head;
if (head)
}
void remove(int n) // delete node at nth index
{
node *current=new node;
node *previous=new node;
current=head;
for(int i=1;i<pos;i++)
{
previous=current;
current=current->next;
}
previous->next=current->next;
}
void remove_first() // delete from first
{
node *temp=new node;
temp=head;
head=head->next;
delete temp;
}
void remove_last() // delete from last
{
node *current=new node;
node *previous=new node;
current=head;
while(current->next!=NULL)
{
previous=current;
current=current->next;
}
tail=previous;
previous->next=NULL;
delete current;
}
int get(int n) // find element at given index
{
struct Node* current = head;
int count = 0;
while (current != NULL)
{
if (count == n)
return(current->data);
count++;
current = current->next;
}
assert(0);
}
int get_first(); // find head of list
{
return head->data;
}
int get_last(); // find tail of list
{
struct Node* temp = head;
if (temp == NULL)
return -1;
while (temp != NULL)
{
if (temp->next==NULL)
return temp->data;
temp = temp->next;
}
}
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);
}
}
Hello, can someone help me solve the remove function since when I try to remove it...
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...
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...
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; ...
Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write an additional method called push_back(int) that will add an integer to the end of the list. You can modify the provided code. 2.Modify the Node class and LinkedList class so that you can access your parent node (double linked-list). #include #include using namespace std; typedef int Type; enum Boolean { False = 0, True }; class Item { friend class SLList; public: Type getVal()...
Can someone please help me with the following: Implement a Binary Search Tree that can be used to store data values that are strings. The data values will be stored in nodes in a binary search tree. Each node will have a unique integer key. Provide functions to add, get and remove elements to/from the tree. Also provide functions to print the elements (both the key and the data values) in forward (dump) and reverse (dump_rev) order. To help in...
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...
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();...
can someone please double check my code here are the requirements please help me fulfill the requirements Using the material in the textbook (NumberList) as a sample, design your own dynamic linked list class (using pointers) to hold a series of capital letters. The class should have the following member functions: append, insert (at a specific position, return -1 if that position doesn't exist), delete (at a specific position, return -1 if that position doesn't exist), print, reverse (which rearranges...
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) { ...
I have a C++ code that lets me enter, display and delete a student record. I need to implement a function that prints the average grade score of the students I input. Below is my code and a picture of how my code looks right now. #include<iostream> #include<stdlib.h> using namespace std; //Node Declaration struct node { string name; string id; int score; node *next; }; //List class class list { private: //head...