单链表 错误找不出来
来源:互联网 发布:js数组find方法举例 编辑:程序博客网 时间:2024/04/18 05:40
LinkList.h:
#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
LinkList.cpp:
#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"位置";
else{
s=new Node<DataType>;
s->data=x;
s->next=p->next;p->next=s;
}
template<class 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"位置";
else{
q=p->next;
x=q->data;
p->next=q->next;
delete q;
return x;
}
}
template<class DataType>
int LinkList<DataType>::Locate(DataType x)
{
Node<DataType>*p=first->next;
int count=1;
while (p=!NULL)
{if(p->data==x)return count;
p=p->next;
count++;
}
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;
}
}
LinkList_main.cpp:
#include<iostream>
using namespace std;
#include"LinkList.cpp"
void main()
{int r[5]={1,2,3,4,5};
LinkList<int>L(r,5);
cout<<"执行插入操作前数据为:"<<endl;
L.PrintList();
try
{L.insert(2,3);
}
catch(char*s)
{cout<<s<<endl;
}
cout<<"执行插入操作后数据为:"<<endl;
L.PrintList();
cout<<"值为5的元素位置为:";
cout<<L.Locate(5)<<endl;
L.PrintList();
try
{
L.Delete(1);
}
catch(char *s)
{cout<<s<<endl;
}
cout<<"执行删除操作后数据为;"<<endl;
L.PrintList();
}
错误提示:
点击后出现新的界面:
不知道是什么原因,求解~~
- 单链表 错误找不出来
- 一个例程,大学人员管理信息系统的设计,不知哪里有错误,一直找不出来
- 成绩表!!错误!运行不出来了!!
- 发现VC有个错误检查不出来
- java中那些经常检查不出来的错误
- CF——289D(推不出来就找规律吧)
- 找错误
- 找错误
- 找错误
- win下cygwin openwrt编译测试 (应该行得通,但是我找不出来working g++ failed原因。)
- 帮我找下错误 我调试了许久就是不知道怎么改正这个错误
- 笑不出来
- 哭不出来
- eclipse 点不出来
- 图片显示不出来
- 状态栏显示不出来
- 惠普打印机打印不出来
- ImageIcon显示不出来.
- jQuery Lazy Load 图片延迟加载
- Volley使用介绍
- 怎么制作你的第一个机器人
- 【连载】软件工程大型项目实训纪实
- GCD学习 Main Dispatch Queue/Global Dispatch Queue
- 单链表 错误找不出来
- svn for mac 无法记住密码
- centos6安装中文输入法
- compare方法
- JSP+JavaBean连接数据库时无法读取表中内容
- windows下搭建node.js及npm的工作环境
- n个元素进栈,出栈顺序问题
- SDUT 2482 二叉排序树
- php升级到5.3 localhost 不能链接mysql