#include "name.h"
#include "contact.h"
using namespace std;
class ContactList;
typedef class Node* NodePtr;
class Node
{
Contact item;
NodePtr next;
friend class ContactList;
};
class ContactList
{
public:
ContactList(char* clfile) ;
~ContactList();
void display (ostream & output) const;
int insert (Contact record_to_insert);
int insert (ContactList contact_list);
int remove (Contact record_to_delete);
int size () const;
int save () const;
void find_by_lname (ostream & output, string lname) const;
void find_by_fname (ostream & output, string fname) const;
void find (ostream & output, Contact record_to_find ) const;
int make_empty ();
private:
NodePtr head;
int num_items;
char clfile_pathname[512];
bool find( Contact item, NodePtr & where );
bool insert_at( NodePtr & prev, Contact new_item );
};
#include <fstream>
#include <stdlib.h>
#include <string.h>
#include "contactlist.h"
using namespace std;
bool ContactList::insert_at( NodePtr & prev, Contact new_item )
{
NodePtr tmpPtr = new Node;
if ( tmpPtr == NULL )
return false;
tmpPtr->item = new_item;
tmpPtr->next = prev;
prev = tmpPtr;
return true;
}
ContactList::ContactList(char* clfile)
{
ifstream fin;
char line[512];
Contact temp;
num_items = 0;
head = NULL;
strcpy(clfile_pathname, clfile);
fin.open(clfile);
if ( fin.fail()) {
throw "error";
}
while ( !fin.eof() ) {
fin.getline(line, 511);
if ( fin.bad() )
throw "error";
else if ( 0 == temp.set(line) )
continue;
else {
insert(temp);
}
}
}
ContactList::~ContactList()
{
make_empty();
}
void ContactList::find_by_lname (ostream & output,string lname) const
{
NodePtr current;
Name tempname;
if ( NULL == head )
return;
current = head;
while ( current != NULL ) {
current->item.get_name(tempname);
if ( tempname.last() < lname ) {
current = current->next;
}
else if ( tempname.last() > lname ) {
return;
}
else {
output << current->item;
current = current->next;
}
}
}
void ContactList::find_by_fname (ostream & output, string fname) const
{
NodePtr current;
Name tempname;
if ( NULL == head )
return;
current = head;
while ( current != NULL ) {
current->item.get_name(tempname);
if ( tempname.first() != fname ) {
current = current->next;
}
else {
output << current->item;
current = current->next;
}
}
}
void ContactList::find (ostream & output, Contact record_to_find ) const
{
NodePtr current;
if ( NULL != head ) { current = head;
while ( current != NULL && !(current->item > record_to_find)) {
if ( match(current->item, record_to_find) ) {
output << current->item << "\n";
}
current = current->next;
}
}
}
int ContactList::remove( Contact item )
{
NodePtr current;
NodePtr prev;
int count = 0;
if ( NULL != head ) {
current = head;
prev = NULL;
while ( current != NULL ) {
if ( match(current->item, item) ) {
if ( NULL == prev ) {
head = current->next;
current->next = NULL;
delete current;
current = head;
count++;
}
else {
current = current->next;
prev->next->next = NULL;
delete prev->next;
prev->next = current;
count++;
}
}
else {
prev = current;
current = current->next;
}
}
}
num_items -= count;
return count;
}
int ContactList::insert( Contact new_item)
{
NodePtr current, previous;
bool success = false;
if ( NULL == head || head->item > new_item ) {
success = insert_at( head, new_item);
if (success)
num_items++;
return 1;
}
if ( head->item == new_item )
return 0;
previous = head;
current = head->next;
while ( current != NULL && !success ) {
if ( current->item < new_item ) {
previous = current;
current = current->next;
}
else if ( current->item > new_item ) {
if ( (success = insert_at(previous->next, new_item)) )
num_items++;
return 1;
}
else
return 0;
}
if ( (success = insert_at(previous->next, new_item)) )
num_items++;
return 1;
}
int ContactList::insert (ContactList contact_list)
{
NodePtr current = contact_list.head;
int count = 0;
while ( current != NULL ) { count += insert(current->item);
current = current->next;
}
return count;
}
void ContactList::display ( ostream & output) const
{
NodePtr current = head;
while ( current != NULL) {
cout << current->item << "\n";
current = current->next;
}
}
int ContactList::size() const
{
return num_items;
}
int ContactList::make_empty()
{
NodePtr temp_ptr;
for ( int i = 1; i <= num_items; i++){
temp_ptr = head;
head = head->next;
temp_ptr->next = NULL;
delete temp_ptr;
}
return 1;
}
int ContactList::save() const
{
ofstream fout;
char backupfile[512];
char syscmmd[256];
int count = 0;
sprintf(backupfile, "%s.%s", clfile_pathname,"bkp");
sprintf(syscmmd, "mv -f %s %s", clfile_pathname, backupfile );
if ( system (syscmmd ) < 0 )
return -1;
fout.open(clfile_pathname);
if ( fout.fail() )
return -2;
NodePtr current = head;
while ( current != NULL) {
fout << current->item.convert2csv() << "\n";
current = current->next;
count++;
}
fout.close();
return count;
}
------------------------------
1.Put the letters of the true statements on the
answersheet with the reason.
(a) The ContactList is a linked list data structure.
(b) The ContactList is maintained in sorted order, by last name
only.
(c) The ContactList is not in sorted order.
(d) The ContactList is in sorted order using the last name as a
first key and the first name as a second key.
(a) True . The ContactList is a linked list data structure where
Contact nodes are used for insertion and deletion operations.
(c) True. The ContactList is not in sorted order because the
insert_at () function is used to insert new Contact node at a
particular position and insert() function is used to insert new
contact node at the end of the list.
Do ask if any doubt. Please upvote.
#include "name.h" #include "contact.h" using namespace std; class ContactList; typedef class Node* NodePtr; class Node {...
Please rewrite this function using recursive function #include using namespace std; struct Node { char ch; Node* next; }; class LinkedList { Node* head; public: LinkedList(); ~LinkedList(); void add(char ch); bool find(char ch); bool del(char ch); friend std::ostream& operator<<(std::ostream& out, LinkedList& list); }; LinkedList::LinkedList() { head = NULL; } LinkedList::~LinkedList() { Node* cur = head, * tmp; while (cur != NULL) { tmp = cur->next; delete cur; cur = tmp; } } void LinkedList::add(char ch) { Node* cur = head,...
// Name.h #ifndef __NAME_H__ #define __NAME_H__ #include <iostream> #include <string> using namespace std; #define MAXLENGTH 12 #define NAME_CHARS "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ-'" namespace Errors { struct InvalidName { }; } class Name { public: Name ( string first_name = "", string last_name = ""); string first() const; string last() const; void set_first( string fname ); void set_last( string lname); friend ostream& operator<< ( ostream & os, Name name ); friend bool operator== (Name name1, Name...
CONVERT THE FOLLOWING C/C++ PROGRAM INTO JAVA: //LinkedString.h #pragma once #include<iostream> #include<string> using namespace std; //declare a node datastruct struct Node { char c; Node *next; }; class LinkedString { Node *head; public: LinkedString(); LinkedString(char[]); LinkedString(string); char charAt(int) const; string concat(const LinkedString &obj) const; bool isEmpty() const; int length() const; LinkedString substring(int, int) const; //added helper function to add to linekd list private: void add(char c); };...
#include <iostream> using namespace std; struct ListNode { float value; ListNode *next; }; ListNode *head; class LinkedList { public: int insertNode(float num); void deleteNode(float num); void destroyList(); void displayList(); LinkedList(void) {head = NULL;} ~LinkedList(void) {destroyList();} }; int LinkedList::insertNode(float num) { struct ListNode *newNode, *nodePtr = head, *prevNodePtr = NULL; newNode = new ListNode; if(newNode == NULL) { cout << "Error allocating memory for new list member!\n"; return 1; } newNode->value = num; newNode->next = NULL; if(head==NULL) { cout << "List...
C++ comment code Comment the following code #include <iostream> using namespace std; class Node { public: Node(int val); int value; Node* next; }; Node::Node(int val){ value = val; } class List { public: List(); // Uncomment the line below once you're ready List(List &other); void push_front(int value); bool pop_front(int &value); void push_back(int value); bool pop_back(int &value); int at(int index); void insert_at(int index, int value); void remove_at(int index); int size(); private: // other members you may have used Node* head; Node*...
C++ program: Convert the classes to template classes #include <iostream> #include <string> using namespace std; class Node { private: int data; Node* next; public: Node(int data) { this->data=data; this->next = 0; } int getData(){return data;} Node* getNext(){return next;} void setNext(Node* next){this->next=next;} }; class LinkedList { private: Node* head = 0; public: int isEmpty() {return head == 0;} void print() { Node* currNode = head; while(currNode!=0) { cout << currNode->getData() << endl; currNode = currNode->getNext(); } } void append(int data) {...
C++ Create a class that implements a sorted, doubly-linked list: Start with a copy of the sortedList class. Call your new class doublyLinkedList. Convert the baseline code into a doubly linkedlist, and thoroughly test all existing operations (make sure to check all edge conditions), and then implement the new operations below. The class should have the following additional class methods: • A reverse method: this method will reverse the order of the doubly linked list. This method takes no parameters,...
Using the provided Linked List template, add the following recursive functions and demonstrate them in a separate cpp file. Write a recursive function to print the list in order. Write a recursive function to print the list in reverse order. Write a recursive function to print every other node in the list in order. Write a recursive function to return the number of nodes in the list. Write a Boolean function that implements the recursive version of sequential search. THIS...
#include <iostream>
#include <string>
#include <cstring>
using namespace std;
class Node{
private:
int data;
Node* nextNodePtr;
public:
Node(){}
void setData(int d){
data = d;
}
int getData(){
return
data;
}
void setNextNodePtr(Node*
nodePtr){
nextNodePtr = nodePtr;
}
...
#include "stdafx.h" #include <iostream> using namespace std; class dateType { private: int dmonth; int dday; int dyear; public: void setdate (int month, int day, int year); int getday()const; int getmonth()const; int getyear()const; int printdate()const; bool isleapyear(int year); dateType (int month=0, int day=0, int year=0); }; void dateType::setdate(int month, int day, int year) { int numofdays; if (year<=2008) { dyear=year;...