线性表实验二 以失败告终

挣扎了两个星期之后,线性表的实验失败了,败在一个非常郁闷的地方:调试不过。

不知道发什么神经,之后我又用实验书里面的代码试了一下,坑爹的又是调试不过。书上完整代码如下:

(头文件)

#ifndef LinkList_H
#define LinkList_H

template<class DataType>
struct Node
{
	DataType data;
	Node<DataType> * next;
};

template<class DataType>
class LinkList
{
public:
	LinkList();
	LinkList(DataType a[],int n);
	~LinkList();
	int Locate(DataType x);
	void Insert(int i,DataType x);
	DataType Delete(int i);
	void PrintList();
private:
	Node<DataType> * first;
};
#endif

(源文件)

#include<iostream>
using namespace std;
#include"LinkList.h"

template<class DataType>
LinkList<DataType>::LinkList()
{
	first=new Node<DataType>;
	first->next=NULL;
}

template<class DataType>
LinkList<DataType>::LinkList(DataType a[],int n)
{
	Node<DataType> * r,* s;
	first=new Node<DataType>;
	r=first;
	for(int i=0;i<n;i++)
	{
		s=new Node<DataType>;
		s->data=a[i];
		r->next=s;r=s;
	}
	r->next=NULL;
}

template<class DataType>
LinkList<DataType>::~LinkList()
{
	Node<DataType> * q=NULL;
	while(first!=NULL)
	{
		q=first;
		first=first->next;
		delete q;
	}
}

template<class DataType>
void LinkList<DataType>::Insert (int i,DataType x)
{
	Node<DataType> * p=first,* s=NULL;
	int count=0;
	while(p!=NULL && count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL) throw"location";
	else{
		s=new NodeV;
		s->data=s;
		s->next=p->next;p->next=s;
	}
}

template<class DataType>
DataType LinkList<DataType>::Delete (int i)
{
	Node<DataType> * p=first, * q=NULL;
	DataType x;
	int count=0;
	while(p!=NULL && count<i-1)
	{
		p=p->next;
		count++;
	}
	if(p==NULL||p->next==NULL)
		throw"location"
	else{
		q=p->next;x=q->data;
		p->next=q->next;
		delete q;
		return q;
	}
}

template<class DataType>
int LinkList<DataType>::Locate (DataType x)
{
	Node<DataType> * p=first->next;
	int count=i;
	while(p!=NULL)
	{
		if(p->data==x) return count;
		p=p->next;
	}
	return 0;
}

template<class DataType>
void LinkList<DataType>::PrintList ()
{
	Node<DataType> * p=first->next;
	while(p!=NULL)
	{
		cout<<p->data<<" ";
		p=p->next;
	}
	cout<<endl;
}

(”main“源文件)

#include<iostream>
using namespace std;
#include"LinkList.cpp"
void main()
{
	int r[5]={1,2,3,4,5};
	Linklist<int>L(r,5);
	cout<<"show the data that are before inserting: "<<endl;
	L.PrintList();
	try
	{
		L.Insert(2,3);
	}
	catch (char * s)
	{
		cout<<s<<endl;
	}
	cout<<"apply the order that insert the data and show them: "<<endl;
	L.PrintList();
	cout<<"the location of the element whose number is 5 directs: ";
	cout<<L.Locate(5)<<endl;
	cout<<"show the data that are infron the operation of delection: "<<endl;
	L.PrintList();
	try
	{
		L.Delect(1);
	}
	catch(char * s)
	{
		cout<<s<<end
	}
	L.PrintList();	
}


调试错误如下:


F:\c++文件\单链表验证实验\LinkList_main.cpp(7) : error C2065: 'Linklist' : undeclared identifier
F:\c++文件\单链表验证实验\LinkList_main.cpp(7) : error C2062: type 'int' unexpected
F:\c++文件\单链表验证实验\LinkList_main.cpp(9) : error C2065: 'L' : undeclared identifier
F:\c++文件\单链表验证实验\LinkList_main.cpp(9) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(12) : error C2228: left of '.Insert' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(19) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(21) : error C2228: left of '.Locate' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(23) : error C2228: left of '.PrintList' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(26) : error C2228: left of '.Delect' must have class/struct/union type
F:\c++文件\单链表验证实验\LinkList_main.cpp(32) : error C2228: left of '.PrintList' must have class/struct/union type
执行 cl.exe 时出错.

单链表验证实验.exe - 1 error(s), 0 warning(s)


是书上代码不完整么?但事实上它显示的错误是main文件上面的类成员没有定义类型,但是我们都看到在一开始类类型就已经被定义了,并且在main函数中也把类文件引入了(请参看main文件的头文件),这是怎么回事?先前我以为是自己的代码问题,还重写了几次,现在连实验书的代码都出错,而且可以看到,这个错误看起来有些不合理。问题出在什么地方?

顺便说一下,我用的是绿色版,这个跟版本应该无关。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
好的,我可以为您提供一些关于数据结构线性表实验停车场方面的帮助。在停车场管理系统中,可以使用线性表来表示停车场的车位情况,每个车位可以用一个节点来表示。下面是一个简单的示例: ```C++ struct car_node { char car_number[10]; int park_time; }; struct park_node { car_node car; park_node *next; }; class Park { private: int capacity; park_node *head; public: Park(int n) { capacity = n; head = new park_node; head->next = NULL; } bool is_full() { return (size() == capacity); } bool is_empty() { return (head->next == NULL); } int size() { int count = 0; park_node *p = head->next; while (p != NULL) { count++; p = p->next; } return count; } bool park(car_node car) { if (is_full()) { return false; } park_node *p = new park_node; p->car = car; p->next = head->next; head->next = p; return true; } bool leave(char *car_number, int &park_time) { park_node *p = head->next, *pre = head; while (p != NULL && strcmp(p->car.car_number, car_number) != 0) { pre = p; p = p->next; } if (p == NULL) { return false; } pre->next = p->next; delete p; park_time = time(NULL) - p->car.park_time; return true; } }; ``` 在上面的代码中,`Park` 类表示一个停车场,包括车位数量 `capacity` 和车辆信息链表的头节点 `head`。该类提供了以下几个方法: - `is_full()`:判断停车场是否已满 - `is_empty()`:判断停车场是否为空 - `size()`:获取停车场中当前车辆数量 - `park(car_node car)`:停车,将一辆车停放到停车场中 - `leave(char *car_number, int &park_time)`:离开,将一辆车从停车场中取出,并返回停车时长 以上是一个简单的停车场管理系统示例,希望能对您有所帮助。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值