复制单链表成为新的链表,然后return the number of items in the new list
来源:互联网 发布:最近网络疯传的名校 编辑:程序博客网 时间:2024/05/15 09:51
关于单链表的知识点,小编就不在这里介绍了,那么就直接上代码吧!
//This is the list.h file#include<iostream>#include<cctype>#include<cstring>using namespace std;struct node{ int data; node * next;};class list{ public: //These functions already supplied list(); //Supplied ~list(); //Supplied void build(); //Supplied void display(); //Supplied //Copy the contents of a linear linked list and make a new linear linked list //Return the number of items in the new list int copy_list(list & to_copy); private: //Copy the contents of a linear linked list and make a new linked list //Return the number of items in the new list int copy_list(node *& new_head,node*& new_tail, node * head); node * head; node * tail;};
因为建立链表需要用到tail指针,那么在函数的prototype里就得传入新的tail指针来指向新的链表
下面是如何实现这两个函数的代码展示:
//This is the list.cpp file#include "file.h"int list::copy_list(list & to_copy){ return copy_list(to_copy.head,to_copy.tail,head);}int list::copy_list(node *& new_head, node *& new_tail, node * head){ if(!head) { new_head = NULL; return 0; } new_head = new node; new_head->data = head->data; new_tail = new_head; return new_tail->data + copy_list(new_head->next,new_tail,head->next);}
下面是展示如何在主函数里调用函数:
//This is the main.cpp#include "list.h"int main(){ //This is the original list object list object; object.build(); //Builds a LLL object.display(); //displays the LLL //This declares a new object list new_list; //Copy the contents of a linear linked list and make a new linear linked list //Return the number of items in the new list int result = object.copy_list(new_list); cout<<"The result is: "<<result<<endl; new_list.display(); return 0;}
下面就展示结果:
阅读全文
0 0
- 复制单链表成为新的链表,然后return the number of items in the new list
- php5.3 的问题 “Assigning the return value of new by reference is deprecated in xxxx”
- Retrieving the list of items in a directory all at once
- php 报错Deprecated: Assigning the return value of new by reference is deprecated in的解决办法
- php5.3 中显示Deprecated: Assigning the return value of new by reference is deprecated in 的解决方法
- Assigning the return value of new by reference is deprecated in xxxx
- php报错:Deprecated: Assigning the return value of new by reference is deprecated in
- PHP显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- PHP显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- PHP显示Deprecated: Assigning the return value of new by reference is deprecated in解决办法
- Deprecated: Assigning the return value of new by reference is deprecated in……【解决方法】
- Deprecated: Assigning the return value of new by reference is deprecated in……解决方法
- The order of items in struts-config.xml
- Can I change the order of items in category views?
- What are the meaning of the items in the “alignment trap” error message?
- 解决Deprecated: Assigning the return value of new by reference
- lintcode:Number of Airplanes in the Sky
- lintcdoe: Number of Airplanes in the Sky
- best-time-to-buy-and-sell-stock Java code
- 进程的创建-Process子类(python 版)
- 在vue项目中 如何定义全局变量 全局函数
- form表单
- 1024程序员节
- 复制单链表成为新的链表,然后return the number of items in the new list
- Destroying Array CodeForces
- React Native 模仿网易云音乐手机客户端,兼容安卓和IOS两个平台
- form表单样式
- Android lanunmode
- Tableau&BDP,哪个才是最适合中国用户的数据可视化分析工具?
- 从SVN上检出Maven项目报红叉错误,package错误 does not match the expected package "main.java.com.*
- ubuntu下C++程序的编译和运行
- Java中的String