Question

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:

dump 99 et last: invalid index get: invalid index DUMP: (31ze 0, first:-1, last= -1) DUMP: head NULL addf 0 0 add: data 0, n

0 0
Add a comment Improve this question Transcribed image text
Answer #1

//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 -

Compile Log Debug 88 Compiler Resources dh VDebus laFindResultAciot Compiling single file Abort Compilation - Filename: C:\Us

Execution -

dump , add, add_first and add_last

C:\Users G priya Darshni\ Documentslinklist.exe ump 9 9 IA 1 N :.crnd dump. İ 9, d- ull list DUMP: くsize = 0, first =-1, last

remove, remove_first and remove_last

addl 4 4 AIN: cmd = add i, i = 4, d = 4 add: data -4. n-1 dump 9 9 AIN: cmd = dump, 1-9, d = 9 DUMP <size -2. first 4. last-4

get, get_first, get_last

C:\Users G priya Darshni\ Documentslinklist ump 9 9 IA 1 N :.crnd dump. İ 9, d- ull list DUMP: くsize = 0, first =-1, last = -

Add a comment
Know the answer?
Add Answer to:
Hello, I have this code but its not running like it should: #include <iostream> #include &l...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Hello, can someone help me solve the remove function since when I try to remove it...

    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++ - I have a doubly linked list, but I haven't been able to get the...

    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...

  • c++ modify the attached unsorted linked list class into a sorted linked list class #include <iostream>...

    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...

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

  • Hello, I have some errors in my C++ code when I try to debug it. I...

    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...

    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...

  • Linkedlist implementation in C++ The below code I have written is almost done, I only need...

    Linkedlist implementation in C++ The below code I have written is almost done, I only need help to write the definition for delete_last() functio​n. ​ Language C++ // LinkedList.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <string> #include <iostream> using namespace std; struct Node { int dataItem;//Our link list stores integers Node *next;//this is a Node pointer that will be areference to next node in the list }; class LinkedList { private: Node *first;...

  • Please rewrite this function using recursive function #include using namespace std; struct Node { char ch;...

    Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...

  • I have a C++ code that lets me enter, display and delete a student record. I...

    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...

  • Hi, I hope I can get some help with the following exercise in C++( CPP): 1.Write...

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

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT