单链表的实现(带头结点/不带头结点)

带头结点

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

//单链表结点的定义 
typedef struct LNode{
	int data;
	struct LNode *next;	
}LNode,*LinkedList;

//单链表的初始化
bool InitList(LinkedList &L){
	L=(LNode *)malloc(sizeof(LNode));
	if(L==NULL) return false;
	L->next=NULL;
	return true;
} 
//头插法建立单链表
LinkedList InsertHead(LinkedList &L){
	LNode *s;int x;
	L=(LNode *)malloc(sizeof(LNode));
	L->next=NULL;
	cout<<"请输入要插入的数据(9999为退出):"<<endl;
	scanf("%d",&x);
	while(x!=9999){
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		s->next=L->next;
		L->next=s;
		cout<<"请输入要插入的数据(9999为退出):"<<endl;
		scanf("%d",&x);
	}
	return L;	
}
//尾插法建立单链表
LinkedList InsertTail(LinkedList &L){
	LNode *s;int x;
	L=(LNode *)malloc(sizeof(LNode));
	L->next=NULL;
	LinkedList p=L;
	cout<<"请输入要插入的数据(9999为退出):"<<endl;
	scanf("%d",&x);
	while(x!=9999){
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		p->next=s;
		p=s;
		cout<<"请输入要插入的数据(9999为退出):"<<endl;
		scanf("%d",&x);	
	}
	p->next=NULL;
	return L;
}
//判断单链表是否为空
bool isEmpty(LinkedList &L){
	return L==NULL; 
}
//按照位序插入
bool ListInsert(LinkedList &L,int i,int e){
	if(i<1) return false;
	LNode *p=L;
	int j=0;
	while(p!=NULL&&j<i-1){
		p=p->next;
		j++;
	}
//	1 2 3 4
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
//指定节点的后插操作
bool InsertNextNode(LNode *p,int e){
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true; 
}
//指定节点的前插操作
bool InsertBeforeNode(LNode *p,int e){
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	//改变p数据的值 
	s->data=p->data;
	p->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
//按位序删除
bool ListDelete(LinkedList &L,int i,int &e){
	if(i<1) return false;
	LinkedList p=L;
	int j=0;
	while(p!=NULL&&j<i-1){
		p=p->next;
		j++;
	}
	if(p==NULL) return false;
	if(p->next==NULL) return false;
	e=p->next->data;
	p->next=p->next->next;
	return true;
}
//指定节点的删除
bool DeleteNode(LinkedList &L,LNode *p){
	if(p==NULL) return false;
	if(p->next!=NULL){
		p->data=p->next->data;
		p->next=p->next->next;
		return true;
	}
	LinkedList s=L;
	while(s->next->next!=NULL){
		s=s->next;
	}
	s->next=NULL; 
}
//按值查找
LNode * LocateElem(LinkedList &L,int e){
	LNode *p=L->next;
	while(p!=NULL&&p->data!=e) p=p->next;
	return p;
}
//输出链表
void print(LinkedList L){
	LinkedList p=L;
	cout<<"链表:";
	while(p!=NULL){
		if(p->next==NULL){
			cout<<p->data;
		}
		else{
			cout<<p->data<<"->"; 
		}
		p=p->next;
	}
}

 不带头结点

#include<stdio.h>
#include<stdlib.h>
#include<iostream>
using namespace std;

//单链表结点的定义 
typedef struct LNode{
	int data;
	struct LNode *next;	
}LNode,*LinkedList;

//单链表的初始化
bool InitList(LinkedList &L){
	L=NULL;
	return true;
} 
//头插法建立单链表
LinkedList InsertHead(LinkedList &L){
	LNode *s;int x;
	L=NULL;
	cout<<"请输入要插入的数据(9999为退出):"<<endl;
	scanf("%d",&x);
	while(x!=9999){
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		s->next=NULL;
		LNode * l=L;
		L=s;
		L->next=l;
		cout<<"请输入要插入的数据(9999为退出):"<<endl;
		scanf("%d",&x);
	}
	return L;	
}
//尾插法建立单链表
LinkedList InsertTail(LinkedList &L){
	LNode *s;int x;
	L=NULL;
	cout<<"请输入要插入的数据(9999为退出):"<<endl;
	scanf("%d",&x);
	LinkedList p; 
	int flag=0;
	while(x!=9999){
		if(flag==0){
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		s->next=NULL;
		L=s;
		p=L;
		flag=1;
		cout<<"请输入要插入的数据(9999为退出):"<<endl;
		scanf("%d",&x);	
		continue;
		}
		s=(LNode *)malloc(sizeof(LNode));
		s->data=x;
		s->next=NULL;
		p->next=s;
		p=s;
		cout<<"请输入要插入的数据(9999为退出):"<<endl;
		scanf("%d",&x);	
	}
	p->next=NULL;
	return L;
}
//判断单链表是否为空
bool isEmpty(LinkedList &L){
	return L==NULL; 
}
//按照位序插入
bool ListInsert(LinkedList &L,int i,int e){
	if(i<1) return false;
	if(i==1){//插到第一个 
		LNode *s=(LNode *)malloc(sizeof(LNode));
		if(s==NULL) return false;
		s->data=e;
		s->next=L;
		L=s;
		LinkedList p=L;
		return true;
	}
	LNode *p=L;
	int j=0;
	while(p!=NULL&&j<i-2){
		p=p->next;
		j++;
	}
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
//指定节点的后插操作
bool InsertNextNode(LNode *p,int e){
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	s->data=e;
	s->next=p->next;
	p->next=s;
	return true; 
}
//指定节点的前插操作
bool InsertBeforeNode(LNode *p,int e){
	if(p==NULL) return false;
	LNode* s=(LNode *)malloc(sizeof(LNode));
	if(s==NULL) return false;
	//改变p数据的值
	s->data=p->data;
	p->data=e;
	s->next=p->next;
	p->next=s;
	return true;
}
//按位序删除
bool ListDelete(LinkedList &L,int i,int &e){
	if(i<1) return false;
	if(L==NULL) return false;
	if(i==1){
		e=L->data;
		L=L->next;
		return true;
	}
	LinkedList p=L;
	int j=0;
	while(p!=NULL&&j<i-2){
		p=p->next;
		j++;
	}
	if(p==NULL) return false;
	if(p->next==NULL) return false;
	e=p->next->data;
	p->next=p->next->next;
	return true;
}
//指定节点的删除
bool DeleteNode(LinkedList &L,LNode *p){
	if(p==NULL) return false;
	if(p->next!=NULL){
		p->data=p->next->data;
		p->next=p->next->next;
		return true;
	}
	LinkedList s=L;
	while(s->next->next!=NULL){
		s=s->next;
	}
	s->next=NULL; 
}
//按值查找
LNode * LocateElem(LinkedList &L,int e){
	LNode *p=L->next;
	while(p!=NULL&&p->data!=e) p=p->next;
	return p;
}
//输出链表 
void print(LinkedList L){
	LinkedList p=L;
	cout<<"链表:";
	while(p!=NULL){
		if(p->next==NULL){
			cout<<p->data;
		}
		else{
			cout<<p->data<<"->"; 
		}
		p=p->next;
	}
}

 总结:推荐使用带头结点的链表实现,用起来比较方便

如果感觉文章写的不错,可以点个小小的赞哦~

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值