编程论坛
注册
登录
编程论坛
→
C++教室
c 和c++l里的链表 使我很困惑
小小编号
发布于 2009-08-12 22:30, 735 次点击
......各位前辈好
小弟我出来乍到 还要像各位前辈们请教
原来老师讲C链表的时候 翘课了....不是很懂 后来也没仔细看 现在忽然发现 完全不会用...不太懂得它的原理
请前辈们多多讲解下 谢谢 万分感谢
7 回复
#2
小小编号
2009-08-12 22:31
沙发 我先踩下 请前辈们多多指教
#3
mfkblue
2009-08-12 23:06
就是一个牵一个,用链子链在一起的表,
#4
小小编号
2009-08-13 19:04
额 那要怎么牵呢 怎么编写程序??举个例子 吧
谢谢
#5
sydyh43
2009-08-13 19:22
你先把结构体搞懂,这样就好办了。
#6
mfkblue
2009-08-13 23:50
#include <iostream>
using namespace std;
template <class T>
class ChainIterator;
class NoMem
{
public:
NoMem(){}
};
void OutOfBounds()
{
throw NoMem();
}
template <class T>
class Chain;
template <class T>
class ChainNode
{
friend Chain<T>;
friend class ChainIterator<T>;
private:
T data;
ChainNode<T> *link;
};
template <class T>
class Chain
{
public:
Chain(){first=0;}
~Chain();
bool IsEmpty()const {return first==0;}
int Length()const;
bool Find(int k,T& x) const;
int Search(const T& x) const;
Chain<T>& Delete(int k,T& x);
Chain<T>& Insert(int k,const T& x);
void Output(ostream& out) const;
void Erase();
Chain<T>& Append(const T& x);
private:
ChainNode<T> *first;
friend class ChainIterator<T>;
};
template <class T>
Chain<T>::~Chain()
{
ChainNode<T> *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
template <class T>
int Chain<T>::Length()const
{
ChainNode<T> *current=first;
int len=0;
while(current)
{
len++;
current=current->link;
}
return len;
}
template <class T>
bool Chain<T>::Find(int k,T& x)const
{
if(k<1) return false;
ChainNode<T> *current=first;
int index=1;
while(index<k&¤t)
{
current=current->link;
index++;
}
if(current)
{
if(x==current->data)
return true;
}
return false;
}
template <class T>
int Chain<T>::Search(const T& x) const
{
ChainNode<T> *current=first;
int index=1;
while(current&¤t->data!=x)
{
current=current->link;
index++;
}
if(current) return index;
return 0;
}
template <class T>
void Chain<T>::Output(ostream& out) const
{
ChainNode<T> *current;
for(current=first;current;current=current->link)
out<<current->data<<" ";
}
template <class T>
ostream& operator<<(ostream&out,const Chain<T>& x)
{
x.Output(out);
return out;
}
template <class T>
Chain<T>& Chain<T>::Delete(int k,T& x)
{
if(k<1||!first)
throw OutOfBounds();
ChainNode<T> *p=first;
if(k==1)
first=first->link;
else
{
ChainNode<T> *q=first;
for(int index=1;index<k-1&&q;index++)
q=q->link;
if(!q||!q->link)
throw OutOfBounds();
p=q->link;
q->link=p->link;
}
x=p->data;
delete p;
return *this;
}
template <class T>
Chain<T>& Chain<T>::Insert(int k, const T& x)
{
if(k<0) throw OutOfBounds();
ChainNode<T> *p=first;
for(int index=1;index<k&&p;index++)
p=p->link;
if(k>0&&!p) throw OutOfBounds();
ChainNode<T> *y=new ChainNode<T>;
y->data=x;
y->link=0;
if(k)
{
y->link=p->link;
p->link=y;
}
else
{
y->link=first;
first=y;
}
return *this;
}
template <class T>
void Chain<T>::Erase()
{
ChainNode<T> *next;
while(first)
{
next=first->link;
delete first;
first=next;
}
}
template <class T>
Chain<T>& Chain<T>::Append(const T&x)
{
ChainNode<T> *y;
y=new ChainNode<T>;
y->data=x;
y->link=0;
ChainNode<T> *c;
c=first;
while(c->link)
{
c=c->link;
}
c->link=y;
return *this;
}
template <class T>
class ChainIterator
{
public:
T* Initialize(const Chain<T>& c)
{
location=c.first;
if(location) return &location->data;
return 0;
}
T* Next()
{
if(!location) return 0;
location=location->link;
if(location) return &location->data;
return 0;
}
private:
ChainNode<T> *location;
};
int main()
{
Chain <int> a;
a.Insert(0,2).Insert(1,6).Insert(2,3).Insert(3,5).Insert(4,7);
cout<<a<<endl;
int i;
i=a.Length();
cout<<i<<endl;
int *x;
ChainIterator<int> c;
x=c.Initialize(a);
while(x)
{
cout<<*x<<" ";
x=c.Next();
}
cout<<endl;
i=6;
if(a.Find(2,i)) cout<<"find"<<endl;
int j;
j=a.Search(i);
cout<<"search:"<<j<<endl;
a.Delete(2,i);
cout<<a<<endl;
a.Append(i);
cout<<a<<endl;
a.Erase();
cout<<a<<endl;
return 0;
}
#7
y_afu
2009-08-15 10:20
首先链表中的元素肯定是一个复合数据结构,因为它至少要包含两个内容,一个是要存储的数据本身另一个是用来链接用的指针。如:有一个结构struct link{int data;link* next};data是用来存储数据的不然你做这个链表就没意义了,next则是用来指向下一个元素;举例来说:
link A;link B;link C; //声明了三个结构变量
A.data=1;B.data=2;C.data=3;//现在将它们串起来
link* head=&A;A.next=&B;B.next=&C;/*head为链表头这样它们的关系就是head指向A,然后A.next指向B,B.next指向C,从A访问C的数据域时可以这样head->next->next->data=99;*/
[
本帖最后由 y_afu 于 2009-8-15 10:22 编辑
]
#8
shining小南
2010-10-08 22:03
我也在学链表
给你个比较经典的
#include "iostream.h"
struct node//定义结点结构类型
{
char data;//用于存放字符数据
node *next;//用于指向下一个结点(后继结点)
};
node * create();//创建链表的函数,返回表头
void showList(node *head);//遍历链表的函数,参数为表头
node *search(node *pRead,char keyword);
void insert(node * &head,char keyWord,char newdata);
int main()
{
node *head;
head=create();//以head 为表头创建一个链表
showList(head);//遍历以head 为表头的链表
search(head,'u');
insert(head, 'u','l');
showList(head);
return 0;
}
node * create()
{
node *head=NULL;//表头指针,一开始没有任何结点,所以为NULL
node *pEnd=head;//表为指针,一开始没有任何结点,所以指向表头
node *pS;//创建新结点时使用的指针
char temp;//用于存放从键盘输入的字符
cout <<"Please input a string end with '#':" <<endl;
do//循环至少运行一次
{
cin >>temp;
if (temp!='#')//如果输入的字符不是结尾符#,则建立新结点
{
pS=new node;//创建新结点
pS->data=temp;//新结点的数据为temp
pS->next=NULL;//新结点将成为表尾,所以next 为NULL
if (head==NULL)//如果链表还没有任何结点存在
{
head=pS;//则表头指针指向这个新结点
}
else//否则
{
pEnd->next=pS;//把这个新结点连接在表尾
}
pEnd=pS;//这个新结点成为了新的表尾
}
}while (temp!='#');//一旦输入了结尾符,则跳出循环
return head;//返回表头指针
}
void showList(node *head)
{
node *pRead=head;//访问指针一开始指向表头
cout <<"The data of the link list are:" <<endl;
while (pRead!=NULL)//当访问指针存在时(即没有达到表尾之后)
{
cout <<pRead->data;//输出当前访问结点的数据
pRead=pRead->next;//访问指针向后移动
}
cout <<endl;
}
node * search(node *head,char keyword)//返回结点的指针
{
node *pRead=head;
while (pRead!=NULL)//采用与遍历类似的方法,当访问指针没有到达表尾之后
{
if (pRead->data==keyword)//如果当前结点的数据和查找的数据相符
{
cout<<pRead->data<<endl;
return pRead;//则返回当前结点的指针
}
pRead=pRead->next;//数据不匹配,pRead 指针向后移动,准备查找下一个结点
}
return NULL;//所有的结点都不匹配,返回NULL
}
void insert(node * &head,char keyWord,char newdata)//keyWord 是查找关键字符
{
node *newnode=new node;//新建结点
newnode->data=newdata;//newdata 是新结点的数据
node *pGuard=search(head,keyWord);//pGuard 是插入位置前的结点指针
if (head==NULL || pGuard==NULL)//如果链表没有结点或找不到关键字结点
{//则插入表头位置
newnode->next=head;//先连
head=newnode;//后断
}
else//否则
{//插入在pGuard 之后
newnode->next=pGuard->next;//先连
pGuard->next=newnode;//后断
}
}
1