链表的基本操作

2023-06-18 0 762

注意事项

不带头头插和带头头插
不带头头插插入的每一次头都会改变
带头头插由于有头节点的存在每一次插入不会改变头节点的指针

定义

//结构体定义
typedef struct LNode{
	int data;
	struct LNode *next;
}LNode,*LinkList;
//双链表结构 
typedef struct DNode{
	int data;
	struct DNode *next,*prior;
}DNode,*DLinkList; 

代码

单链表操作

//头插法(带头结点) 
LinkList List_HeadInsert(LinkList &L){
	LNode *s;int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->data = -1;
	L->next = NULL;
	while(true){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
	} 
	return L;
}

//头插法不带头结点
LinkList List_HeadInsertWithoutHNode(LinkList &L){
	LNode *s,*p;int x;
	scanf("%d",&x);
	if(x==-1)return NULL;
	L = (LNode*)malloc(sizeof(LNode));
	L->data = x;
	L->next = NULL;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L;
		L = s;
	}
	return L;
} 

//尾插法带头结点
LinkList List_TailInsert(LinkList &L){
	LNode *s,*p;int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->data = -1;
	p = L;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
	    s->next = NULL;
	    s->data = x;
	    p->next = s;
	    p = s;
	}
	return L;
} 

//尾插法不带头结点
LinkList List_TailInsertWithoutHNode(LinkList &L){
	LNode *s,*p;int x;
	scanf("%d",&x);
	if(x==-1)return NULL;
	L = (LNode*)malloc(sizeof(LNode));
	L->data = x;
	p = L;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = NULL;
		p->next = s;
		p = s;
	}
	return L;
} 

//创建带头结点的循环单链表(头插) 
LinkList create_xhd(LinkList &L){
	LNode *s;int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->next = L;
	L->data = -1;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
	}
	return L;
} 

//创建不带头结点的循环单链表(头插) 
LinkList create_xhdw(LinkList &L){
	LNode *s;int x;
	scanf("%d",&x);
	if(x==-1) return NULL; 
	L = (LNode*)malloc(sizeof(LNode));
	L->next = L;
	L->data = x;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L;
		L = s;
	}
	return L;
} 

//创建带头结点的循环单链表(尾插) 
LinkList create_xhde(LinkList &L){
	LNode *s,*p;int x;
	L = (LNode*)malloc(sizeof(LNode));
	L->data = -1;
	L->next = NULL;
	p = L;
	while(1){
		scanf("%d",&x);
	    if(x == -1)break;
	    s = (LNode*)malloc(sizeof(LNode));
	    s->data = x;
	    s->next = NULL;
	    p->next = s;
	    p = s;
	}
	p->next = L;
	return L;
} 

//创建不带头结点的循环单链表(尾插) 
LinkList create_xhdew(LinkList &L){
	LNode *s,*p;int x;
	scanf("%d",&x);
	if(x==-1) return NULL; 
	L = (LNode*)malloc(sizeof(LNode));
	L->next = L;
	L->data = x;
	p = L;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = NULL;
		p->next = s;
		p=s;
	} 
	p->next = L;
	return L;
}

双链表操作

//创建带头节点的双链表(头插) 
LinkList create_slbdt(DLinkList &L){
	l = (DLNode*)malloc(sizeof(DLNode));
	DNode *s,*l;int x;
	l = L;
	l->data = -1;
	l->prior = NULL;
	l->next = NULL;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (DLNode*)malloc(sizeof(DLNode));
		s->next = l->next;
		s->data = x;
		s->prior = l;
		if(l->next!=NULL){
			l->next->prior = s;
		} 
		l->next = s;
	}
	return L;
} 

//创建不带头节点的双链表(头插) 
void create_slbbdt(DLinkList &L){
	DNode *s,*l;int x;
	scanf("%d",&x);
	if(x==-1)return NULL;
	l = L;
	l = (DLNode*)malloc(sizeof(DLNode));
	l->data = -1;
	l->prior = NULL;
	l->next = NULL;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (DLNode*)malloc(sizeof(DLNode));
		s->data = x;
		s->prior = NULL;
		s->next = l;
		//这步是否多余? 
		l->prior = s;
		l = s;
	}
} 

