C++实现链表函数
2014-09-15 10:24
176 查看
//链表,头结点中要放置元素
#include<iostream>
using namespace std;
struct listnode
{
int data;
listnode* next;
};
listnode* createlistnode(int value)
{
listnode* pnode=new listnode;
pnode->data=value;
pnode->next=NULL;
return pnode;
}
void connectlistnode(listnode* pcurrent,listnode* pnext)
{
if(pcurrent==NULL)
{
cout<<"error"<<endl;
return;
}
pcurrent->next=pnext;
}
void printlistnode(listnode* pnode)
{
if(pnode==NULL)
cout<<"the listnode is null"<<endl;
else
cout<<pnode->data<<" ";
}
void printlist(listnode* phead)
{
listnode* pnode=phead;
if(phead==NULL)
cout<<"the list is null"<<endl;
else
{
while(pnode!=NULL)
{
cout<<pnode->data<<" ";
pnode=pnode->next;
}
cout<<endl;
}
}
void destroylist(listnode* phead)
{
listnode* pnode=phead;
while(phead!=NULL)
{
phead=phead->next;
delete pnode;
pnode=phead;
}
pnode=NULL;
phead=NULL;
}
void addtotail(listnode** phead,int value)//注意这里必须把phead设置为指向指针的指针,因为当链表为空时,插入一个节点,此时肯定会改变头指针
{
listnode* pnodenew=new listnode;
pnodenew->data=value;
pnodenew->next=NULL;
if(*phead==NULL)
*phead=pnodenew;
else
{
listnode* pnode=*phead;
while(pnode->next!=NULL)
pnode=pnode->next;
pnode->next=pnodenew;
}
}
void removenode(listnode** phead,int value)
{
if(phead==NULL||*phead==NULL)
return ;
listnode* ptobedeleted=NULL;
if((*phead)->data==value)
{
ptobedeleted=*phead;
*phead=(*phead)->next;
}
else
{
listnode* pnode=*phead;
while(pnode->next!=NULL&&pnode->next->data!=value)
pnode=pnode->next;
if(pnode->next!=NULL&&pnode->next->data==value)
{
ptobedeleted=pnode->next;
pnode->next=pnode->next->next;
}
}
if(ptobedeleted!=NULL)
{
delete ptobedeleted;
ptobedeleted=NULL;
}
}
int main()
{
//链表的第一种构造方式
listnode* pnode1 = createlistnode(1);
listnode** head=&pnode1;
addtotail(head,2);
addtotail(head,3);
addtotail(head,4);
addtotail(head,5);
addtotail(head,6);
addtotail(head,7);
addtotail(head,8);
addtotail(head,9);
printlist(pnode1);
//链表的第二种构造方式
listnode* pnode11 = createlistnode(1);
listnode* pnode2 = createlistnode(2);
listnode* pnode3 = createlistnode(3);
listnode* pnode4 = createlistnode(4);
listnode* pnode5 = createlistnode(5);
listnode* pnode6 = createlistnode(6);
connectlistnode(pnode11, pnode2);
connectlistnode(pnode2, pnode3);
connectlistnode(pnode3, pnode4);
connectlistnode(pnode4, pnode5);
connectlistnode(pnode5, pnode6);
printlist(pnode11);
//链表的第三种构造方式
listnode* pnode13=NULL;
listnode** head3=&pnode13;
addtotail(head3,2);
addtotail(head3,3);
addtotail(head3,4);
addtotail(head3,5);
addtotail(head3,6);
addtotail(head3,7);
addtotail(head3,8);
addtotail(head3,9);
printlist(*head3);
return 0;
}
#include<iostream>
using namespace std;
struct listnode
{
int data;
listnode* next;
};
listnode* createlistnode(int value)
{
listnode* pnode=new listnode;
pnode->data=value;
pnode->next=NULL;
return pnode;
}
void connectlistnode(listnode* pcurrent,listnode* pnext)
{
if(pcurrent==NULL)
{
cout<<"error"<<endl;
return;
}
pcurrent->next=pnext;
}
void printlistnode(listnode* pnode)
{
if(pnode==NULL)
cout<<"the listnode is null"<<endl;
else
cout<<pnode->data<<" ";
}
void printlist(listnode* phead)
{
listnode* pnode=phead;
if(phead==NULL)
cout<<"the list is null"<<endl;
else
{
while(pnode!=NULL)
{
cout<<pnode->data<<" ";
pnode=pnode->next;
}
cout<<endl;
}
}
void destroylist(listnode* phead)
{
listnode* pnode=phead;
while(phead!=NULL)
{
phead=phead->next;
delete pnode;
pnode=phead;
}
pnode=NULL;
phead=NULL;
}
void addtotail(listnode** phead,int value)//注意这里必须把phead设置为指向指针的指针,因为当链表为空时,插入一个节点,此时肯定会改变头指针
{
listnode* pnodenew=new listnode;
pnodenew->data=value;
pnodenew->next=NULL;
if(*phead==NULL)
*phead=pnodenew;
else
{
listnode* pnode=*phead;
while(pnode->next!=NULL)
pnode=pnode->next;
pnode->next=pnodenew;
}
}
void removenode(listnode** phead,int value)
{
if(phead==NULL||*phead==NULL)
return ;
listnode* ptobedeleted=NULL;
if((*phead)->data==value)
{
ptobedeleted=*phead;
*phead=(*phead)->next;
}
else
{
listnode* pnode=*phead;
while(pnode->next!=NULL&&pnode->next->data!=value)
pnode=pnode->next;
if(pnode->next!=NULL&&pnode->next->data==value)
{
ptobedeleted=pnode->next;
pnode->next=pnode->next->next;
}
}
if(ptobedeleted!=NULL)
{
delete ptobedeleted;
ptobedeleted=NULL;
}
}
int main()
{
//链表的第一种构造方式
listnode* pnode1 = createlistnode(1);
listnode** head=&pnode1;
addtotail(head,2);
addtotail(head,3);
addtotail(head,4);
addtotail(head,5);
addtotail(head,6);
addtotail(head,7);
addtotail(head,8);
addtotail(head,9);
printlist(pnode1);
//链表的第二种构造方式
listnode* pnode11 = createlistnode(1);
listnode* pnode2 = createlistnode(2);
listnode* pnode3 = createlistnode(3);
listnode* pnode4 = createlistnode(4);
listnode* pnode5 = createlistnode(5);
listnode* pnode6 = createlistnode(6);
connectlistnode(pnode11, pnode2);
connectlistnode(pnode2, pnode3);
connectlistnode(pnode3, pnode4);
connectlistnode(pnode4, pnode5);
connectlistnode(pnode5, pnode6);
printlist(pnode11);
//链表的第三种构造方式
listnode* pnode13=NULL;
listnode** head3=&pnode13;
addtotail(head3,2);
addtotail(head3,3);
addtotail(head3,4);
addtotail(head3,5);
addtotail(head3,6);
addtotail(head3,7);
addtotail(head3,8);
addtotail(head3,9);
printlist(*head3);
return 0;
}
相关文章推荐
- 链表的基本操作函数算法(C/C++实现)
- 数据结构之C++实现无头节点链表(List)(无主函数)
- C++异常机制的实现方式和开销分析 (大图,编译器会为每个函数增加EHDL结构,组成一个单向链表,非常著名的“内存访问违例”出错对话框就是该机制的一种体现)
- 单链表结构的实现(C++版, Link.h)
- 在Delphi与C++之间实现函数与对象共享
- 利用C++模板,代替虚函数,实现类的静态多态性(加入性能测试部分)
- Python中数组 链表 元组 字典实现类Sql多字段排序和动态函数实现
- 在Delphi与C++之间实现函数与对象共享
- 【头文件】c++实现链表
- 如何在C++中实现类似Scanf的变参个数函数
- C++实现线形链表
- c++实现单向链表反转的学习总结
- 探索c++的函数pow()的实现方法·数学与程序设计的结合(zz)
- C++中的property库的设计与实现过程(二)——如何为属性指定get_和set_函数?
- 用无序链表实现字典ADT(C++描述)
- C++版-----链表实现
- 在C++中用虚函数及模版实现委托
- 在Delphi与C++之间实现函数与对象共享
- 在c++中实现realloc()函数
- C++ 学习练手 - 双向链表的模板实现