Question

C++ PROGRAMMING Submit the code for each problem separately. Important: Please place all appropriate coding comments...

C++ PROGRAMMING

Submit the code for each problem separately.

Important: Please place all appropriate coding comments inside of the code.

Problem 1    Create a simple linked list program to create a class list containing

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

};

Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be able to display the contents of the list.

Write a little driver program with at least 5 values passed in (so that 5 nodes are created) as you insert/append, delete and display data, showing the programs operation.

Output: While displaying the data, make sure you use an informational label–using the terms “insert”, “append”, “remove” and any other term which displays the action

Homework Answers

Answer #1

main.cpp:

#include <iostream>
using namespace std;

// node class
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;
   }
};

// list class
// support operation
//   1. insert
//   2. append
//   3. find
//   4. display
//   5. remove
class list {
node *head; // head of the list
node *tail;
int nNode; // number of nodes in list
public:
list ( ) {
       nNode = 0; // set number of nodes to 0
       head = 0; // set head poitner to 0
       tail = 0; // set tail pointer to 0
   }
  
   // remove node at given index
void remove (int index){
   if (nNode == 0 || index >= nNode){
       // if list is empty do nothing
       return;
       }
      
       if (index == 0){ // if index is 0
           // remove head node
           head = head->get_next();
           nNode--;
           return;
       }
      
       if (index == nNode - 1){ // if index is index of last element
           // remove tail
           tail->put_next(0);
           nNode--;
           return;
       }
      
       // if index is not zero remove node at given index
       node *prev = head;
       node *curr = head->get_next();
       int i=1;
       while(curr != 0){
           if ( i == index){
               prev->put_next(curr->get_next());
               nNode--;
               break;
           }
           i++;
           prev = curr;
           curr = curr->get_next();
       }
   }
  
   // insert node at gievn position
void insert (void *v, int index){
   node *newNode = new node(v); // create new node

   if (index == 0 || nNode == 0){
       // if index is 0 or list is empty
       // insert node as head of list
       nNode++;
           newNode->put_next(head);
           head = newNode;
           return;
       }
      
       // otherwise insert node at given index
       nNode++;
       node *prev = head;
       node *curr = head->get_next();
       int i=1;
       while(curr != 0){
           if ( i == index){
               break;
           }
           i++;
           prev = curr;
           curr = curr->get_next();
       }
       newNode->put_next(curr);
       prev->put_next(newNode);
   }
  
   // append a node at end of the list
   void append (void * v){
       node *newNode = new node(v); // create new node
   if (nNode == 0){ // if list is empty insert node as head
           tail = newNode;
           head = newNode;
           nNode++;
           return;
       }
       nNode++;
       tail->put_next(newNode);
       tail = newNode;
   }
  
   // get info of node at given position
   void *find (int index){
       if (index >= nNode){
           return 0;
       }
       node *curr = head;
       int i=0;
       while (curr != 0){
           if(i == index){
               return curr->get_info();
           }
           curr = curr->get_next();
       }
   }
  
   // display info of all node
void display ( ){
   node *curr = head;
   while (curr != 0){
       cout<<(*(int*)curr->get_info())<<" ";
       curr = curr->get_next();
       }
   }
};

int main(){
   list l1; // list
  
   // append 5 element into list
   for (int i=0; i<5; i++){
       cout<<"append: "<<(i+1) * 13<<endl;
       l1.append(new int((i+1) * 13));
   }
   cout<<endl;
   cout<<"list: ";
   l1.display();
   cout<<endl<<endl;
  
   cout<<"Insert 88 at index 2: \n";
   l1.insert(new int(88), 2);
   cout<<"list: ";
   l1.display();
   cout<<endl<<endl;
  
   l1.remove(2);
   cout<<"After removing element from index 2, \nlist: ";
   l1.display();
}

Output:


