Hello Code Running Like Include Include Include Using Namespace Std Class Node Public Type Q37300053

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

OR
OR

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.