C++实现链表函数
来源:互联网 发布:程序员上班闲 编辑:程序博客网 时间:2024/05/16 10:03
//链表,头结点中要放置元素
#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;
}
0 0
- 链表的基本操作函数算法(C/C++实现)
- my_strchr 函数实现 C函数
- C/C++--strcpy函数实现
- C/C++--memcpy函数实现
- C/C++--memmove函数实现
- C/C++--strcpy函数实现
- C/C++--strlen函数实现
- C/C++--strcmp函数实现
- C字符串函数实现
- c常用函数实现
- strcat函数C实现
- c常用函数实现
- c实现strcpy函数
- C++ atoi 函数实现
- c常用函数实现
- trim 函数c实现
- C字符串函数实现
- C实现memset函数
- 将一个进程转变为守护进程所需要的步骤
- 啊手机卡打开拉萨决定了卡结算来得及啊劳动力
- Android通过String.format格式化(动态改变)字符串资源的显示内容
- 修改linux下命令提示行路径长度
- 轮廓的查找、表达、绘制、特性及匹配
- C++实现链表函数
- 如何阅读源代码
- Chapter 12 Concurrent Programming [《CSAPP》 笔记]
- UVa 10003 - Cutting Sticks
- Single Number问题
- RabbitMQ RPC
- Date日期处理
- Storm实战之Trident简介
- Maximum Subarray