C语言:线性表的基本操作(数组/链表)

输入:

第一行为一个整数 m (m <= 10000)

后输入 m 行,每行描述一个命令(插入 / 删除 / 查找 / 统计 / 去重 / 集体删除)

(命令格式:

1 i x 表示调用函数 insert(i, x),将x插入到 a[i];

2 i 表示调用函数 DeleteByIndex(i),删除 a[i];

3 x 表示调用函数 Find(x),找到第一个 ai = x,若找到则输出 i;若 x 不在线性表中,则输出0;

4 x y 表示调用函数 Count(x, y),统计表中 [x, y] 范围内的元素个数 t,并输出;

5 表示调用函数 EliminateRepeat(),删除表中重复出现的元素;

6 x y 表示调用函数 DeleteByRange(x, y),从表中删除所有 [x, y] 范围内的数。

通过数组实现:

#include <stdio.h>

const int maxn = 10000;

int m;  //命令数 
int n;  //线性表中元素个数 
int arr[maxn];

/*const char * inputfilename =  "9.in";
const char * outputfilename = "9.out";
FILE * fin = fopen(inputfilename, "r");
FILE * fout = fopen(outputfilename, "w"); */

void insert(int i, int x){//插入x到a[i] 
	for (int j = n; j >= i; j--)
		arr[j] = arr[j - 1];
	arr[i - 1] = x;
	n++;
} 

void deleteByIndex(int i){ //删除a[i] 
	for (int j = i; j < n; j++)
		arr[j - 1] = arr[j];
	n--;
} 

void find(int x){//查找第一个x 
	for (int j = 0; j < n; j++)
		if (arr[j] == x){
			printf("%d\n", j + 1);
			return;
		}
	printf("0\n");
}

void count(int x, int y){//统计[x,y]中元素个数	
	int k = 0;
	for (int j = 0; j < n; j++)
		if (arr[j] >= x && arr[j] <= y)
			k++;
	printf("%d\n", k);
}

void deleteByRange(int x, int y){ //去除[x,y]范围内的元素 
	int k = 0;
	for (int j = 0; j < n; j++)
		if (arr[j] < x || arr[j] > y)
			arr[k++] = arr[j];
	n = k;
} 

void eliminateRepeat(){ //去除重复元素 
	int i = 0;
	while (i < n){
		int k = i + 1;
		for (int j = i + 1; j < n; j++)
			if (arr[j] != arr[i])
				arr[k++] = arr[j];
		n = k; i++;
	}
}

int main() {
	scanf("%d", &m);
	for (int k = 0; k < m; k++){
		int c, i, x, y;
		scanf("%d", &c);
		switch (c){
			case 1: scanf("%d%d", &i, &x); insert(i, x); break;
			case 2: scanf("%d", &i); deleteByIndex(i); break;
			case 3: scanf("%d", &x); find(x); break;
			case 4: scanf("%d%d", &x, &y); count(x, y); break;
			case 5: eliminateRepeat(); break;
			case 6: scanf("%d%d", &x, &y); deleteByRange(x, y); break;
		}
	}
	/*fclose(fin); fclose(fout);*/
	return 0;
}

通过链表实现:

#include <stdio.h>
#include <stdlib.h>

int m;  //命令数 

struct node{
	int data;
	struct node *next;
};
node *head; // head指向头节点 

void insert(int i, int x){//插入x到a[i] 
	node *p = head;
	while (i > 1){
		p = p->next; i--;
	} 
	node *q = (node*) malloc(sizeof(node));
	q->data = x; q->next = p->next; p->next = q;	
} 

void deleteByIndex(int i){ //删除a[i] 
	node *p = head;
	node *q = head->next;
	while (i > 1){
		p = q; q = p->next; i--;
	}
	p->next = q->next;
	free(q);
} 


void find(int x){//查找第一个x 
	node *p = head->next; 
	int j = 1;
	while (p != NULL && p->data != x){
		p = p->next; j++;
	}
	if (p == NULL) j = 0;
	printf("%d\n", j);
}

void count(int x, int y){//统计[x,y]中元素个数	
	int k = 0;
	node *p = head->next; 
	while (p != NULL){
		if (p->data >= x && p->data <= y) k++;
		p = p->next;
	}
	printf("%d\n", k);
}

void deleteByRange(int x, int y){ //去除[x,y]范围内的元素 
	node *p = head;
	node *q = head-> next;
	while (q != NULL){
		if (q->data >= x && q->data <= y){
			p->next = q->next; free(q); q = p->next;
		}
		else{
			p = q; q = p->next;
		}
	}
} 

void eliminateRepeat(){ //去除重复元素 
	node *r = head->next, *p, *q;
	while (r != NULL){
		p = r; q = r->next;
		while (q != NULL){
			if (q->data == r->data){
				p->next = q->next; free(q);	q = p->next;
			}
			else{
				p = q; q = p->next;
			}
		}
		r = r->next;
	}
}

int main() {
	scanf("%d", &m);
	head = (node*) malloc(sizeof(node));
	head->next = NULL;
	for (int k = 0; k < m; k++){
		int c, i, x, y;
		scanf("%d", &c);
		switch (c){
			case 1: scanf("%d%d", &i, &x); insert(i, x); break;
			case 2: scanf("%d", &i); deleteByIndex(i); break;
			case 3: scanf("%d", &x); find(x); break;
			case 4: scanf("%d%d", &x, &y); count(x, y); break;
			case 5: eliminateRepeat(); break;
			case 6: scanf("%d%d", &x, &y); deleteByRange(x, y); break;
		}
	}
	//fclose(fin); fclose(fout);
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值