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
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:...
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;     }​​​​​...
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 Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class...
The Binary Search Tree implementation for bst.zip. The code in the destructor of the BST class is empty. Complete the destructor so the memory allocated for each node in the BST is freed. Make a couple of different trees in your main method or in a function to test the destructor (the program should not crash upon exiting). bst.zip (includes the following files below in c++): bst.h: #pragma once #include #include "node.cpp" using namespace std; template class BST { public:...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
my code has several functions; delete and backward functions are not working, rewrite the code for...
my code has several functions; delete and backward functions are not working, rewrite the code for both functions and check them in the main: #include<iostream> #include<cassert> using namespace std; struct nodeType {    int info;    nodeType *link; }; class linkedList { public:    void initializeList();    bool isEmptyList();    void print();    int length();    void destroyList();    void insertFirst(int newItem);    void insertLast(int newItem);    int front();    linkedList();    void copyList(const linkedList otherList);    void insertNewValue(int value);...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building...
Project 1 - NodeList write in c++ with 5 files: main.cpp List.h List.cpp ListNode.h ListNode.cpp Building upon the the ListNode/List code I would like you to extend the interface of a list to have these member functions as well. struct ListNode { int element; ListNode *next; } Write a function to concatenate two linked lists. Given lists l1 = (2, 3, 1)and l2 = (4, 5), after return from l1.concatenate(l2)the list l1should be changed to be l1 = (2, 3,...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if...
Helllp plz Allow the InsertAt method to add to the front position 0 (zero) also if you try to add to a position greater than the number of nodes you get an error warning. /INSERT.C /*THIS PROGRAM READS A BINARY FILE (MUST ALREADY EXIST AND BE FILLED) */ /*AND PUTS IT INTO A LINKED LIST AND PRINTS THE LIST TO THE SCREEN) */ #include #include #include #include typedef struct ENTRY { char name[81]; }ENTRY; typedef struct LISTREC /* LISTREC is...