首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

求证:这个程序是实现了一个链表的大体功能吗

2013-11-30 
求证:这个程序是实现了一个链表的大致功能吗?/*main.c*/#includeiostream//#include node.h#include

求证:这个程序是实现了一个链表的大致功能吗?
/*main.c*/
#include<iostream>
//#include "node.h"
#include "linklist.h"
using namespace std;
int main ()
{
//Node a;
//using namespace std;
int tempi;
char tempc;
cout << "Please enter a number and a charactor:" << endl;
cin >> tempi >> tempc;
Linklist a(tempi,tempc);
a.Locate(tempi);
a.Insert(1,'C');
a.Insert(2,'B');
a.Insert(2,'F');
cout << "After Insert!!" << endl;
a.Show();
a.Locate('B');
a.Delete();
cout << "After Delete!!" << endl;
a.Show();
a.Destroy();
cout << "After Destroy!!" << endl;
a.Show();
return 0;
}
/*linklist.h*/
#include<iostream>
#include "node.h"
using namespace std;
class Linklist
{
public:
Linklist(int i,char c);
bool Locate(int i);
bool Locate(char c);
bool Insert(int i=0,char c='0');
bool Delete();
void Show();
void Destroy();
private:
Node head;
Node * pcurrent;
};
Linklist::Linklist(int i,char c):head(i,c)
{
cout << "Linklist constructor is running..." << endl;
pcurrent=&head;
}
bool Linklist::Locate(int i)//这个函数是实现一个定位的功能吗??
{
Node * ptemp=&head;
while(ptemp!=NULL)//为什么是不等于NULL??
{
if (ptemp->readi()==i)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Locate(char c)//
{
Node * ptemp=&head;
while(ptemp!=NULL)
{
if (ptemp->readc()==c)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
bool Linklist::Insert(int i,char c)
{
if (pcurrent!=NULL)
{
Node * temp=new Node(i,c,pcurrent,pcurrent->readn());
if (pcurrent->readn()!=NULL)
{
pcurrent->readn()->setp(temp);
}
pcurrent->setn(temp);
return true;
}
else
{
return false;
}
}
bool Linklist::Delete()
{
if (pcurrent!=NULL && pcurrent!=&head)
{
Node * temp=pcurrent;
if (temp->readn()!=NULL)
{
temp->readn()->setp(pcurrent->readp());
}
temp->readp()->setn(pcurrent->readn());
pcurrent=temp->readp();
delete temp;
return true;
}
else 
{
return false;
}
}
void Linklist::Show()
{
Node * ptemp=&head;
while (ptemp!=NULL)
{
cout << ptemp->readi()<<'\t' << ptemp->readc() << endl;
ptemp=ptemp->readn();
}
}
void Linklist::Destroy()
{
Node * ptemp1=head.readn();
while (ptemp1!=NULL)
{
Node * ptemp2=ptemp1->readn();
delete ptemp1;
ptemp1=ptemp2;
}
head.setn(NULL);
}
/*node.h*/
#include<iostream>
using namespace std;
class Node
{
public:
Node();
Node(int i,char c='0');
Node(int i,char c,Node *p,Node *n);
int readi() const;
char readc() const;
Node * readp() const;
Node * readn() const;
bool set(int i);
bool set(char c);
bool setp(Node *p);
bool setn(Node *n);
private:
int idata;
char cdata;
Node *prior;
Node *next;
};
Node * Node::readp() const
{
return prior;
}
Node * Node::readn() const
{
return next;
}
int Node::readi() const
{
return idata;
}
char Node::readc() const
{
return cdata;
}
bool Node::set(int i)
{
idata=i;
return true;
}
bool Node::set(char c)
{
cdata=c;
return true;
}
bool Node::setp(Node *p)
{
prior=p;
return true;
}
bool Node::setn(Node *n)
{
next=n;
return true;
}
Node::Node()
{
cout<<"Node constructor is running..."<< endl;
idata=0;
cdata='0';
prior=NULL;
next=NULL;
}
Node::Node(int i,char c)
{
cout << "Node constructor is running..." << endl;
idata=i;
cdata=c;
prior=NULL;
next=NULL;
}
Node::Node(int i,char c,Node* p,Node* n)
{
cout << "Node constructor is running..."<< endl;
idata=i;
cdata=c;
prior=p;
next=n;
}
已经学完C语言了,对自己要求可能较高,所以没去学Java C#之类的语言。当然这并不代表我认为Java是不好的语言!!学到面向对象的时候还可以看懂些东西,但不知道指针指向对象是个什么样的情况!!说实话,我挺喜欢指针这东西的,觉得他很强大,但好像不小心又会使程序出现崩溃的现象!!希望有人推荐本具体讲指针和对象,类之间的关系的的书籍!!


[解决办法]

bool Linklist::Locate(int i)//这个函数是实现一个定位的功能吗??
{
Node * ptemp=&head;
while(ptemp!=NULL)//为什么是不等于NULL??
{
if (ptemp->readi()==i)
{
pcurrent=ptemp;
return true;
}
ptemp=ptemp->readn();
}
return false;
}
1、这个函数准确说应该是查找是否存在,而不是定位
2、这里为什么是NULL,因为C数据结构中,无明确指向的指针一般都是指向NULL(这是一个好习惯),所以只要不指向NULL也就是说这不是最后一项指针
————————
其他方面:
1、头文件中不要用using指令,比如using std::vector,using namespace等等,这等于没有给对方选择权。namespace本身就是为了解决命名问题而引入的,但你还用using语句去解除。解除也就算了,放在头文件中别人根本不会留意到,引入了这个头文件就等于解除了namespace,对于大工程来说这是灾难。头文件里本身就是声明,所以namespace出现的不是很多,建议直接用std::vector。
2、对于数据结构来说,没必要隐藏数据,这样做只会变麻烦。直接用struct就OK了。
3、这个是数据结构的知识,楼主学过数据结构吗?建议看看严蔚敏的《数据结构(C语言版)》

不知道楼主想问点啥,这程序有什么好讲的?

热点排行