美文网首页
代码代码day1

代码代码day1

作者: Emilylxf | 来源:发表于2018-03-24 06:52 被阅读0次

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();

}

output

相关文章

  • 代码代码day1

    Mar.23 题目 Write a program that concatenates two linked li...

  • 2022-04-22

    day1 markdown语法学习-罗wb 已学内容汇总 代码练习 代码分行 代码不分行 A表示95-100,C表...

  • 练习代码day1

    //应用程序代理类 //AppDelegate中的方法都是UIApplicationDelegatez的协议方法 ...

  • 代码大全阅读[day1]

    第二章 隐喻 三点 隐喻的重要性 重大的发现都是从类比中产生,举一反三,通过已知的知识类比到未知的知识上,能够更容...

  • python#zipfile破解密码

    每日下班学习python记录 day1。 破解zip密码的代码比较简单,只需使用zipfile库: import ...

  • Python从新手到大师——07:操作字符串

    代码一 代码二 代码三 代码四 代码五 代码六 代码七 代码八 代码九 代码十

  • 100天代码挑战:DAY1

    LeetCode 303. 区域和检索 - 数组不可变 给定一个整数数组 nums,求出数组从索引 i 到 j ...

  • ruoyi day1 - 初探代码生成

    来看看如何使用代码生成器对某张表进行快速的CRUD开发 1、准备数据表 注意表名不要设置微user、test这种,...

  • Python从新手到大师——10:文件操作

    代码一 代码二 代码三 代码四 代码五 代码六 代码七 代码八

  • Android学习记录(一)

    Day1: 在代码中通过R.string.hello_world可以获得该字符串的引用; 在XML中通过@stri...

网友评论

      本文标题:代码代码day1

      本文链接:https://www.haomeiwen.com/subject/qonfcftx.html