链表的操作

没有完全实现,不知道错哪里了,不过指针还是难以理解啊

#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct LNode
{
	ElemType data;
	struct LNode * next;
}LNode,*LinkList;
//初始化链表
int InitList_link(LinkList *head)
{
	*head =(LinkList)malloc(sizeof(LNode));
	if(NULL==*head)
		return -1;
	(*head)->next=NULL;
	return 0;
}
//判空
int ListEmpty_link(LinkList head)
{
	if(NULL==head->next)
		return 1;
	return 0;
}
//尾插法创建单链表
int CreateList_link_tail(LinkList head,int n)
{
	int i;
	ElemType temp;
	LNode *p=NULL,*q=head;
	if(n<=0)
		return -1;
	for(i=1;i<=n;i++)
	{
		p=(LNode *)malloc(sizeof(LNode));
		if(NULL==p)
			return -2;
		printf("please input no. %d data\'s value: \n",i);
		scanf("%d",&temp);
		getchar();
		p->data=temp;
		p->next=NULL;
		q->next=p;
		q=p;
	}
	return 0;
}
//求长运算
int ListLength_link(LinkList head)
{
	int len=0;
	LNode *p=head->next;
	while(NULL!=p)
	{
		len++;
		p=p->next;
	}
	return len;
}
//查找数据元素
int LocateElem_link(LinkList head,ElemType e)
{
	int count =1;
	LNode *p=head->next;
	while(NULL!=p&&p->data!=e)
	{
		p=p->next;
		count++;
	}
	if(NULL==p)
		return 0;
	return count;
}
//基于带头结点的单链表实现插入运算
int ListInsert_link(LinkList head,int i,ElemType e)
{
	int count =0;
	LNode *p=head,*q=NULL;
	while(count<i-1&&NULL!=p)
	{
		p=p->next;
		count++;
	}
	if(count>i-1||NULL==p)
		return -1;
	q=(LNode *)malloc(sizeof(LNode));
	if(NULL==q)
		return -2;
	q->data=e;
	q->next=p->next;
	p->next=q;
	return 0;
}
//删除运算
int ListDelete_link(LinkList head,int i,ElemType *e)
{
	int count=0;
	LNode *p=head,*q=NULL;
	if(1==ListEmpty_link(head))
		return -1;
	while(count<i-1 && NULL!=p->next)
	{
		p=p->next;
		count++;
	}
	if(count>i-1||NULL==p->next)
		return -2;
	q=p->next;
	*e=p->data;
	p->next=q->next;
	free(q);
	return 0;
}

通过调试,结果如下

#include <stdio.h>
#include <stdlib.h>
typedef char ElemType;
typedef struct LNode
{
	ElemType data;
	struct LNode * next;
}LNode,*LinkList;
//初始化链表
int InitList_link(LinkList *head)
{
	*head =(LinkList)malloc(sizeof(LNode));
	if(NULL==*head)
		return -1;
	(*head)->next=NULL;
	return 0;
}
//判空
int ListEmpty_link(LinkList head)
{
	if(NULL==head->next)
		return 1;
	return 0;
}
//尾插法创建单链表
int CreateList_link_tail(LinkList head,int n)
{
	int i;
	ElemType temp;
	LNode *p=NULL,*q=head;
	if(n<=0)
		return -1;
	for(i=1;i<=n;i++)
	{
		p=(LNode *)malloc(sizeof(LNode));
		if(NULL==p)
			return -2;
		printf("please input no. %d data\'s value: \n",i);
		scanf("%c",&temp);
		getchar();
		p->data=temp;
		p->next=NULL;
		q->next=p;
		q=p;
	}
	return 0;
}
//求长运算
int ListLength_link(LinkList head)
{
	int len=0;
	LNode *p=head->next;
	while(NULL!=p)
	{
		len++;
		p=p->next;
	}
	return len;
}
//查找数据元素
int LocateElem_link(LinkList head,ElemType e)
{
	int count =1;
	LNode *p=head->next;
	while(NULL!=p&&p->data!=e)
	{
		p=p->next;
		count++;
	}
	if(NULL==p)
		return 0;
	return count;
}
//基于带头结点的单链表实现插入运算
int ListInsert_link(LinkList head,int i,ElemType e)
{
	int count =0;
	LNode *p=head,*q=NULL;
	while(count<i-1&&NULL!=p)
	{
		p=p->next;
		count++;
	}
	if(count>i-1||NULL==p)
		return -1;
	q=(LNode *)malloc(sizeof(LNode));
	if(NULL==q)
		return -2;
	q->data=e;
	q->next=p->next;
	p->next=q;
	return 0;
}
//删除运算
int ListDelete_link(LinkList head,int i,ElemType *e)
{
	int count=0;
	LNode *p=head,*q=NULL;
	if(1==ListEmpty_link(head))
		return -1;
	while(count<i-1 && NULL!=p->next)
	{
		p=p->next;
		count++;
	}
	if(count>i-1||NULL==p->next)
		return -2;
	q=p->next;
	*e=q->data;
	p->next=q->next;
	free(q);
	return 0;
}
void DispList(LinkList head)
{
	LNode *p=head->next;
	while (NULL!=p)
	{
		printf("%c",p->data);
		p=p->next;
	}
	printf("\n");
}
void DestroyList(LinkList head)
{
	LNode *p=head,*q=p->next;
	while (q!=NULL)
	{
		free(p);
		p=q;
		q=p->next;
	}
	free(p);
}
int main()
{
	int i=0;
	ElemType e;
	LinkList L;
	InitList_link(&L);
	ListInsert_link(L,1,'a');
	ListInsert_link(L,2,'b');
	ListInsert_link(L,3,'c');
	ListInsert_link(L,4,'d');
	ListInsert_link(L,5,'e');
//	CreateList_link_tail(L,5);
//	printf("%d\n",ListLength_link(L));
	DispList(L);
//	ListInsert_link(L,2,'a');
//	ListDelete_link(L,4,&e);
//	printf("%c\n",e);
	printf("%d\n",LocateElem_link(L,'d'));
	DestroyList(L);
	printf("ok!\n");
	return 0;
}

调试成功,问题是%d,和%c的输入问题,还有删除操作时返回的删除元素的值出现偏差,将p改为q即可

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值