链式线性表数据结构

#include<stdio.h>
#include <malloc.h>

typedef struct LNode* PtrNode;
struct LNode {
	int data;
	PtrNode next;
};
typedef PtrNode list;
list read()//头插法创建链表
{
	int n;
	list head, tmp, cur;
	head = cur = (list)malloc(sizeof(struct LNode));
	cur->next = NULL;
	scanf_s("%d", &n);
	while (n--) {
		tmp = (list)malloc(sizeof(struct LNode));
		scanf_s("%d", &tmp->data);
		tmp->next = NULL;
		cur->next = tmp;
		cur = tmp;
	}
	return head;
}
int len(list L)
{
	list head;
	int count = 0;
	head = L;
	while (head) {
		head = head->next;
		count++;
	}
	return count;
}
int find(list L, int location)
{
	int count = 0;
	list head;
	head = L;
	while (head && count < location)
	{
		head = head->next;
		count++;
	}
	if (count == location)
	{
		printf_s("the index is %d", head->data);
	}
	return 0;
}
list insert(list L, int x, int location)
{
	list tmp, pre;
	int count = 0;
	pre = L;
	while (pre && count < location )//location从0开始
	{
		pre = pre->next;
		count++;
	}
	if (pre == NULL || count != location)
	{
		printf_s("ERROR");
	}
	else
	{
		tmp = (list)malloc(sizeof(struct LNode));
		tmp->data = x;
		tmp->next = pre->next;//去除头结点影响
		pre->next = tmp;
		return pre;
	}
}
list Delete(list L, int i)
{
	list tmp, pre;
	int count=0;
	pre = L;
	while (pre && count < i)
	{
		pre = pre->next;
		count++;
	}

	if (pre->next == NULL||count!=i||pre==NULL)
	{
		printf_s("ERROR");
	}
	else
	{
		tmp = pre->next;
		pre->next = tmp->next;
		free(tmp);
		return pre;
	}

}
void Print(list L)
{
	while (L)
	{
		printf_s("%d ", L->data);
		L = L->next;
	}
}
int main()
{
	list L;
	L = read();
	//find(L, 2);
	//printf_s("%d", len(L));
	insert(L, 666, 0);
	Delete(L, 1);
	printf_s("\n");
	Print(L);
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值