Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Create a simple linked list program to create a class list containing class node

ID: 3838805 • Letter: C

Question

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.

Explanation / Answer

#include "stdafx.h"
#include <iostream>
#include <cstdlib>
#include <cstring>

using namespace std;

class list
{
private:
   int data;
   list* next;
public:
   void insert(int data);
   void deletes(int data);
   void print();
};
list* head;

void list::print() {
   cout << "This is the list:" << endl;

  
   list *tmp = head;

  
   if (tmp == NULL) {
       cout << "list is empty" << endl;
       return;
   }

   // One node in the list
   if (tmp->next == NULL) {
       cout << "data: " << tmp->data;
  
   }
   else {
   //   print the list
       while (tmp != NULL) {
           cout << "data: " << tmp->data;
           tmp = tmp->next;
       }
   }
}

void list::insert(int data) {
   // Create a new list
   list* newlist = new list;
   newlist->data = data;

   newlist->next = NULL;

   // temp pointer
   list *tmp = head;

   if (tmp != NULL) {
       while (tmp->next != NULL) {
           tmp = tmp->next;
       }

       // pointer the last node to the new node
       tmp->next = newlist;
   }
   else {
       // head node
       head = newlist;
   }
}

void list::deletes(int num) {
   int k = 0;
   list* tmp = head;
   if (tmp == NULL)
       return;
   //Last node

   if (tmp->next == NULL && tmp->data == num) {
       delete tmp;
       head = NULL;
   }
   else {
       //parsing thru the nodes
       list* prev;
       prev = new list;
       while (tmp != NULL)
       {
           if (tmp->data == num && k == 0)
               head = head->next;
           if (tmp->data == num)
               break;
           prev = tmp;

           tmp = tmp->next;
           k++;
       }

       //Adjust pointers
       if(tmp)
       prev->next = (tmp->next);
       //delete the current node
       delete tmp;
       delete prev;

   }
}

// main function which call the application
// print function will print elements in the list
// insert function will delete elements in the list
// delete function will insert elements in the list
int main()
{
   head->print();
   head->insert(10);
   head->insert(20);
   head->insert(30);
   head->print();
   head->deletes(10);
   head->print();
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote