1. 程式人生 > >順序表和連結串列拷貝構造

順序表和連結串列拷貝構造

簡單一部

#include <iostream>
#define DATA int
class SXB
{
	int size;
	int len;
	DATA* p;

public:
	SXB(const SXB& that) // 拷貝構造
	{
		size = that.size;
		len = that.len;
		p = new DATA[size];
		for (int i = 0;i < len; ++i)
			p[i] = that.p[i];
	}
};

struct NODE
{
	DATA data;
	NODE* next;
};
class LinkList
{
	NODE* head;
	int len;
public:

	LinkList(const LinkList& that)
	{
		head = new NODE;
		head->next = 0;

		NODE* pthis = head;
		NODE* pthat = that.head->next;
		while (pthat)
		{
			NODE* nnode = new NODE;
			nnode->data = pthat->data;//資料
			nnode->next = 0;
			pthis->next = nnode;
			pthis = nnode;

			pthat = pthat->next;
		}
		len = that.len;
	}

};