线性表作业

#define MAX 1000
#include<stdio.h>
typedef struct ArrayNode
{
	int len;
	int array[MAX];
}ArrayNode, * pArray;
//
pArray create2() {
	pArray p = (ArrayNode*)malloc(sizeof(ArrayNode));
	p->len = 0;
	return p;
}
//
void add2(int val, pArray array) {

	array->array[array->len] = val;
	array->len++;
}
//
void insert2(int index, int val, pArray array) {
	if (index > array->len || index < 0)
	{
		printf_s("error\n");
		return;
	}
	array->array[index] = val;
}
//
int getByIndex2(int index, pArray array) {
	if (index > array->len || index < 0)
	{
		printf_s("error\n");
		return -250;
	}
	return array->array[index];
}
//
void tr2(pArray array) {
	for (int i = 0; i < array->len; i++) {
		printf_s("%d ", array->array[i]);
	}
	printf_s("\n");
}
//
void removeByIndex2(int index, pArray array) {
	if (index > array->len || index < 0)
	{
		printf_s("error\n");
		return;
	}
	for (int i = index; i < array->len; i++) {
		array->array[i] = array->array[i + 1];
	}
	array->len--;
}
//
void updateByIndex2(int index, int val, pArray array) {
	if (index > array->len || index < 0)
	{
		printf_s("error\n");
		return;
	}
	array->array[index] = val;
}
//
void clear2(pArray array) {
	array->len = 0;
}

int main() {
	pArray a = create2();
	for (int i = 0; i < 5; i++) {
		add2(i, a);
	}
	tr2(a);
	insert2(2, 99, a);
	tr2(a);
	removeByIndex2(2, a);
	tr2(a);
	clear2(a);
	tr2(a);
	return 0;
}
#include<stdio.h>
#include<stdbool.h>
typedef struct Node
{
	int val;
	struct Node* next;
}Node, *pNode;

pNode create(int val) {
	pNode p = (Node*)malloc(sizeof(Node));
	p->val = val;
	p->next = NULL;
	return p;
}

pNode addToLast(int val,pNode tail) {
	pNode p=create(val);
	tail->next = p;
	return p;
}

pNode addToFirst(int val, pNode head) {
	pNode p = create(val);
	p->next = head;
	head = p;
	return p;
}
void tr(pNode p) {
	pNode q = p;
	while(q){
		printf_s("%d ", q->val);
		q = q->next;
	}
	printf_s("\n");
}

void removeNode(int index,pNode head) {
	pNode p = head;
	for (int i = 1; i < index; i++) {
		p = p->next;
		if (p==NULL)
		{
			printf_s("超出长度");
			return;
		}
	}
	p->next = p->next->next;
}
void insertNode(int index, int val, pNode head) {
	pNode p = head;
	if (index<=0)
	{
		addToFirst(val, head);
		return;
	}
	for (int i = 1; i < index; i++) {
		p = p->next;
		if (p == NULL)
		{
			printf_s("超出长度");
			return;
		}
	}
	pNode n = p->next;
	pNode c = create(val);
	p->next = c;
	c->next = n;
}

void update(int index, int val, pNode head) {
	pNode p = head;
	for (int i = 1; i < index; i++) {
		p = p->next;
		if (p == NULL)
		{
			printf_s("超出长度");
			return;
		}
	}
	p->val = val;
}

pNode getByIndex(int index,pNode head) {
	pNode p = head;
	for (int i = 1; i < index; i++) {
		p = p->next;
		if (p == NULL)
		{
			printf_s("超出长度");
			return NULL;
		}
	}
	return p;
}

//int main() {
//	pNode head = create(1);
//	pNode tail = head;
//	tail = addToLast(1, tail);
//	tail = addToLast(2, tail);
//	tail = addToLast(3, tail);
//	tail = addToLast(4, tail);
//	tail = addToLast(5, tail);
//	head = addToFirst(99, head);
//	tr(head);
//	removeNode(1, head);
//	tr(head);
//	insertNode(5, 99, head);
//	tr(head);
//	update(5, 88, head);
//	tr(head);
//	printf_s("%d", getByIndex(5, head)->val);
//	return 0;
//}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值