Know the answer?
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
Similar Questions
Using C++ / provide code comments so I can understand. Create a simple linked list program...
Using C++ / provide code comments so I can understand. Create a simple linked list program to create a class list containing 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;} }; Be able to initially fill the list. Provide functions to insert/append nodes and remove nodes from the linked list. Be...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix...
Data Structure in C++ I keep getting the same warning, and I cant seem to fix it.. Can you explain to me what I am doing wrong? Warning: dlist.cc: In function 'std::ostream& operator<<(std::ostream&, dlist&)': dlist.cc:66:10: error: invalid initialization of reference of type 'std::ostream& {aka std::basic_ostream&}' from expression of type 'dlist::node*' dlist.cc: In function 'dlist operator+(dlist&, dlist&)': dlist.cc:93:8: error: invalid operands of types 'dlist::node*' and 'dlist::node*' to binary 'operator+' dlist.cc:97:8: error: could not convert 'result' from 'int' to 'dlist' My code:...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {...
Please explain code 1 and code 2 for each lines code 1 public class MyQueue {    public static final int DEFAULT_SIZE = 10;    private Object data[];    private int index; code 2 package test; import java.util.*; /* Class Node */ class Node { protected Object data; protected Node link; /* Constructor */ public Node() { link = null; data = 0; } /* Constructor */ public Node(Object d,Node n) { data = d; link = n; } /*...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I...
I'm not sure how to fix my code I keep getting an error with rhs.begin. I linked the header file and the test file, THESE TWO CANNOT be changed they have absolutely no errors in them. To clarify I ONLY need help with the copy constructor part. /* ~~~~~~~~~~~~list.cpp~~~~~~~~~~~~~~~*/ #include #include "list.h" using namespace std; Node::Node(string element) { data = element; previous = nullptr; next = nullptr; } List::List() { first = nullptr; last = nullptr; } List::List(const List& rhs)//...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is...
IN C++ PLEASE!!! What needs to be done is in the code itself where it is written TO DO List! #include<iostream> using namespace std; template<typename T> class DoubleList{​​​​​     class Node{​​​​​     public: T value; Node* next; Node* prev;         Node(T value = T(), Node* next = nullptr, Node* prev = nullptr){​​​​​             this->value = value;             this->next = next;             this->next = next;         }​​​​​     }​​​​​;     int size;     Node* head;     Node* tail; public:     DoubleList(){​​​​​         size = 0;         head = nullptr;     }​​​​​     int length(){​​​​​         return size;     }​​​​​...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node...
public class DoublyLinkedList { Node Head; // head of Doubly Linked List //Doubly Linked list Node class Node { int value; Node prev; Node next; // Constructor to create a new node Node(int d) { value = d; } } // Inserting a node at the front of the list public void add(int newData) { // allocate node and put in the data Node newNode = new Node(newData); // Make the next of new node as head // and previous...
i want to complete this code to insert a new node in the middle of list...
i want to complete this code to insert a new node in the middle of list (take a node data from user, search the node and insert new node after this node). this is the code #include <iostream> #include <stdlib.h> using namespace std ; struct Node{                int data;                Node *link ;}; struct Node *head=NULL, *tail=NULL; /* pointers to Node*/ void InsertFront(); void InsertRear(); void DeleteFront(); void DeleteRear(); int main(){                int choice;                do{                               cout << "1:...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>...
Data Structures using C++ Consider the following class #ifndef LINKEDQUEUETYPE_H #define LINKEDQUEUETYPE_H #include <iostream> #include <new>    #include <cstdlib> #include "QueueADT.h" using namespace std; // Definition of the node template <class ItemType> struct NodeType {        ItemType info;        NodeType<ItemType> *next; }; template <class ItemType> class LinkedQueueType: public QueueADT<ItemType> { public:        // Constructor        LinkedQueueType();           // Default constructor.           // Post: An empty queue has been created. queueFront = NULL;           //       queueBack = NULL;...
The output only produces the values from the first linkedlist and not the second. Please fix...
The output only produces the values from the first linkedlist and not the second. Please fix the code to produce the desired objective. Thanks for your help! Objective: Interleave Example: Define the calling list as a set of ordered nodes, $L1 = {4, 2, 8 ,5, 8}$, and define the list that is passed as a parameter as a set of ordered nodes, $L2 = {5, 1, 8, 4, 5, 9}$. L1.interleave(L2) yields the set ${4, 5, 2, 1, 8,...