//创建带头节点的双链表(尾插)  
void create_slbdtw(DLinkList &l){
	l = (DLNode*)malloc(sizeof(DLNode));
	DLNode *s,*p;int x;
	l->data = -1;
	l->prior = NULL;
	l->next = NULL;
	p=l;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (DLNode*)malloc(sizeof(DLNode));
		s->next = NULL;
		s->data = x;
		s->prior = p;
		p->next = s;
		p = s;
	}
} 

//创建不带头节点的双链表(尾插)  
void create_slbbdtw(DLinkList &l){
	DNode *s,*p;int x;
	scanf("%d",&x);
	if(x==-1)return NULL;
	l = (DLNode*)malloc(sizeof(DLNode));
	l->data = -1;
	l->prior = NULL;
	l->next = NULL;
	p=L;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (DLNode*)malloc(sizeof(DLNode));
		s->next = NULL;
		s->data = x;
		s->prior = p;
		p->next = s;
		p = s;
	}
} 

//创建带头节点的循环双链表(尾插)  
void create_slbdtwx(DLinkList &l){
	l = (DLNode*)malloc(sizeof(DLNode));
	DLNode *s,*p;int x;
	l->data = -1;
	l->prior = NULL;
	l->next = NULL;
	p=l;
	while(1){
		scanf("%d",&x);
		if(x==-1)break;
		s = (DLNode*)malloc(sizeof(DLNode));
		s->next = NULL;
		s->data = x;
		s->prior = p;
		p->next = s;
		p = s;
	}
	s->next = l;
	l->prior = s;
} 

打印与插入和删除

//链表打印(单链表且不循环)
void printList(LinkList L){
	while(L){
		printf("%d ",L->data);
		L = L->next;
	}
} 

//双链表打印
void printDList(DLinkList L){
	while(L){
		printf("%d ",L->data);
		L = L->next;
	}
} 

//循环双链表打印
void printXDList(DLinkList L){
	while(L){
		printf("%d ",L->data);
		L = L->next;
		if(L->data == -1)break; 
	}
} 
 
//删除单链表中指定位置的元素(无头节点的单链表)
void deletelinklist(LinkList &L,int i){
	LNode *p=L->next,*a;int k=1;
	while(++k<i-1 && p){ 
		p = p->next;
	}
	a = p->next;
	p->next = p->next->next;
	free(a);
} 
//指定位置插入结点(无头节点的单链表)
bool InsertNode(LinkList &L,LinkList &node,int k){
	LinkList p=L;int i=1;
	while(p && i<k-1){
		p = p->next;
		i++;
	}
	//找不到前一个结点,给定K值超出链长,插入失败 
	if(i!=k-1)return false;
	node->next = p->next;
	p->next = node;
	return true;
} 
收藏 (0) 打赏

感谢您的支持,我会继续努力的!

打开微信扫一扫,即可进行扫码打赏哦,分享从这里开始,精彩与您同在
点赞 (0)

本站部分资源收集自互联网,我站仅分享内容,仅供用户个人研究,不提供商业使用,如有侵权下架处理,觉得资源内容不错请您在官方渠道购买正版,您在阅读的同时下载的内容本站不承担任何法律责任!Some resources of this website are collected from the Internet. We only share the content, which is only for personal research of users, and not for commercial use. If there is infringement and off shelf processing, please purchase the authentic version of the resource content from the official channel if you think it is good. We will not bear any legal responsibility for the content you download while reading!

橙凰素材-精品源码下载|正版主题更新 学习交流 链表的基本操作 https://b.bqzmz.com/2023/06/18/%e9%93%be%e8%a1%a8%e7%9a%84%e5%9f%ba%e6%9c%ac%e6%93%8d%e4%bd%9c/

一名苦逼的程序员

上一篇: 顺序表题目2
下一篇: 链表题目
常见问题
  • 正版主题是指站长在指定渠道购买的官方主题,提供作者联系,资源支持,长期更新等等内容
查看详情
  • 非常抱歉,请联系网站页脚的客服,我们将在核实后下架处理并返还给您此资源的收益(会扣去网站运营的费用),谢谢理解!
查看详情
  • 网络资源是站长从互联网分享收集而来的内容,本站不会做测试,直接分享,不包源码可用性,运营性,如有需求点击右边的联系正版,谢谢!
查看详情
  • 如果资源不是最新版本,请在资源下方评论区进行催更或提交工单处理,我们会尽快更新并通过邮箱通知您。
查看详情

相关文章

发表评论
暂无评论
官方客服团队

为您解决烦忧 - 24小时在线 专业服务