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

C++ Linked List My Code: #include <iostream> #include <stdlib.h> #include<string

ID: 3560465 • Letter: C

Question

C++ Linked List

My Code:

#include <iostream>
#include <stdlib.h>
#include<string.h>
using namespace std;


typedef struct node{
   char id[20];
   char name[20];
   char prog[20];
   struct node *next;
}studentnode;


class StudentList{
   studentnode *head;

public:
StudentList(){
       head = NULL;
   }

   /* add a node at the end of the list */
   void append( char* name, char* id, char* prog){
       studentnode *temp, *nodeptr;
       temp = new studentnode;
       strcpy(temp->name, name);
       strcpy(temp->id, id);
       strcpy(temp->prog, prog);

       temp->next = NULL;
       if(head == NULL)
           head = temp;
       else
       {
           nodeptr = head;
           while(nodeptr->next!=NULL)
               nodeptr = nodeptr->next;
           nodeptr->next = temp;
       }
   }

   /* display the whole linked list */
   void display(){
       studentnode *nodeptr;
       nodeptr = head;
       if((nodeptr->next == NULL))
           cout << " " << nodeptr->id;
       else
       {
           while(nodeptr->next!=NULL)
           {
               cout << " Student Name: " << nodeptr->name;
               cout << " Student ID: " << nodeptr->id;
               cout << " Programme: " << nodeptr->prog << " ";
               nodeptr = nodeptr->next;
           }
          cout << " Student Name: " << nodeptr->name;
          cout << " Student ID: " << nodeptr->id;
          cout << " Programme: " << nodeptr->prog << " ";
       }
   }


   /* search for a particular value of a node in the list */
   void search(char* id){
       int found = 0;
       studentnode *nodeptr;
       nodeptr = head;
       while(nodeptr!=NULL)
       {
           if(strcmp(nodeptr->id,id) == 0)
           {
               found = 1;
               cout << " Status: Student Found. ";
               cout << " Student Name: " << nodeptr->name;
               cout << " Student ID: " << nodeptr->id;
               cout << " Programme: " << nodeptr->prog;
               cout << " ";
           }
           nodeptr = nodeptr->next;
       }
       if(found != 1)
           cout << " Status: Error. Student does not exist. ";
   }

   /* delete a specified node */
   void remove(char *id){
       studentnode *nodeptr, *temp;
       int found = 0;

       nodeptr = head;
       if((strcmp(nodeptr->id,id) == 0) && (nodeptr->next == NULL))
       {
           delete head;
           head = NULL;
           found =1;
       }
       else
       {
           while(nodeptr->next!=NULL)
           {
               if((strcmp(nodeptr->id,id)==0) && (nodeptr == head))
               {
                       head = head->next;
                       found = 1;
               }
               else if(strcmp(nodeptr->id,id)==0)
               {
                   if(nodeptr->next->next == NULL)
                   {
                       temp = nodeptr->next;
                       nodeptr->next = NULL;
                       found = 1;
                       break; /* Because the code nodeptr = nodeptr->next will cause an error */
                   }
                   else
                   {
                       temp = nodeptr->next;
                       nodeptr->next = nodeptr->next->next;
                   }
                   delete temp;
                   found = 1;
               }
               nodeptr = nodeptr->next;
           }
       }
       if(found == 0){
           cout << " Status: Error. Student does not exist. ";
       } else {
           cout << " Status: Data deleted. ";
       }
   }

};

/* Stack display */
class LinkedListStack{
   studentnode *first;
public:
   LinkedListStack(){
       first = NULL;
   }

   void push( char* name, char* id, char* prog ){
       studentnode *temp = new studentnode;
       if(temp == NULL){
           cout << " Exhausted momory!";
       }else{
           strcpy(temp->name, name);
           strcpy(temp->id, id);
           strcpy(temp->prog, prog);
           temp->next = first;
           first = temp;
       }
   }

   void display(){
       studentnode *temp;
       temp = first;
       if(first==NULL){
           cout << " Empty Stack";
       }else{
           while(temp!=NULL){
               cout << " Student Name: " << temp->name;
               cout << " Student ID: " << temp->id;
               cout << " Programme: " << temp->prog << " ";
               temp = temp->next;
           }
       }
       cout << " ";
   }

   int isEmpty(){
       if(first == NULL)
           return 1;
       else
           return 0;
   }

};


int main(){

   StudentList sl;
   LinkedListStack st;

   int ch1, ch2, ch3, n;

   char name[20], prog[20], id[20];

   cout << " Welcome to Date Structure and Algorithm." << endl;
   cout << " Please select your option ";
   cout << " 1. Linked List";
   cout << " 2. Exit ";
   cout << " Selection: ";
   cin >> ch1;

   switch(ch1){
       case 1:
           cout << " Selection:1 - Insert new Data ";
           cout << " How many do you want to insert?: ";
           cin >> n;
           cout << " ";

           for(int i = 0; i < n; i++ ){
               cout << "Student Name: ";
               cin>>name;
               cout << "Student ID: ";
               cin>>id;
               cout << "Programme: ";
               cin>>prog;
               sl.append( name, id, prog );
               st.push( name, id, prog );
               cout << " ";
           }

           cout << "Enter:2->Delete Data, 30>Search Data, 4->Add new Data (existing list), 5->Display Information, 0->Exit ";
           cout << "Selection: ";
           cin >> ch2;
           while( ch2 ){
               switch(ch2){

                   case 2:
                       cout << " Selection:2 - Delete Data ";
                       cout << " Enter student ID to be deleted ";
                       cout << " Student ID: ";
                       cin >> id;
                       sl.remove(id);
                       break;
                   case 3:
                       cout << " Selection:3 - Search Data ";
                       cout << " Enter student ID to search ";
                       cout << "Student ID: ";
                       cin >> id;
                       sl.search(id);
                       break;
                   case 4:
                       cout << " Selection:4 - Add New Node ";
                       cout << " How many data do you want to add?: ";
                       cin >> n;
                       cout << " ";

                       for(int i = 0; i < n; i++ ){
                           cout << "Student Name: ";
                            cin >> name;
                           cout << "Student ID: ";
                            cin >> id;
                           cout << "Programme: ";
                            cin >> prog;
                           sl.append( name, id, prog );
                           st.push( name, id, prog );
                           cout << " ";
                       }
                       break;
                   case 5:
                       cout << " Selection:5 - Display Information ";
                       cout << " 1. Queue Method" ;
                       cout << " 2. Stack Method ";
                       cout << " Selection: ";
                       cin >> ch3;
                       switch(ch3){
                       case 1:
                           sl.display();
                           break;
                           case 2:
                               st.display();
                           break;
                           default:
                           cout << "Error: Invalid choice. ";
                               break;
                       }
                       break;
               }
               cout << "Enter:2->Delete Data, 30>Search Data, 4->Add new Data (existing list), 5->Display Information, 0->Exit ";
               cout << "Selection: ";
               cin >> ch2;
           }
           break;
   }
   return 0;
}

