我是靠谱客的博主 眯眯眼乌龟,最近开发中收集的这篇文章主要介绍循环单链表-删除元素,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

#include<stdio.h>
#include<stdlib.h>
typedef int ElemType;
typedef struct LNode {
	ElemType data;
	struct LNode *next;
}LNode,*LinkList;

LinkList listTailInsert(LinkList &L)
{
	ElemType x;
	LNode *s,*r;
	L = (LinkList)malloc(sizeof(LNode));
	r = L;
	scanf("%d",&x);
	while(x!=9999)
	{
		s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		r->next = s;
		r = s;
		scanf("%d",&x);
	}
	r->next = L;
	return L;
}

//循环单链表删除第i位元素,并存储到x中
bool deleteElem(LinkList &L,int i,ElemType &x)
{
	LNode *p = L;
	LNode *s;
	int j = 0;
	while(p && j<i-1)
	{
		p = p->next;
		++j;
	}
	if(p && j==i-1)
	{
		s = p->next;
		p->next = s->next;
		x = s->data;
		free(s);
		return true;
	}
	else
		return false;
}

void printList(LinkList L)
{
	LNode *p = L->next;
	while(p!=L)
	{
		printf("%d ",p->data);
		p = p->next;
	}
	printf("n");
}

void main()
{
	LinkList L;
	ElemType x;
	listTailInsert(L);
	printList(L);
	printf("删除第2位元素,并将其保存到x中:n");
	deleteElem(L,2,x);
	printList(L);
	printf("x = %dn",x);
}	

最后

以上就是眯眯眼乌龟为你收集整理的循环单链表-删除元素的全部内容,希望文章能够帮你解决循环单链表-删除元素所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(84)

评论列表共有 0 条评论

立即
投稿
返回
顶部