顺序表用动态数组实现

Posted 两片空白

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了顺序表用动态数组实现相关的知识,希望对你有一定的参考价值。

实现功能

typedef int ElementType;//定义数据类型,方便维护

typedef struct Node{
	ElementType *data;//数组
	int size;//数组元素个数
	int capacity;//数组大小
}seqlist;

void Seqlistinit(seqlist *list);//初始化
void Seqlistpushfront(seqlist *list,int x);//头插
void Seqlistpopfront(seqlist *list);//头删
void Seqlistpushback(seqlist *list, int x);//尾插
void Seqlistpopback(seqlist *list);//尾删
void Seqlistinsert(seqlist *list, int pos, int x);//任意位置插
int Seqlistfind(seqlist *list, int x);//查找
void Seqlistdelete(seqlist *list, int pos);//任意位置删
void Seqprint(seqlist *list);//打印
void Seqlistsort(seqlist *list);//排序

代码:

#pragma once

#include<stdio.h>
#include<Windows.h>
#include<stdlib.h>
#include<assert.h>
#pragma warning(disable:4996)

typedef int ElementType;//定义数据类型,方便维护

typedef struct Node{
	ElementType *data;//数组
	int size;//数组元素个数
	int capacity;//数组大小
}seqlist;

void Seqlistinit(seqlist *list);//初始化
void Seqlistpushfront(seqlist *list,int x);//头插
void Seqlistpopfront(seqlist *list);//头删
void Seqlistpushback(seqlist *list, int x);//尾插
void Seqlistpopback(seqlist *list);//尾删
void Seqlistinsert(seqlist *list, int pos, int x);//任意位置插
int Seqlistfind(seqlist *list, int x);//查找
void Seqlistdelete(seqlist *list, int pos);//任意位置删
void Seqprint(seqlist *list);//打印
void Seqlistsort(seqlist *list);//排序


#include"sqelist.h"

int main(){
	seqlist list;//也可以定义指针,初始化时返回一个地址,就不用传地址了
	Seqlistinit(&list);//传地址,指针接受,形参改变,实参也变。
	Seqlistpushfront(&list, 6);
	Seqlistpushfront(&list, 5);
	Seqlistpushfront(&list, 4);
	Seqlistpushfront(&list, 3);
	Seqlistpushfront(&list, 2);
	Seqlistpushfront(&list, 1);
	//Seqlistpopfront(&list);
	//Seqlistpopfront(&list);
	//Seqlistpopfront(&list);
	Seqlistpushback(&list, 7);
	Seqlistpushback(&list, 8);
	//Seqlistpopback(&list);
	Seqlistinsert(&list, 5, 9);
	Seqlistinsert(&list, 1, 2);
	Seqprint(&list);
	Seqlistsort(&list);
	//Seqlistdelete(&list, 2);
	//int pos = Seqlistfind(&list, 5);
	//printf("%d\\n",pos);
	Seqprint(&list);
	system("pause");
	return 0;
}



#include"sqelist.h"

static void Seqcheckadd(seqlist *list){//当size大于capacity,扩容
	assert(list);
	if (list->size >= list->capacity){
		list->data = realloc(list->data, sizeof(ElementType)*list->capacity * 2);
		list->capacity *= 2;
	}
}
static void Seqchecksub(seqlist *list){//当空间小于需要空间的一半时,缩小空间
	assert(list);
	if (list->size < (list->capacity / 2)){
		list->data = realloc(list->data, sizeof(ElementType)*list->capacity / 2);
		list->capacity /= 2;
	}
}

void Seqlistinit(seqlist *list){//初始化
	assert(list);
	list->data = (ElementType *)malloc(sizeof(ElementType)* 4);
	if (list->data == NULL){
		printf("malloc erro\\n");
		exit(-1);
	}
	list->capacity = 4;
	list->size = 0;
}

//将第一位后面的数据往后移一位,在将数据放到开始
void Seqlistpushfront(seqlist *list, int x){//头插
	Seqcheckadd(list);
	for (int i = 0; i < list->size; i++){
		list->data[list->size - i] = list->data[list->size - 1 - i];//还要减i。
	}
	list->data[0] = x;
	list->size++;
	
}
//将第一位前面的数据向前移一位
void Seqlistpopfront(seqlist *list){//头删
	assert(list);
	for (int i = 0; i < list->size; i++){
		list->data[i] = list->data[i + 1];
	}
	list->size--;
	Seqchecksub(list);

}
//直接将数据放后面,注意size++;
void Seqlistpushback(seqlist *list, int x){//尾插
	assert(list);
	Seqcheckadd(list);
	list->data[list->size] = x;
	list->size++;
}
//直接size--
void Seqlistpopback(seqlist *list){//尾删
	assert(list);
	list->size--;
	Seqchecksub(list);
}
//按照头插尾插原理,在要插位置后面数据往后移一位
void Seqlistinsert(seqlist *list, int pos, int x){//随便在哪插
	assert(list);
	Seqcheckadd(list);
	for (int i = 0; i < list->size - pos + 1; i++){
		list->data[list->size - i] = list->data[list->size - 1 - i];//还要减i。
	}
	list->data[pos - 1] = x;
	list->size++;
}
//按照头删尾插原理,在要插位置前面数据往前移一位
void Seqlistdelete(seqlist *list, int pos){
	assert(list);
	for (int i = 0; i < list->size - pos; i++){
		list->data[pos - 1 + i] = list->data[pos + i];//还要加i
	}
	list->size--;
	Seqchecksub(list);

}
//找数据
int Seqlistfind(seqlist *list, int x){
	assert(list);
	int i = 0;
	for (i = 0; i < list->size; i++){
		if (list->data[i] == x){//找到
			break;
		}
	}
	if (i < list->size){//返回位置
		return i + 1;
	}
	printf("不存在\\n");//没找到
	return -1;
	
}

void Seqlistsort(seqlist *list){//冒泡排序
	assert(list);
	for (int i = 0; i < list->size; i++){
		int flag = 0;
		for (int j = 0; j < list->size - 1 - i; j++){
			if (list->data[j] < list->data[j + 1]){
				int temp = list->data[j];
				list->data[j] = list->data[j + 1];
				list->data[j + 1] = temp;
				flag = 1;
			}

		}
		if (flag == 0){
			break;
		}
	}
}

void Seqprint(seqlist *list){
	assert(list);
	for (int i = 0; i < list->size; i++){
		printf("%d ", list->data[i]);
	}
	printf("\\n");
}

以上是关于顺序表用动态数组实现的主要内容,如果未能解决你的问题,请参考以下文章

动态内表用ALV输出时能不能再定义每个FIELD的颜色呢?

scrapy按顺序启动多个爬虫代码片段(python3)

在android中动态创建选项卡并使用传入的参数加载片段

❤❤❤❤顺序表的实现(c语言)----数据结构

❤❤❤❤顺序表的实现(c语言)----数据结构

❤❤❤❤顺序表的实现(c语言)----数据结构