代码代码day1
Mar.23
题目
Write a program that concatenates two linked list objects of characters. The program should include function concatenate, which takes references to both list objects as arguments and concatenates the second list to the first list.
Sample Run
The list is: a b c d e
The list is: f g h i j
Concatenate the 2 lists means appending second list to first list
start the concatenation.
Create a copy of second list
...remove value from copied list: f
...insert at end of first list: f
...remove value from copied list: g
...insert at end of first list: g
...remove value from copied list: h
...insert at end of first list: h
...remove value from copied list: i
...insert at end of first list: i
...remove value from copied list: j
...insert at end of first list: j
Display the copied list.
The list is empty
end the concatenation.
Start the destructor
The list is empty ...
End the destructor.
The first list after concatenation is:
The list is: a b c d e f g h i j
Start the destructor
Destroying nodes ...
f
g
h
i
j
All nodes destroyed
End the destructor.
Start the destructor
Destroying nodes ...
a
b
c
d
e
f
g
h
i
j
All nodes destroyed
End the destructor.
写代码
/*******************************************************************
* CIS22B Exercise 17.1
* Author :
* Description: This program concatenates two list objects of characters.
* linklistc concatenate (linklistc, linklistc)
* Date : Mar.19, 2018
*******************************************************************/
#include<iostream>
#include<string>
using namespace std;
// Specification file for the CharList class
#ifndef CHARLIST_H
#define CHARLIST_H
class CharList{
private:
// Declare a structure for the list
struct ListNode
{
char value; // The value in this node
struct ListNode *next; // To point to the next node
};
ListNode *head;
public:
// Constructor
CharList()
{
head =nullptr;
}
//Destructor
~CharList();
//Linked list operations
void appendNode(char);
void concatenate(CharList &);
void showList();
bool isEmpty();
};
#endif
void CharList::appendNode(char c) {
ListNode *newNode; // To point to a new node, must be a pointer
ListNode *nodePtr; // To move through the list
// Allocate a new node and store char there
newNode =new ListNode;
newNode ->value = c;
newNode ->next =nullptr;
//If there are no nodes in the list
//make newNode the first node.
if(!head)
head = newNode;
else // Otherwise, insert new node at end.
{
// Initialize nodeptr to head of list.
nodePtr =head;
//Find the last node in the list.
while (nodePtr->next)
nodePtr = nodePtr->next;
// Insert newNode as the last node.
nodePtr ->next = newNode;
}
}
void CharList::showList(){
ListNode *nodeptr;
if(!head)
return;
else{
nodeptr =head;
while(nodeptr){
cout<< nodeptr->value << " ";
nodeptr = nodeptr ->next;
}
}
}
bool CharList::isEmpty() {
if(!head)
return true;
else
return false;
}
void CharList::concatenate(CharList &l2){
ListNode *firstNode2; // For tranverse L2
ListNode *lastNode1; // For tranverse list
// ListNode *tempNode; // For get the oringin list l2;
//tempNode = l2.head;
// Find the end of list l1(this list)
if(head)
{
lastNode1 =head;
while(lastNode1->next)
{
lastNode1 = lastNode1->next;
}
}
else
{
lastNode1 =head;
}
// Find the first node of l2
while(l2.head !=nullptr)
{
//Remove the first
firstNode2 = l2.head;
//firstNode2->next = nullptr;
cout<< "...remove value from copied list: " << firstNode2->value <
lastNode1->next = firstNode2;
lastNode1 = firstNode2;
cout<<"...insert at end of first list: " << firstNode2->value << endl;
l2.head = l2.head->next;
}
//cout << "\ntemp:" << tempNode->value<
}
// Destructor
// This function deletes every node in the list.
CharList:: ~CharList() {
ListNode * nodePtr; // To traverse the list
ListNode * nextNode; // To point to the next node
// Position nodePtr at the head of the list.
nodePtr =head;
// While nodePtr is not at the end of the list...
while (nodePtr !=nullptr)
{
//Save a pointer to the next node.
nextNode = nodePtr->next;
// Delete the current node.
delete nodePtr;
// Position nodePtr at the next node.
nodePtr = nextNode;
}
}
int main(){
// Create Char
CharList l1, l2;
l1.appendNode('a');
l1.appendNode('b');
l1.appendNode('c');
l1.appendNode('d');
l1.appendNode('e');
cout<< "The list is: " ;
l1.showList();
l2.appendNode('f');
l2.appendNode('g');
l2.appendNode('h');
l2.appendNode('i');
l2.appendNode('j');
cout<< "\nThe list is: ";
l2.showList();
cout<< endl;
l1.concatenate(l2);
cout<< "\nDisplay the copied list.\n";
l2.showList();
if(l2.isEmpty())
{
cout<< "The list is empty." << endl;
cout<< "\nend the concatenation." <
}
else
cout<< "The list is not empty, there must be sonthing wrong." <
cout<< "Start the destructor"<
cout<< "Destroying nodes... "<
l1.~CharList();
}