带头结点的单链表实现

带头结点的单链表实现

/*
	带头结点的单链表实现:
	通过对结点的指针操作来实现各个核心功能. 
	代码中要注重核心思想,但是必要的安全检查还是必须的
	后期可以省略,但是要有这个意识 
*/

#include <stdio.h>
#include <stdlib.h>

#define null NULL
typedef int ElemType;

typedef struct LNode{//定义结点 
	ElemType data;//数据域
	struct LNode *next;//指针域用于存放指针. 
} node,*LinkList;

//初始化操作 
bool InitList(LinkList *L){
	*L=(node*)malloc(sizeof(node));
	if(*L==null){
		return false;
	}
	(*L)->data=null;
	(*L)->next=null;
	return true;
}

//判空 
bool IsEmpty(LinkList L){
	if(L->next=null){
		return true;
	}else{
		return false;
	}
} 


//头插法建表
bool CreateFromHead(LinkList L){
	node *tem;//结点
	int flag=1;//标志 
	ElemType ch;//数据 
	while(flag){
		scanf("%d",&ch);
		if(ch!=9999){
			tem=(node*)malloc(sizeof(node));
			if(tem==null){
				return false;
			}
			tem->data=ch;
			tem->next=L->next;
			L->next=tem;
			return true;	
		} else{
			flag=0;
		}
	}	
	return true;
} 

//尾插法建表
bool CreateFromTail(LinkList L){
	node *tem;//结点
	node *p=L;//临时指针 (尾指针)
	int flag=1;//标志 
	ElemType ch;//数据 
	while(flag){
		scanf("%d",&ch);
		if(ch!=9999){
			tem=(node*)malloc(sizeof(node));
			if(tem==null){
				return false;
			}
			tem->data=ch;
			tem->next=p->next;
			p->next=tem;
			p=p->next;//注意尾指针要时刻指向末尾 
		}
		else{
			p->next=null;
			flag=0;
		}
	} 
	return true;
} 


//遍历操作 
bool PrintList(node* L){
	if(L==null){
		printf("error\n");
		return false;
	} 
	node *tem;
	tem=L->next;
	ElemType e;
	while(tem!=null){
		e=tem->data;
		tem=tem->next;
		printf("%d ",e);
	}
	printf("\n");
	return true;
}

//按位查找
bool GetElem(LinkList L,int index,ElemType *e){
	int i=1;
	node *tem=L->next;//临时指针,开始指向第一个节点 
	if(index<1||L==null){
		return false;
	} 
	while(i<index&&tem!=null){
		tem=tem->next;
		i++;	
	}
	if(i==index){
		*e=tem->data;
		return true;
	}
	else{
		*e=-1;
		return false;
	}
}

//按值查找 
bool LocalElem(LinkList L,int *index,ElemType e){
	node *tem=L->next;
	if(L==null){
		return false;
	}
	int i=1;
	while(tem->data!=e&&tem!=null){
		tem=tem->next;
		i++;
	}
	if(tem->data==e){
		*index=i;
		return true;
	}else{
		*index=-1;
		return false;
	}
}


//插入操作,在index的位置插入元素. 
bool InsertList(LinkList L,int index,ElemType e){
	int i=0;//计数器,这里从零开始考虑到在1的位置插入元素情况. 
	node *tem=L;//临时指针
	node *p;//节点 
	if(L==null){
		return false;
	}
	while(i<index-1&&tem!=null){
		tem=tem->next;
		i++;
	}
	if(tem==null){
		return false;
	}
	p=(node*)malloc(sizeof(node));
	if(p==null){
		return false;
	}
	p->data=e;
	p->next=tem->next;
	tem->next=p;
	return true;
} 

//删除操作
bool DelList(LinkList L,int index,ElemType *e){
	int i=1;
	node *tem=L->next;//指向第一个节点 
	if(L==null){
		return false;
	}	 
	while(i<index-1&&tem==null){
		tem=tem->next;
		i++;
	}
	if(tem==null){
		return false;
	}
	node *p;//临时指针
	p=tem->next;
	tem->next=p->next; 
	*e=p->data;
	return true;
} 

//逆序,头插法 
bool ReverseList(LinkList L){
	node *tem=L->next;//指向第一个结点 
	LinkList NewList;//新的链表 
	if(InitList	(&NewList)){
		node *p; 
		while(tem!=null){
			p=tem; 
			NewList->next=tem;
			tem=tem->next; 
		}
		return true;
	}
	else{
		return false;
	} 
}

//main()
int main(){
	LinkList L;
	InitList(&L);
	if(IsEmpty(L)){
		printf("OK");
	}
	CreateFromTail(L);
	PrintList(L);
	ElemType e;
	//GetElem(L,2,&e);
	LocalElem(L,&e,2);
	printf("%d\n",e);
	InsertList(L,1,99);
	PrintList(L);
	DelList(L,1,&e);
	printf("%d",e);
	return 0;
}
	 
	 
  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值