Why delete also cannot find last node?
And display function problem, show the same node after delete node.

Please modify my code.
Please comment why explain my error.

Thank you.

Explanation / Answer

By this program you can delete last node

/**
* A sample code of removing a node from a linked list.
* In this examples, we try to remove a node with a given value from
* a linked list if it exists.
*/

#include <iostream>
#include <cstddef>

using std::cout;
using std::endl;

/*
A linked list is a list constructed using pointers. It is not fixed in
size and could grow and shrink while the program is running.

/* definition of the list node class */
class Node
{
    friend class LinkedList;
private:
    int _value; /* data, can be any data type, but use integer for easiness */
    Node *_pNext; /* pointer to the next node */
  
public:
    /* Constructors with No Arguments */
    Node(void)
    : _pNext(NULL)
    { }
  
    /* Constructors with a given value */
    Node(int val)
    : _value(val), _pNext(NULL)
    { }
  
    /* Constructors with a given value and a link of the next node */
    Node(int val, Node* next)
    : _value(val), _pNext(next)
    {}
  
    /* Getters */
    int getValue(void)
    { return _value; }
  
    Node* getNext(void)
    { return _pNext; }
};

/* definition of the linked list class */
class LinkedList
{
private:
    /* pointer of head node */
    Node *_pHead;
    /* pointer of tail node */
    Node *_pTail;
  
public:
    /* Constructors with a given value of a list node */
    LinkedList(int val);
    /* Destructor */
    ~LinkedList(void);
  
    /* Function to append a node to the end of a linked list */
    void tailAppend(int val);
  
    /* Remove a node with a specific value if it exists */
    void remove(int val);
  
    /* Traversing the list and printing the value of each node */
    void traverse_and_print();
};

LinkedList::LinkedList(int val)
{
    /* Create a new node, acting as both the head and tail node */
    _pHead = new Node(val);
    _pTail = _pHead;
}

LinkedList::~LinkedList()
{
    /*
     * Leave it empty temporarily.
     * It will be described in detail in the example "How to delete a linkedlist".
     */
}

void LinkedList::tailAppend(int val)
{
    /* The list is empty? */
    if (_pHead == NULL) {
        /* the same to create a new list with a given value */
        _pTail = _pHead = new Node(val);
    }
    else
    {
        /* Append the new node to the tail */
        _pTail->_pNext = new Node(val);
        /* Update the tail pointer */
        _pTail = _pTail->_pNext;
    }
}

void LinkedList::remove(int val)
{
    Node *pPre = NULL, *pDel = NULL;
  
    /* Check whether it is the head node?
     if it is, delete and update the head node */
    if (_pHead->_value == val) {
        /* point to the node to be deleted */
        pDel = _pHead;
        /* update */
        _pHead = pDel->_pNext;
        delete pDel;
        return;
    }
  
    pPre = _pHead;
    pDel = _pHead->_pNext;
  
    /* traverse the list and check the value of each node */
    while (pDel != NULL) {
        if (pDel->_value == val) {
            /* Update the list */
            pPre->_pNext = pDel->_pNext;
            /* If it is the last node, update the tail */
            if (pDel == _pTail) {
                _pTail = pPre;
            }
            delete pDel; /* Here only remove the first node with the given value */
            break; /* break and return */
        }
        pPre = pDel;
        pDel = pDel->_pNext;
    }
}

void LinkedList::traverse_and_print()
{
    Node *p = _pHead;
  
    /* The list is empty? */
    if (_pHead == NULL) {
        cout << "The list is empty" << endl;
        return;
    }
  
    cout << "LinkedList: ";
    /* A basic way of traversing a linked list */
    while (p != NULL) { /* while there are some more nodes left */
        /* output the value */
        cout << p->_value << " ";
        /* The pointer moves along to the next one */
        p = p->_pNext;
    }
    cout << endl;
}

int main(int argc, const char * argv[])
{
    /* Create a list with only one node */
    LinkedList list(1);
    /* Append 3 nodes to the end of the list */
    list.tailAppend(2);
    list.tailAppend(3);
    list.tailAppend(4);
    cout << "Before removing: " << endl;
    /* output the result */
    list.traverse_and_print();
  
    /* Remode the node with value 3 */
    cout << "Trying to remove the node with value 3" << endl;
    list.remove(3);
    /* output the result */
    cout << "After removing: " << endl;
    list.traverse_and_print();
  
    return 0;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Chat Now And Get Quote