Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with this question: Create a program that uses a derived class based on the list class youve created in the first program.

I need help with this question:

Create a program that uses a derived class based on the list class youve created in the first program. This program will use a stack data type. class node { void *info; node *next; public: node (void *v) {info = v; next = 0; } void put_next (node *n) {next = n;} node *get_next ( ) {return next;} void *get_info ( ) {return info;} }; class list { node *head; int node_num; public: list ( ) { node_num = 0; head = 0;}

Here is the code I have for the first question it is referring to:

#include //allows input/output

using namespace std; //using standard namespace library

class linklist //create class linklist { private: //private members struct node //create struct node { int data; //declare data as integer node *link; //create node variable for link pointer }*p; //end struct, first node p

public: //public members linklist(); //constructor void append(int num); //void function append with variable num as int void add_as_first(int num); //void function add as first in list with variable num as int void addafter(int c, int num); //void function add after variable c (int) with variable num as int void del(int num); //void function delete with variable num as int void display(); //void function to display int count(); //void function to count ~linklist(); //destructor }; //end class

linklist::linklist() //further constructor { p = NULL; //p initially null }

void linklist::append(int num) //void function append { node *q, *t; //points to nodes q and t in list if (p == NULL) //if p is null... { p = new node; //p is new node p->data = num; //assigned num to new node p->link = NULL; //there is no node to link to yet } else //else... { q = p; //q is assigned p while (q->link != NULL) //while q is linked and does not = null q = q->link; //q will assess the link t = new node; //new node named t t->data = num; //new node assigned num t->link = NULL; //t is not linked to another node... q->link = t; //q is now linked to t } } //end append function

void linklist::add_as_first(int num) //void function add as first { node *q; //node pointing to q q = new node; //q is new node q->data = num; //assigning num to new node q->link = p; //new node linked to p p = q; //p assigned q } //end add as first

void linklist::addafter(int c, int num) //void function add after { node *q, *t; //pointing to nodes q and t int i; //declaring i as int for counting for (i = 0, q = p; i { q = q->link; //q is linked if (q == NULL) //if q is not linked and null { cout << " There are less than " << c << " elements."; //error message return as there aren't enough elements to add new item in list return; } //end if } t = new node; //t is a new node t->data = num; //assigning num to the new node t->link = q->link; //assigning both links q->link = t; //linked to new node }

void linklist::del(int num) //void function delete { node *q, *r; //pointing to q and r q = p; //q is assigned to p

if (q->data == num) //if node is = to num { p = q->link; //p is assigned the q link delete q; //will now delete q return; } r = q; //r assigned q while (q != NULL) //while q is null { if (q->data == num) //if data assigned to q is num { r->link = q->link; //assigning both links delete q; //delete the q node return; } r = q; //r is assigned q q = q->link; //q is now linked } cout << " Element " << num << " not Found. "; //output }

void linklist::display() //void function display { node *q; //node pointer to q cout << endl; //display for (q = p; q != NULL; q = q->link) //for q assigned p, q is not null, q is linked. cout << endl << q->data; //will output the data in the q node }

int linklist::count() //void function for counter { node *q; //node pointer for q int c = 0; //initializing the variable c to 0 (the data member it will find) for (q = p; q != NULL; q = q->link) //for q assigned p, q is not null, q is linked c++; //count the amount assigned to c return c; }

linklist::~linklist() //destructor, will destruct members in the list { node *q; if (p == NULL) return; while (p != NULL) { q = p->link; delete p; p = q; } }

int main() //main function to demonstrate functions { linklist newlist; //creates new instance of class cout << "Number of elements = " << newlist.count(); //First will count initial number of elemenents

newlist.append(10); //adding 10 to list newlist.append(13); //adding 13 to list newlist.append(33); //adding 33 to list newlist.append(53); //adding 53 to list newlist.append(64); //adding 64 to list newlist.append(70); //adding 70 to list newlist.add_as_first(4); //will add 4 at beginning of list newlist.add_as_first(2); //will add 2 to beginning of list newlist.addafter(3, 352); //will add number after 3rd data member newlist.addafter(6, 669); //will add number after 6th data member newlist.display(); //displays all items in the list

cout << " Number of elements = " << newlist.count() << " "; //outputs number of elements using count function

newlist.del(352); //deletes the 352 member newlist.del(13); //deletes the 13 member newlist.del(123); //deletes the 123 member

cout << " Number of elements = " << newlist.count() << " " << endl; //outputs new number of elements after deletion of 3 return 0; }

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_2

Step: 3

blur-text-image_3

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

MongoDB Applied Design Patterns Practical Use Cases With The Leading NoSQL Database

Authors: Rick Copeland

1st Edition

1449340040, 978-1449340049

More Books

Students also viewed these Databases questions