第四周-项目二 建立单链表算法库

本文详细介绍了一个使用C++实现的链表的各种操作,包括初始化、输出、销毁、判断空链表、获取链表长度、顺序查找指定位置元素、查找特定元素、在指定位置插入元素等。通过具体的代码实例展示了每种操作的实现过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

#include <iostream>  
#include "kudetouwenjian.h"  
using namespace std;  
int main()  
{  
     linknode *L;  
     A a[5]={1,2,3,4,5};  
     int n=5,b=4,c=3;  
     A e;  
     toucha(L,a,n);  
     shuchu(L);  
     cout<<endl;  
     cuihui(L);  
     shuchu(L);  
     cout<<"(被摧毁的链表输出的空白。)"<<endl;  
     cout<<endl;  
     cout<<panduanshifouweikong(L)<<endl;;  
     cout<<"(如果为空输出1,不为空输出0.)"<<endl;  
     cout<<endl;  
     weicha(L,a,n);  
     shuchu(L);  
     cout<<endl;  
     cout<<"该链表的长度为:"<<changdu(L)<<endl;  
     cout<<endl;  
     shunxuchazhao(L,b,e);  
     cout<<"顺序查找的第四个为:"<<e<<endl;  
     cout<<endl;  
     e=3;  
     cout<<"查找3,如果在链表中找到3,输出这1,如果没有,输出0"<<endl;  
     cout<<chazhaodange(L,e)<<endl;  
     cout<<endl;  
     e=0;  
     cout<<"在第三个位置差入一个0"<<endl;  
     charudange(L,c,e);  
     shuchu(L);  
     return 0;  
}  
#include <iostream>  
#include <malloc.h>  
#include "kudetouwenjian.h"  
using namespace std;  
void toucha(linknode *&L,A a[],int n)  
{  
     linknode *s;  
     L=(linknode *)malloc(sizeof(linknode));  
     L->next=NULL;  
     for(int i=0;i<n;i++)  
     {  
          s=(linknode *)malloc(sizeof(linknode));  
          s->shuju=a[i];  
          s->next=L->next;  
          L->next=s;  
     }  
}  
void shuchu(linknode *L)  
{  
     linknode *p=L->next;  
     while(p!=NULL)  
     {  
          cout<<p->shuju<<" ";  
          p=p->next;  
     }  
     cout<<endl;  
}  
void weicha(linknode *&L,A a[],int n)  
{  
     linknode *s,*r;  
     L=(linknode *)malloc(sizeof(linknode));  
     r=L;  
     for(int i=0;i<n;i++)  
     {  
          s=(linknode *)malloc(sizeof(linknode));  
          s->shuju=a[i];  
          r->next=s;  
          r=s;  
     }  
     r->next=NULL;  
}  
void chushihua(linknode *&L)  
{  
     L=(linknode *)malloc(sizeof(linknode));  
     L->next=NULL;  
}  
void cuihui(linknode *&L)  
{  
     linknode * pre=L,* p=L->next;  
     while(p!=NULL)  
     {  
          free(pre);  
          pre=p;  
          p=pre->next;  
     }  
     free(pre);  
     L->next=NULL;  
}  
int panduanshifouweikong(linknode *L)  
{  
     return(L->next==NULL);  
}  
int changdu(linknode *L)  
{  
     int e=0;  
     linknode *p=L->next;  
     while(p!=NULL)  
     {  
          e++;  
          p=p->next;  
     }  
     return(e);  
}  
int shunxuchazhao(linknode * L,int i,A &e)  
{  
     int j=1;  
     linknode *p=L->next;  
     if(i<=0)j=j;;  
     while(j<i)  
     {  
          j++;  
          p=p->next;  
     }  
     if(p==NULL)  
          j=j;  
     else  
     {  
          e=p->shuju;  
          return(e);  
     }  
}  
int chazhaodange(linknode *L,A e)  
{  
     int j=1;  
     linknode *p=L->next;  
     if(p==NULL)  
          return 0;  
     while(p!=NULL&&e!=p->shuju)  
     {  
          p=p->next;  
          j++;  
     }  
     if(p==NULL)  
          return 0;  
     else  
     return(1);  
}  
bool charudange(linknode *&L,int i,A e)  
{  
     int j=0;  
     linknode * p=L,* s;  
     if(i<=0)return false;  
     while(j<i-1&&p!=NULL)  
     {  
          j++;  
          p=p->next;  
     }  
     if(p==NULL)  
          return false;  
     else  
     {  
          s=(linknode *)malloc(sizeof(linknode));  
          s->shuju=e;  
          s->next=p->next;  
          p->next=s;  
     }  
}  
#ifndef KUDETOUWENJIAN_H_INCLUDED  
#define KUDETOUWENJIAN_H_INCLUDED  
#include <iostream>  
typedef int A;  
typedef struct lianbiao  
{  
     A shuju;  
     struct lianbiao *next;  
}linknode;  
void toucha(linknode *&L,A a[],int n);  
void shuchu(linknode *L);  
void weicha(linknode *&L,A a[],int n);  
void chushihua(linknode *&L);  
void cuihui(linknode *&L);  
int panduanshifouweikong(linknode *L);  
int changdu(linknode *L);  
int shunxuchazhao(linknode *L,int i,A &e);  
int chazhaodange(linknode *L,A e);  
bool charudange(linknode *&L,int i,A e);  
#endif // KUDETOUWENJIAN_H_INCLUDED  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值