链表在C++语言中是比较常要的技术,比如在做游戏时可以记录人物行走路线,一些用数组难以实现的功能用链表可以轻松实现。那么在C++语言中是链表是怎么实现的呢。下面主要讲解一下。。
链表实际上就像是自行车里的链条,一环接着一环。在链表里常用指向自身类得NEXT指针来和下一个链表节点做联系。而双向链表主要就是添加一个向上排序的节点。。我们只要理解单向链表,那么双向链表也就明白是怎么回事呢,请看简单的链表代码。。
#include "stdafx.h"
#include <iostream>
using namespace std;
//C++简单链表演示
struct Book
{
int num;
int price;
Book *next;
};
//显示链表
void Show(Book *head)
{
while (head)
{
cout<<"编号为:"<<head->num<<",价格为:"<<head->price<<endl;
head=head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Book *head=new Book;//头结点
Book *p1=new Book;
Book *p2=new Book;
Book *p3=new Book;
head->num=0;
head->price=0;
head->next=p1;
p1->num=1;
p1->price=10;
p1->next=p2;
p2->num=2;
p2->price=20;
p2->next=p3;
p3->num=3;
p3->price=30;
p3->next=NULL;
Show(head);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//C++简单链表演示
struct Book
{
int num;
int price;
Book *next;
};
//显示链表
void Show(Book *head)
{
while (head)
{
cout<<"编号为:"<<head->num<<",价格为:"<<head->price<<endl;
head=head->next;
}
}
int _tmain(int argc, _TCHAR* argv[])
{
Book *head=new Book;//头结点
Book *p1=new Book;
Book *p2=new Book;
Book *p3=new Book;
head->num=0;
head->price=0;
head->next=p1;
p1->num=1;
p1->price=10;
p1->next=p2;
p2->num=2;
p2->price=20;
p2->next=p3;
p3->num=3;
p3->price=30;
p3->next=NULL;
Show(head);
system("pause");
return 0;
}
以上代码只是一个简单的静态链表,目的是让大家能明白链表是个什么意思。真正在程序中我们一般是动态绑定节点的。下面用一个程序简单的演示动态创建链表。因为结构在C++中默认成员是public级别的。所以我们并不需要手动添加public:块。下面是用类
Book* CreateHead()
{
Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
p1=new Book;
head=p1;
p2=p1;//将三个结点一起在椎中创建
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
else
{
delete p1;p2=NULL;return head;
}
while(p1->num!=0)
{
p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
p1=new Book;
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
{
Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
p1=new Book;
head=p1;
p2=p1;//将三个结点一起在椎中创建
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
else
{
delete p1;p2=NULL;return head;
}
while(p1->num!=0)
{
p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
p1=new Book;
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
原理很简单.每次执行编号和价格的时候用本次指针的next成员指向新创建的成员,那么这两个节点就关联起来呢,请大家自行在编辑器里运行并试着自己写代码
链表的删除
删除原理如下:
当一个链表中有1,2,3,4,5这5个节点,如果我们删除第2个,那么链表将会用1->next指针指向3就可以呢。代码如下
/删除链表某个结点
void Delete(Book *p,int num)
{
Book *temp;
if (p->num==num)
{
temp=p;
p=p->next;
::p=p;
delete temp;
return;
}
while(p)
{
if(p->next==NULL)
{
cout<<"null"<<endl;
return;
}
if(p->next->num==num)
{
temp=p->next;
p->next=temp->next;
delete temp;
cout<<"ok"<<endl;
return;
}
p=p->next;
}
cout<<"null"<<endl;
}
void Delete(Book *p,int num)
{
Book *temp;
if (p->num==num)
{
temp=p;
p=p->next;
::p=p;
delete temp;
return;
}
while(p)
{
if(p->next==NULL)
{
cout<<"null"<<endl;
return;
}
if(p->next->num==num)
{
temp=p->next;
p->next=temp->next;
delete temp;
cout<<"ok"<<endl;
return;
}
p=p->next;
}
cout<<"null"<<endl;
}
链表的插入
这里假设插入到链表的尾段(因为这样更容易让人理解链表插入的原理),代码如下
完整可运行的程序如下
#include "stdafx.h"
#include <iostream>
using namespace std;
//C++动态链表的建立
class Book
{
public:
int num;
int price;
Book *next;
};
Book *p;
//创建头结点返回头结点地址给程序调用
Book* CreateHead()
{
Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
p1=new Book;
head=p1;
p2=p1;//将三个结点一起在椎中创建
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
else
{
delete p1;p2=NULL;return head;
}
while(p1->num!=0)
{
p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
p1=new Book;
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
void Show(Book *head)
{
while(head!=NULL)
{
cout<<"图书编号为:"<<head->num<<",价格为:"<<head->price<<endl;
head=head->next;
}
}
//删除链表某个结点
void Delete(Book *p,int num)
{
Book *temp;
if (p->num==num)
{
temp=p;
p=p->next;
::p=p;
delete temp;
return;
}
while(p)
{
if(p->next==NULL)
{
cout<<"null"<<endl;
return;
}
if(p->next->num==num)
{
temp=p->next;
p->next=temp->next;
delete temp;
cout<<"ok"<<endl;
return;
}
p=p->next;
}
cout<<"null"<<endl;
}
//链表的添加(这里假设直接添加到最尾段)
void Insert(Book *p,int num,int price)
{
Book *temp=new Book;//这个结点用于添加编号和价格
Book *tem=new Book;//这个结点用于存放尾结点地址
while(p)
{
tem=p;
p=p->next;
}
temp->num=num;
temp->price=price;
tem->next=temp;
temp->next=NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
p=CreateHead();
Show(p);
cout<<endl;
cout<<"请输入要删除的编号"<<endl;
int num;
cin>>num;
Delete(p,num);
Show(p);
cout<<"请输入编号"<<endl;
cin>>num;
int price;
cout<<"请输入价格"<<endl;
cin>>price;
Insert(p,num,price);
Show(p);
system("pause");
return 0;
}
#include <iostream>
using namespace std;
//C++动态链表的建立
class Book
{
public:
int num;
int price;
Book *next;
};
Book *p;
//创建头结点返回头结点地址给程序调用
Book* CreateHead()
{
Book *head,*p1,*p2;//定义三个指向Book的类,返回值给head
p1=new Book;
head=p1;
p2=p1;//将三个结点一起在椎中创建
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
else
{
delete p1;p2=NULL;return head;
}
while(p1->num!=0)
{
p2=p1;//这步很重要,意思为将P2设为当前结点,然后用p1继续创建,用培p2->next指向p1
p1=new Book;
cout<<"请输入图书编号,以0结束"<<endl;
cin>>p1->num;
if (p1->num!=0)
{
cout<<"请输入图书价格"<<endl;
cin>>p1->price;
}
p2->next=p1;
}
delete p1;
p2->next=NULL;
return head;
}
void Show(Book *head)
{
while(head!=NULL)
{
cout<<"图书编号为:"<<head->num<<",价格为:"<<head->price<<endl;
head=head->next;
}
}
//删除链表某个结点
void Delete(Book *p,int num)
{
Book *temp;
if (p->num==num)
{
temp=p;
p=p->next;
::p=p;
delete temp;
return;
}
while(p)
{
if(p->next==NULL)
{
cout<<"null"<<endl;
return;
}
if(p->next->num==num)
{
temp=p->next;
p->next=temp->next;
delete temp;
cout<<"ok"<<endl;
return;
}
p=p->next;
}
cout<<"null"<<endl;
}
//链表的添加(这里假设直接添加到最尾段)
void Insert(Book *p,int num,int price)
{
Book *temp=new Book;//这个结点用于添加编号和价格
Book *tem=new Book;//这个结点用于存放尾结点地址
while(p)
{
tem=p;
p=p->next;
}
temp->num=num;
temp->price=price;
tem->next=temp;
temp->next=NULL;
}
int _tmain(int argc, _TCHAR* argv[])
{
p=CreateHead();
Show(p);
cout<<endl;
cout<<"请输入要删除的编号"<<endl;
int num;
cin>>num;
Delete(p,num);
Show(p);
cout<<"请输入编号"<<endl;
cin>>num;
int price;
cout<<"请输入价格"<<endl;
cin>>price;
Insert(p,num,price);
Show(p);
system("pause");
return 0;
}
//链表的添加(这里假设直接添加到最尾段)
void Insert(Book *p,int num,int price)
{
Book *temp=new Book;//这个结点用于添加编号和价格
Book *tem=new Book;//这个结点用于存放尾结点地址
while(p)
{
tem=p;
p=p->next;
}
temp->num=num;
temp->price=price;
tem->next=temp;
temp->next=NULL;
}
void Insert(Book *p,int num,int price)
{
Book *temp=new Book;//这个结点用于添加编号和价格
Book *tem=new Book;//这个结点用于存放尾结点地址
while(p)
{
tem=p;
p=p->next;
}
temp->num=num;
temp->price=price;
tem->next=temp;
temp->next=NULL;
}