#include<iostream>
#include<string.h>
#include <fstream>
#include"stdlib.h"
using namespace std;
typedef int ElemType;
struct LNode{
ElemType data;
LNode* next;
};
//1、初始化单链表
void InitList(LNode* &HL){
HL=NULL;//置单链表为空
}
//2、删除单链表的所有结点,使之成为一个空表
void ClearList(LNode*& HL){
LNode *cp;//cp指向待处理的结点
LNode *np;//np指向cp的后继结点
cp=HL;//将表头指针赋给cp
while(cp!=NULL){//遍历单链表,释放每一个结点的存储空间
np=cp->next;//np指向保存下一个结点的地址
delete cp;//删除当前被处理的结点
cp=np;//使下一个结点成为当前结点
}
HL==NULL;//置单链表为空
}
//3、得到单链表的长度(依次遍历,计数)
int LenthList(LNode* HL){
int i=0;
while(HL!=NULL){
i++;
HL=HL->next;
}
return i;
}
//4、检查单链表是否为空
bool EmptyList(LNode* HL){
return HL==NULL;
}
//5、得到单链表中第pos结点中的元素
ElemType GetList(LNode* HL,int pos){
if(pos<1){
cerr<<" pos is out range!"<<endl;//pos值有错,退出运行!
exit(1);
}
int i;
while(HL!=NULL){//遍历到第pos个结点或者表为空时结束
i++;
if(i==pos) break;
HL->next;
}
if(HL!=NULL)
return HL->data;
else{
cerr<<" pos is out range!"<<endl;
exit(1);
}
}
//6、遍历一个单链表
void TraverseList(LNode* HL){
while(HL!=NULL){
cout<<HL->data<<" ";
HL=HL->next;
}
cout<<endl;
}
//7、从单链表中查找出等于给定值的第一个元素
bool FindList(LNode*HL,ElemType&item){
while(HL!=NULL)
if(HL->data=item){
item=HL->data;
return true;
}
else HL=HL->next;//p2指向后继结点
return false;
}
//8、更新单链表中等于给定值的第一个元素
bool UpdateList(LNode* HL,const ElemType& item){
while(HL!=NULL)
if (HL->data==item) break;//执行break有两种情况当p==null时,p->data==item时
else HL=HL->next;
if(HL==NULL) return false; //判断如果是从 p==null跳出,则返回假
else{
HL->data=item;
return true;
}
}
//9、向单链表中按给条件插入一个元素
bool InsertList(LNode* &HL,ElemType item,int pos){
if(pos<-1){
cout<<" pos值无效!"<<endl;
return false;
}
//为item元素建立新结点
LNode* newptr;
newptr=new LNode;
newptr->data=item;
//寻找新结点的插入位置
LNode* cp=HL;//cp指向当前结点(待查结点),初始指向表头
LNode* ap=NULL;//ap指向cp的前驱节点,初始为空
if(pos==0){
while(cp!=NULL){
if(item<cp->data) break;//找到新结点的插入位置,退出循环
else{
ap=cp;
cp=cp->next;//指针后移,实现顺序向后比较
}
}
}
else if(pos=-1)//查找表尾位置
while(cp!=NULL){
ap=cp;
cp=cp->next;
}
else{//按序号pos的值寻找插入位置
int i=0;
while(cp!=NULL){
i++;
if(i==pos) break;//找到新元素的插入位置,退出循环
else{
ap=cp;
cp=cp->next;
}
}
if(cp==NULL&&i+1<pos){
cout<<" pos的值超出单链表长度加1!"<<endl;
return false;
}
}
//完成新结点的插入操作
if(ap==NULL){//把新结点插入到表头
newptr->next=HL;
HL=newptr;
}
else{
//把新结点插入到非表头位置,即插入到ap和cp之间
newptr->next=cp;
ap->next=newptr;
}
return true;
}
//10、从单链表中删除符合给定条件的第一个元素
bool DeleteList(LNode* &HL,ElemType& item,int pos){
if(HL==NULL){
cerr<<" 单链表为空,删除操作无效!"<<endl;
return false;
}
if(pos<-1){
cout<<" pos值无效!"<<endl;
return false;
}
//寻找被删除的元素结点
LNode* cp=HL;//cp指向当前结点(待查结点),初始指向表头
LNode* ap=NULL;//ap指向cp的前驱节点,初始为空
if(pos==0){
while(cp!=NULL){
if(item==cp->data) break;//找到被删除结点cp,退出循环
else{
ap=cp;
cp=cp->next;//指针后移,实现顺序向后比较
}
}
if(cp==NULL){
cout<<" 单链表中没有相应的结点可删除!"<<endl;
return false;
}
}
else if(pos==-1)//查找表尾位置
while(cp->next!=NULL){
ap=cp;
cp=cp->next;
}
else{//按序号pos的值寻找插入位置
int i=0;
while(cp!=NULL){
i++;
if(i==pos) break;//找到被删除结点cp,退出循环
else{
ap=cp;
cp=cp->next;
}
}
if(cp==NULL){
cout<<" pos值无效!"<<endl;
}
}
//删除cp所指向的结点
if(ap==NULL) HL=HL->next;
else ap->next=cp->next;
delete cp;
return true;
}
//11、对单链表进行数据排序
void SortList(LNode* &HL){
//建立一个反映排序结果的新单链表并初始化为空
LNode* SL;
InitList(SL);
//从待排序的HL单链表中依次取出每一个结点,按值插入到新单链表中
LNode* r=HL;//r指向取出待排序的结点,初始为HL表头结点
while(r!=NULL){
//为新插入的r结点在SL中顺序查找出插入位置
LNode* t=r->next;//t指向r结点的后继结点
LNode* cp=SL;//用cp初始指向SL单链表的表头
LNode* ap=NULL; //用ap指向cp的前驱节点,初始为空
while(cp!=NULL){
if(r->data<cp->data) break;
else{
ap=cp;
cp=cp->next;
}
}
//实现插入操作
if(ap==NULL){//把r结点插入到表头
r->next=SL;
SL=r;
}
else{//把r结点插入到ap和cp之间
r->next=cp;//cp可能为空,则r成为SL的表尾
ap->next=r;
}
//使r指向原单链表的下一个结点
r=t;
}
//由引用参数带回新单链表的表头指针
HL=SL;
}
int main()
{
int a[12] = {3,6,9,12,15,18,21,24,27,30,33,36};
int i ;
ElemType x ;
LNode* t;
InitList(t);
for(i = 0 ; i<12 ; i++)
InsertList(t,a[i],i+1);
cout<<GetList(t,4)<<endl;
TraverseList(t);
cout<<" 输入待查找的元素值:";
cin>>x;
if(FindList(t,x))
cout<<" 查找成功!"<<endl;
else cout<<" 查找失败!"<<endl;
cout<<" 输入待删除元素的值:";
cin>>x;
if(DeleteList(t,x,0))
cout<<" 删除成功!"<<endl;
else cout<<" 删除失败!"<<endl;
TraverseList(t);
cout<<" 按值插入,输入待插入元素的值:";
cin>>x;
if(InsertList(t,x,0))
cout<<" 插入成功!"<<endl;
else cout<<" 插入失败!"<<endl;
TraverseList(t);
cout<<" 单链表长度:"<<LenthList(t)<<endl;
if(EmptyList(t))
cout<<" 单链表为空!"<<endl;
else cout<<" 单链表不空!"<<endl;
ClearList(t);
}
【C++数据结构程序笔记】线性表操作在单链表上的实现
最新推荐文章于 2022-12-05 20:54:57 发布