顺序表的增删改查

#include<stdio.h>
#include<stdlib.h>
#define MAXSIZE 20
typedef int ElemType;
typedef struct {
	int *array;//malloc的返回值为地址 
	int length;
}SqList;

int ListInit(SqList *L){
	L->array = (int*)malloc(MAXSIZE * sizeof(int));//创建数组,函数返回值为地址 
	if(L->array == 0){
		return 0;
	}
	L->length = 0;
	return 1;
} 

int ListInsert(SqList *L, int i, int e){
	int k;
	if(i < 1 || i > L->length + 1){
		return 0;
	}
	if(L->length >= MAXSIZE){
		return 0;
	}
	if(i <= L->length){
		for(k = L->length - 1; k >= i - 1; k--){
			L->array[k + 1] = L->array[k];
		}
	}
	L->array[i - 1] = e;
	L->length++;
	//printf("L->array[%d] = %d\n", i, L->array[i]);
	return 1;
}

int ListGet(SqList L, int i, int *e){
	if(i < 1 || i > L.length){
		return 0;
	}
	*e = L.array[i - 1];
	//printf("e = %d", *e);
	return 1;
}

int ListDelete(SqList *L, int i, int *e){
	int k;
	if(i < 1 || i > L->length){
		return 0;
	}else{
		*e = L->array[i - 1];
		for(k = i; k <= L->length - 1; k++){
			L->array[k - 1] = L->array[k];
		}
		L->length--;
		return 1;
	}
	
}

int main()
{
	SqList L;	
	int i;
	int e;
	//初始化链表 
	if(ListInit(&L)){
		printf("ListInit succeed!\n");
	}else{
		printf("ListInit failed!\n");
	}
	//插入一个结点 
	printf("insert num and i in the List(end of i -1) :\n");
	while(1){
		scanf("%d%d", &e, &i);
		if(i == -1){
			printf("INSERT END\n\n");
			break;
		} 	
		if(ListInsert(&L,i, e)){
			printf("ListInsert succeed!\n");
		}else{
			printf("ListInsert failed!\n");
		}		
	}
	//根据下标查找节点
	printf("请问需要获得链表中的第几个元素?");
	scanf("%d", &i); 
	if(ListGet(L, i, &e)){
		printf("ListGet succeed!\n");
		printf("第%d个链表的值为array[%d] = %d\n\n", i, i - 1, e);
	}else{
		printf("ListGet failed!\n");
	}
	//删除一个节点
	printf("请问要删除链表中的第几个元素?");
	scanf("%d", &i);
	if(ListDelete(&L, i, &e)){
		printf("ListDelete succeed!\n");
		printf("被删除的节点的值为%d", e);
	}else{
		printf("ListDelete failed!\n");
	}
	
}

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
下面是一个简单的在 C# Winform 窗体中实现顺序表增删改查的实例: 1. 首先,在你的 Winform 窗体中添加四个 TextBox 控件和四个 Button 控件,分别用于输入元素、输入要删除的元素位置、输入要修改的元素位置和输入新值,以及按钮事件。 2. 接下来,在窗体的 Load 事件中,声明一个 List<int> 类型的变量来存储顺序表中的元素: ``` List<int> array = new List<int>(); ``` 3. 实现插入元素的方法。在 button1_Click 事件中,通过 TextBox 控件获取要插入的元素值,并调用 List 类型的 Add 方法将元素插入到顺序表中: ``` private void button1_Click(object sender, EventArgs e) { int value = int.Parse(textBox1.Text); array.Add(value); // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 4. 实现删除元素的方法。在 button2_Click 事件中,通过 TextBox 控件获取要删除的元素位置,然后调用 List 类型的 RemoveAt 方法删除指定位置的元素: ``` private void button2_Click(object sender, EventArgs e) { int index = int.Parse(textBox2.Text); array.RemoveAt(index); // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 5. 实现修改元素的方法。在 button3_Click 事件中,通过 TextBox 控件获取要修改的元素位置和新值,然后调用 List 类型的 indexer 语法修改指定位置的元素: ``` private void button3_Click(object sender, EventArgs e) { int index = int.Parse(textBox3.Text); int value = int.Parse(textBox4.Text); array[index] = value; // 更新 DataGridView 控件中的数据 dataGridView1.DataSource = null; dataGridView1.DataSource = array; } ``` 6. 实现查找元素的方法。在 button4_Click 事件中,通过 TextBox 控件获取要查找的元素值,然后调用 List 类型的 FindIndex 方法查找元素的位置: ``` private void button4_Click(object sender, EventArgs e) { int value = int.Parse(textBox5.Text); int index = array.FindIndex(x => x == value); if(index >= 0) { MessageBox.Show($"元素 {value} 的位置是:{index}"); } else { MessageBox.Show($"元素 {value} 不存在!"); } } ``` 以上就是一个简单的在 C# Winform 窗体中实现顺序表增删改查的实例。你可以将这些方法放在一个类中,并在 Winform 中调用这个类来操作顺序表
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值