约瑟夫环实验报告

// 约瑟夫环.cpp : 定义控制台应用程序的入口点。
//

#include "stdafx.h"
#include
#include "LinkList.h"
using namespace std;

template
void Josephus(Node *first, int n)
{
	Node *temp = NULL;
	cout << "离座顺序: ";
	while(first->next != first)
	{
		Node *p = first;		
		for(int j=1;jnext;
			j++;		
		}
		cout << p->next->data << "  ";
		first = p->next->next;
		temp = p->next;
		p->next = first;
		delete temp;
	}
	cout << first->data << endl; 
}
/*错误算法:delete p;后又引用p*/
//template
//void Josephus(Node *first, int n)
//{
//	Node *temp = NULL;
//	cout << "离座顺序: ";
//	while(first)
//	{
//		Node *p = first;		
//		for(int j=1;jnext;
//			j++;		
//		}
//		cout << p->data << "  ";
//		first = p->next->next;
//		temp = p->next;
//		p->next = first;
//		delete temp;
//	}
//	//cout << first->data << endl; 
//}
		

int _tmain(int argc, _TCHAR* argv[])
{
	char a[]={'a','b','c','d'};
	int n;
	LinkList st(a,sizeof(a));
	st.PrintList();
	cout << "请输入n: ";
	cin >> n;
	Josephus(st.GetFirst(),n);
	return 0;
}

// LinkList.h 
//
#pragma once

template
struct Node  
{
	T data;
	struct Node *next;
};

template
class LinkList
{
public:
	LinkList(void);
	LinkList(T a[],int n);
	void PrintList();
	Node *GetFirst();
	~LinkList(void);
private:
	Node *first;
};

template
LinkList::LinkList(void)
{
	first = NULL;
}

template
LinkList::LinkList(T a[], int n)
{
	first = new Node;
	first->data = a[0];
	first->next = first;

	Node *rear = first; 
	Node *temp; 
	for(int i=1; i;
		temp->data = a[i];
		temp->next = rear->next;
		rear->next = temp;
		rear = temp;
	}
}

template
LinkList::~LinkList(void)
{
	/*Node *temp = first;
	while(temp)
	{
		first = temp;
		temp = temp->next;
		delete first;		
	}*/
}

template
void LinkList::PrintList()
{
	if(!first)cout << "单链表为空" << endl;
	else
	{
		cout << first->data << "  ";
		Node *temp = first->next;
		while(temp != first)
		{
			cout << temp->data << "  ";
			temp = temp->next;
		}
		cout << endl;
	}
}

template
Node *LinkList::GetFirst()
{
	return first;
}

实验输出:



实验错误及心得:

1,不能将模版类的实现和声明放在不同的文件里,否则报错:error LNK2019: 无法解析的外部符号。。。该符号在函数 _wmain 中被引用

1>约瑟夫环.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall LinkList::~LinkList(void)" (??1?$LinkList@D@@QAE@XZ),该符号在函数 _wmain 中被引用
1>约瑟夫环.obj : error LNK2019: 无法解析的外部符号 "public: struct Node * __thiscall LinkList::GetFirst(void)" (?GetFirst@?$LinkList@D@@QAEPAU?$Node@D@@XZ),该符号在函数 _wmain 中被引用
1>约瑟夫环.obj : error LNK2019: 无法解析的外部符号 "public: void __thiscall LinkList::PrintList(void)" (?PrintList@?$LinkList@D@@QAEXXZ),该符号在函数 _wmain 中被引用
1>约瑟夫环.obj : error LNK2019: 无法解析的外部符号 "public: __thiscall LinkList::LinkList(char * const,int)" (??0?$LinkList@D@@QAE@QADH@Z),该符号在函数 _wmain 中被引用
1>c:\users\shenqi67\documents\visual studio 2010\Projects\约瑟夫环\Debug\约瑟夫环.exe : fatal error LNK1120: 4 个无法解析的外部命令


2,若指针p的当前值为0xabcdefg8,则delete p;后,p的当前值仍然为0xabcdefg8。只是它所指向的内存已经释放(如何理解释放?)。比如本例中的temp指针变量:

delete temp;后


3,从上图和下图可以看出0xfeeefeee和0xfeeefef2分别是delete后的temp指向的结构体的next域和data域。说明本机器是大端存储。

若将44行改为cout << p->data << "  ";则异常如下:


若将44行改为cout << p->next << "  ";异常和输出如下:



若将44行改为cout << p->next->data << "  ";异常如下:


综上说明:(1)程序碰到异常就会弹出对话框询问(2)被delete的指针变量所指向的单元依然为一个结构体单元,且指针域占有四个字节。(3)指针也是一个数字,且可以被输出


你可能感兴趣的:(c++数据结构,C++)