C++ For this assignment, you will need to provide these public member functions
ID: 3595535 • Letter: C
Question
C++ For this assignment, you will need to provide these public member functions that perform the following tasks:
appendNode appends a node containing the char value passed, to the end of the list.
insertNode this function inserts a node with the char parameter’s value copied to its char member element. This function must insert the values in descending order. Duplicates are okay.
deleteNode this function searches for a node with char parameter’s value as the element to find. The node, if found, is deleted from the list and from memory. If two or more nodes with the same value are found, only one is removed (your choice which one).
The CharList class member-function definitions have been started for you with the displayList function and the destructor already given. Make sure to use the driver program as well. Find both the driver function and member-function definition files that have been provided in the folder under charList_main.cpp and charList.cpp, respectively. Please use the driver and make sure your program can execute the test cases successfully.
Explanation / Answer
Save this code in files as per your requirement.
/*******************************/
/*
* CharList.h
*
* Created on: 19-Oct-2017
* Author: kuhu
*/
#ifndef CHARLIST_H_
#define CHARLIST_H_
struct node{
char c;
struct node* next;
};
class CharList {
public:
CharList();
void appendNode(struct node** head,char ch);
void insertNode(struct node** head,char ch);
void deleteNode(struct node** head,char ch);
void display(struct node* head);
};
#endif /* CHARLIST_H_ */
/*************************/
/*
* CharList.cpp
*
* Created on: 19-Oct-2017
* Author: kuhu
*/
#include <iostream>
#include "CharList.h"
using namespace std;
CharList::CharList() {
// TODO Auto-generated constructor stub
}
void CharList::insertNode(struct node** head,char ch)
{
struct node* ptr;
if(*head==NULL)
{
ptr=new struct node;
ptr->c=ch;
ptr->next=NULL;
*head=ptr;
}
else
{
ptr=new struct node;
ptr->c=ch;
ptr->next=*head;
*head=ptr;
}
}
void CharList::appendNode(struct node** head,char ch)
{
struct node *ptr,*tmp=*head;
while(tmp->next!=NULL)
{
tmp=tmp->next;
}
ptr=new struct node;
ptr->c=ch;
ptr->next=NULL;
tmp->next=ptr;
}
void CharList::deleteNode(struct node** head,char ch)
{
struct node *ptr=*head;//assign ptr equal to head
if((*head)->c==ch)
{
struct node *tmp=*head;//assign tmp as head
*head=(*head)->next;//move head to next
delete tmp;
}
else
{
while(ptr->next->c!=ch)//check condition
{
ptr=ptr->next;//traverse ptr
}
struct node *tmp=ptr->next;//ptr next assign in tmp
ptr->next=ptr->next->next;//assign ptr next as ptr next next
delete tmp;
}
}
void CharList::display(struct node* head)
{
struct node *tmp=head;
while(tmp!=NULL)
{
cout<<tmp->c<<"->";
tmp=tmp->next;
}
}
/*****************************/
//============================================================================
// Name : main.cpp
// Author :
// Version :
// Copyright : Your copyright notice
// Description : Hello World in C++, Ansi-style
//============================================================================
#include "CharList.h"
#include <iostream>
using namespace std;
struct node* head;
int main() {
CharList cl;
cl.insertNode(&head,'a');
cl.insertNode(&head,'b');
cl.appendNode(&head,'d');
cl.deleteNode(&head,'b');
cl.insertNode(&head,'f');
cl.display(head);
return 0;
}
/***************************/
when you run, here is your output
f->a->d->
You modify the main.cpp file for your own test cases
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.