#include"stdio.h"
#include"stdlib.h"
typedef struct node{
int e;
struct node *next;
}*NNode;
//初始化
NNode InitList(NNode L){
NNode head=NULL;
head=(NNode)malloc(sizeof(node));
if(!head){
return 0;
}
head->next=NULL;
L=head;
return L;
}
//重置
void ClearList(NNode L){
NNode p=NULL;
if(L->next){
p=L->next;
free(L);
}
}

//判空
bool ListEmpty(NNode L){
if(L->next==NULL)
return true;
else return false;
}
//长度
int ListLength(NNode L){
NNode p=NULL;
int count=0;
p=L;
while(p->next){
count++;
p=p->next;
}
return count;
}
//创建n个节点插入头节点之后
NNode CreatList(NNode L,int n){
NNode p=NULL;
for(int i=0;i<n;i++){
p=(NNode)malloc(sizeof(node));
scanf("%d",&(p->e));
p->next=L->next;
L->next=p;
}
return L;
}
//GetElem获取
int GetElem(NNode L,int n){
NNode p=NULL,q=NULL;
int j=0;
q=L;
if(q->next&&j<n){
j++;
q=q->next;
if(j+1==n)
return (L->e);
}else{
printf("输入错误");
return 0;
}

}
//获取指定元素的位置
int LocateElem(NNode L,int e){
int i=0;
NNode p=NULL;
p=L;
while(p->next&&p->e!=e){
p=p->next;
i++;
}
if(!p->next){
printf("查找失败");
return -1;
}
if(p->e==e)
return i;
}
//输出
void Out(NNode L){
NNode p;
p=L->next;
while(p)
{
printf("%d",p->e);
p=p->next;
printf("n");
}
}
//插入
NNode LisetInsert(NNode L,int e,int n){
NNode p=NULL,q=NULL;
int j=1;
q=L;
if(q->next&&j<n){
j++;
q=q->next;
}else{
printf("输入错误");
return L;
}
p= (NNode)malloc(sizeof(node));
p->e=e;
p->next=q->next;
q->next=p;
return L;
}
//删除指定节点
NNode ListDelete(NNode L,int n){
NNode p=NULL,q=NULL;
int j=0;
q=L;
if(q->next&&j<n-1){
j++;
q=q->next;
}else{
printf("输入错误");
return L;
}

p=q->next;
q->next=p->next;
free(p);
return L;
}
void main(){
int n,m,i,e;
NNode L=NULL;
//printf("是否为空:");
//printf("%d",ListEmpty(L));//

L=InitList(L);//
printf("初始化成功");
//printf("是否为空:");
//printf("%d",ListEmpty(L));//
CreatList(L,5);//
printf("长度为:");
printf("%dn",ListLength(L));
printf("输出为");
Out(L);
printf("是否为空:");
printf("%d",ListEmpty(L));//
ListDelete(L,2);//
Out(L);
printf("长度为:");
printf("%d",ListLength(L));
printf("插入后");
LisetInsert(L,4,2);
Out(L);
printf("定位");
m=LocateElem(L,4);
printf("%d",m);
}

//区分typedef struct和struct的区别

https://www.cnblogs.com/love-zf/p/5054015.html

内容来源于网络如有侵权请私信删除
你还没有登录,请先登录注册
  • 还没有人评论,欢迎说说您的想法!