下面包括
1、链表的建立(顺序+逆序)
2、删除结点(头、中、尾
3、增加结点(头、中、尾
(这里没有考虑到空链表的情况,并且初链表只有5个结点,之后运用的时候要修改控制输入的语句)
删除结点
void delete_(struct p*head) //删除结点
{
struct p *a,*b;
int num,c=0;
cin>>num;
if(num==0)//删除头部
head=head->next ;
else
{
for(a=head;a!=NULL;a=a->next )
{//删除在中部或尾部的
if(c==num)
b->next =a->next ;
else
b=a;
c++;
}
}
增加结点
void add(struct p*head)//增加结点
{
struct p *a,*b,*cha;
cha=(struct p *)malloc(sizeof(struct p));
int num,temp;//num是插入的位置(下标从0开始),temp是要插入的值
cin>>num>>temp;
cha->n=temp;
cha->next =NULL;
if(num==0)//增加在头部前
{
cha->next =head;
head=cha;
}
else
{
for(a=head;a!=NULL;a=a->next,c++)
{
if(a->next ==NULL)//在尾部插入(插在后面
{
a->next =cha;
cha->next =NULL;
break;
}
if(c==num)//中间插入 (写的时候卡住了)
{
b=(struct p *)malloc(sizeof(struct p));
b=a->next ;
a->next=cha;
cha->next=b ;
break;
}
}
头插(逆序)
for(int i=0;i<5;i++)//逆序建立 (头插)
{
a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间
cin>>a->n;//对 a 开辟出的结构体进行输入
a->next =head;
head=a;
}
尾插(顺序)
for(int i=0;i<5;i++)//顺序建立 (尾插)
{
a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间
cin>>a->n;//对 a 开辟出的结构体进行输入
a->next=NULL;//a 的结点指向空指针
if(head==NULL)//head只起记录链表头作用,之后不再动 head
head=a;//将第一个结点地址给 head
else
ptr->next=a;//之后 就是 ptr 与 a 之间轮换
ptr=a;
}
总代码:
#include<iostream>
#include<stdlib.h>
#include<cstdio>
using namespace std;
struct p{
int n;
struct p *next;
} *head=NULL,*ptr,*a;
void delete_(struct p*head) //删除结点
{
struct p *a,*b;
cout<<"(删除下标从0开始)"<<endl<<"你要删除的结点位置:";
int num,cout=0;
cin>>num;
if(num==0)//删除头部
head=head->next ;
else
{
for(a=head;a!=NULL;a=a->next )
{
if(cout==num)
b->next =a->next ;
else
b=a;
cout++;
}
}
for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表
printf("%d ",ptr->n );;
}
void add(struct p*head)//增加结点
{
struct p *a,*b,*cha;
cha=(struct p *)malloc(sizeof(struct p));
cout<<"下标从0开始;"<<endl;
cout<<"若输入0 增加在头部之前"<<endl<<"若输入4 增加在尾部之后"<<endl<<"若输入1-3 "<<"增加在该结点之前"<<endl<<endl;
cout<<"输入你的选择:";
int num,c=1;
cin>>num;
printf("\n输入要插入的数据 ");
int temp;
cin>>temp;
cha->n=temp;
cha->next =NULL;
printf("\n");
if(num==0)//增加在头部前
{
cha->next =head;
head=cha;
}
else
{
for(a=head;a!=NULL;a=a->next,c++)
{
if(a->next ==NULL)//在尾部插入(插在后面
{
a->next =cha;
cha->next =NULL;
break;
}
if(c==num)//中间插入 (写的时候卡住了)
{
b=(struct p *)malloc(sizeof(struct p));
b=a->next ;
a->next=cha;
cha->next=b ;
break;
}
// cout<<"*"<<c<<endl;
}
}
for(ptr=head;ptr!=NULL;ptr=ptr->next ) //输出链表
printf("%d ",ptr->n);
}
int main()
{
int option;
cout<<"本链表仅建立5个结点"<<endl<<endl;
cout<<"选择创建方法:"<<endl;
cout<<"1 : 尾插_顺序建立"<<endl<<"2 : 头插_逆序建立"<<endl<<endl;
cout<<"你选择的是方法:" ;
cin>>option;
cout<<endl<<"输入你的数据:"<<endl;
if(option==1)
{
for(int i=0;i<5;i++)//顺序建立 (尾插)
{
a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间
cin>>a->n;//对 a 开辟出的结构体进行输入
a->next=NULL;//a 的结点指向空指针
if(head==NULL)//head只起记录链表头作用,之后不再动 head
head=a;//将第一个结点地址给 head
else
ptr->next=a;//之后 就是 ptr 与 a 之间轮换
ptr=a;
}
cout<<endl;
cout<<"输出链表:"<<endl;
for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表
cout<<ptr->n <<" ";
cout<<endl;
}
if(option==2)//逆序建立
{
for(int i=0;i<5;i++)//逆序建立 (头插)
{
a=(struct p *)malloc(sizeof(struct p));//每次使用都要开辟新的空间
cin>>a->n;//对 a 开辟出的结构体进行输入
a->next =head;
head=a;
}
cout<<endl;
cout<<"输出链表:"<<endl;
for(ptr=head;ptr !=NULL;ptr=ptr->next ) //输出链表
cout<<ptr->n <<" ";
}
int op;
cout<<endl;
cout<<"选择接下来的操作:"<<endl<<"1、删除结点"<<endl<<"2、增加结点"<<endl<<"3、结束操作"<<endl<<endl;
cout<<"输入你的选择:";
cin>>op;
cout<<endl;
if(op==1) delete_(head);
if(op==2) add(head);
if(op==3) return 0;
return 0;
}