创建主函数以测试输出效果
#include
#include
#include
using namespace std;
struct node{
int data;
node* next;
};
//遍历链表
void travelsal(node *head){
node *p=head;
while(p->next){
cout<<p->next->data<<' ';
p=p->next;
}
}
int main(){
int a[]={
1,3,5,7,9,8,6,4,2};
node *L=HeadCreate(a,9);
travelsal(L);
return 0;
}
//头插法创建链表,带头结点head,head不含数据域
node *HeadCreate(int a[],int len){
node *head,*p; //p始终指向第一个带数据域的结点
head=new node;
head->next=NULL;
for(int i=0;i<len;i++){
p=new node;
p->data=a[i];
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
//尾插法创建单链表
node *TailCreate(int a[],int len){
node *pre,*p,*head; //pre始终指向最后一个结点
head=new node;
head->next=NULL;
pre=head;
for(int i=0;i<len;i++){
node *p=new node;
p->data=a[i];
p->next=NULL;
pre->next=p;
pre=p; //pre始终指向尾结点
}
return head;
}
//计算链表的长度
int calculateLen(node *head){
int count=0;
node *p=head->next;
while(p){
p=p->next;
count++;
}
return count;
}
4. 统计链表中待查找元素的个数
//统计待查找元素出现的次数
int search(node *head,int x){
int count=0;
node *p=head->next;
while(p){
if(x==p->data){
count++;
}
p=p->next;
}
return count;
}
5. 链表的元素插入
//链表插入元素,插入元素到pos处
void insert(node *head,int x,int pos){
node *p=head;
for(int i=0;i<pos-1;i++){
p=p->next;
}
node *t=new node;
t->data=x;
t->next=p->next;
p->next=t;
}
6. 删除表中所有元素等于x的值
//删除链表中所有元素等于x的值
void delete(node *head,int x){
node *pre,*p;
pre=head;
p=head->next;
while(p){
if(p->data=x){
pre->next=p->next; //删除p所指向的结点
delete p;
p=pre->next; //重新给p赋值
}
else{
pre=p; //pre永远指向待删结点的前一个
p=p->next;
}
}
}
完整代码
#include
#include
#include
using namespace std;
struct node{
int data;
node* next;
};
//头插法创建单链表 带头结点head,head不含数据域
node *HeadCreate(int a[],int len){
node *head,*p; //p始终指向第一个带数据域的结点
head=new node;
head->next=NULL;
for(int i=0;i<len;i++){
p=new node;
p->data=a[i];
p->next=NULL;
p->next=head->next;
head->next=p;
}
return head;
}
//尾插法创建单链表
node *TailCreate(int a[],int len){
node *pre,*p,*head; //pre始终指向最后一个结点
head=new node;
head->next=NULL;
pre=head;
for(int i=0;i<len;i++){
node *p=new node;
p->data=a[i];
p->next=NULL;
pre->next=p;
pre=p; //pre始终指向尾结点
}
return head;
}
//计算链表的长度
int calculateLen(node *head){
int count=0;
node *p=head->next;
while(p){
p=p->next;
count++;
}
return count;
}
//统计待查找元素出现的次数
int search(node *head,int x){
int count=0;
node *p=head->next;
while(p){
if(x==p->data){
count++;
}
p=p->next;
}
return count;
}
//链表插入元素,插入元素到pos处
void insert(node *head,int x,int pos){
node *p=head;
for(int i=0;i<pos-1;i++){
p=p->next;
}
node *t=new node;
t->data=x;
t->next=p->next;
p->next=t;
}
//删除链表中所有元素等于x的值
void deleteLen(node *head,int x){
node *pre,*p;
pre=head;
p=head->next;
while(p){
if(p->data==x){
pre->next=p->next; //删除p所指向的结点
delete p;
p=pre->next; //重新给p赋值
}
else{
pre=p; //pre永远指向待删结点的前一个
p=p->next;
}
}
}
//遍历链表
void travelsal(node *head){
node *p=head;
while(p->next){
cout<<p->next->data<<' ';
p=p->next;
}
}
int main(){
int a[]={
1,3,5,7,9,8,8,4,2};
node *L=TailCreate(a,9);
node *L2=HeadCreate(a,9);
cout<<"尾插法创建链表"<<" ";
travelsal(L);
cout<<endl;
cout<<"头插法创建链表"<<" ";
travelsal(L2);
cout<<endl;
cout<<"链表的长度是"<<" "<<calculateLen(L)<<endl;
cout<<"存在8的个数为"<<" "<<search(L,8)<<endl;
insert(L,45,2);
cout<<"尾插法插入元素后的链表"<<" ";
travelsal(L);
cout<<endl;
deleteLen(L,8);
cout<<"删除链表中所有等于8的元素"<<" ";
travelsal(L);
return 0;
}