⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 wex9_5.cpp

📁 数据结构C++代码,经典代码,受益多多,希望大家多多支持
💻 CPP
字号:
#include <iostream.h>
#pragma hdrstop

#include "node.h"
#include "nodelib.h"

// make a copy of the linked list with head p
// and return a pointer to its first node
template <class T>
Node<T> *Copy(Node<T> *p)
{
	Node<T> *front, *q, *newNode;
	
	// return if the list is empty
	if (p == NULL)
		return NULL;

	// create the first node and assign its address to front
	front = new Node<T>(p->data);
	// use q to build the new list and p to traverse
	// the remainder of the existing one
	q = front;
	p = p->NextNode();
	
	// traverse original list from 2nd node to the end
	while(p != NULL)
	{
		// create new node with same data value as p
		newNode = new Node<T>(p->data);
		// insert new node after current end of new list
		q->InsertAfter(newNode);
		// move the two pointers forward
		q = newNode;
		p = p->NextNode();
	}

	// return the address of the first node in the copy
	return front;
}

void main(void)
{
	// create list1 and make copy with head list2
	Node<int> *list1 = NULL, *list2;
	
	// create the list 1 2 3 ... 10
	for(int i=10;i >= 1;i--)
		InsertFront(list1,i);
	// print the original list
	PrintList(list1);
	cout << endl;
	
	// make a copy of list1 and print it
	list2 = Copy(list1);
	PrintList(list2);
	cout << endl;
}

/*
<Run>

1  2  3  4  5  6  7  8  9  10  
1  2  3  4  5  6  7  8  9  10  
*/

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -