【数据结构】实现顺序表(c语言)

前端之家收集整理的这篇文章主要介绍了【数据结构】实现顺序表(c语言)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

文件


#ifndef _SEQLIST_H
#define _SEQLIST_H

#include <stdio.h>

#define INIT_SIZE 8

typedef struct SeqList
{
	int *base;
	size_t size;
	size_t capacity;
}SeqList;

// 要实现的函数
void InitList(SeqList *list);
int isfull(SeqList *list);
int isempty(SeqList *list);

void show_list(SeqList *list);
void tail_insert(SeqList *list,int x);
void head_insert(SeqList *list,int x);
void sort(SeqList *list);
void head_delete(SeqList *list);
void tail_delete(SeqList *list);
void pos_insert(SeqList *list,int x,int pos);
void val_insert(SeqList *list,int x);
void pos_delete(SeqList *list,int pos);
int find(SeqList *list,int x);
void val_delete(SeqList *list,int x);
int length(SeqList *list);
void reverse(SeqList *list);
void clear(SeqList *list);
void destroy(SeqList *list);
void quit_system(SeqList *list,int* x);

#endif



fun:



#include "SeqList.h"

// 定义两个返回值接收判断函数的结果
int fret = isfull;
int eret = isempty;

// 初始化顺序表
void InitList(SeqList *list)
{
	list->size = 0;
	list->capacity = INIT_SIZE;
	list->base = (int*)malloc(sizeof(int)*list->capacity);
}

// 判断顺序表是否满
int isfull(SeqList *list)
{
	if (list->size >= list->capacity)
	{
		return -1;
	}
	else
		return 0;
}

// 判断顺序表是否空
int isempty(SeqList *list)
{
	if (list->size == 0)
	{
		return -1;
	}
	else
		return 0;
}

// 显示
void show_list(SeqList *list)
{
	int i;
	if (list->size == 0)
	{
		printf("the table is empty!\n");
	}
	for (i = 0; i < list->size; ++i)
	{
		printf("%d  ",list->base[i]);
	}
	printf("\n");
}

// 尾插
void tail_insert(SeqList *list,int x)
{
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	list->base[list->size] = x;
	list->size++;
}

// 头插
void head_insert(SeqList *list,int x)
{
	int i;
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	for (i = list->size; i > 0; --i)
	{
		list->base[i] = list->base[i - 1];
	}
	list->base[0] = x;
	list->size++;
}

// 排序
void sort(SeqList *list)
{
	int i;
	int j;
	int temp;
	for (i = 1; i < list->size; ++i)
	{
		for (j = 0; j < list->size - i; ++j)
		{
			if (list->base[j]>list->base[j + 1])
			{
				temp = list->base[j];
				list->base[j] = list->base[j + 1];
				list->base[j + 1] = temp;
			}
		}
	}
}

// 头删
void head_delete(SeqList *list)
{
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	for (i = 0; i < list->size; ++i)
	{
		list->base[i] = list->base[i + 1];
	}
	list->size--;
}

// 尾删
void tail_delete(SeqList *list)
{
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	list->size--;
}

// 按位插入
void pos_insert(SeqList *list,int pos)
{
	int i;
	if (fret == -1)
	{
		printf("the table is full,can not insert!\n");
		return;
	}
	if (pos<0 || pos>list->size)
	{
		printf("the position is illegal!\n");
		return;
	}
	else
	{
		for (i = list->size; i > pos; --i)
		{
			list->base[i] = list->base[i - 1];
		}
		list->base[pos] = x;
		list->size++;
	}
}

// 按值插入
void val_insert(SeqList *list,can not insert!\n");
		return;
	}
	tail_insert(list,x);
	sort(list);
}

// 按位删除
void pos_delete(SeqList *list,int pos)
{
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	if (pos<0 || pos>list->size)
	{
		printf("the position is illegal!\n");
		return;
	}
	else
	{
		for (i = pos; i < list->size; ++i)
		{
			list->base[i] = list->base[i + 1];
		}
		list->size--;
	}
}

// 查找
int find(SeqList *list,int x)
{
	int i;
	for (i = 0; i < list->size; ++i)
	{
		if (list->base[i] == x)
		{
			return i;
		}
	}
	printf("the number is not exit!\n");
	return 0;
}

// 按值删除
void val_delete(SeqList *list,int x)
{
	int ret = find(list,x);
	int i;
	if (eret == -1)
	{
		printf("the table is empty,can not delete!\n");
		return;
	}
	if (ret == -1)
	{
		printf("the number is not exist!\n");
		return;
	}
	else
	{
		for (i = ret; i <list->size; ++i)
		{
			list->base[i] = list->base[i + 1];
		}
		list->size--;
	}
}

// 求长度
int length(SeqList *list)
{
	return list->size;
}

// 反转
void reverse(SeqList *list)
{
	int temp;
	int i = 0;
	int j = list->size - 1;
	if (eret == -1)
	{
		printf("the table is empty,can not operate!\n");
		return;
	}
	while (i < j)
	{
		temp = list->base[i];
		list->base[i] = list->base[j];
		list->base[j] = temp;
		++i;
		--j;
	}
}

// 清空
void clear(SeqList *list)
{
	list->size = 0;
}

// 摧毁
void destroy(SeqList *list)
{
	list->base = NULL;
}

// 退出系统
void quit_system(SeqList *list,int *x)
{
	*x = 0;
}


函数



// c实现顺序表

#include "SeqList.h"

int main()
{
	SeqList mylist;
	InitList(&mylist);
	int input = 1;
	int insert = 0;
	int pos = 0;
	while (input)
	{
		printf("*********************************************************************\n");
		printf("*     [1] show_list                       [2] tail_insert           *\n"); 
		printf("*     [3] head_insert                     [4] sort                  *\n"); 
		printf("*     [5] head_delete                     [6] tail_delete           *\n"); 
		printf("*     [7] pos_insert                      [8] val_insert            *\n"); 
		printf("*     [9] pos_delete                      [10] find                 *\n"); 
		printf("*     [11] val_delete                     [12] length               *\n"); 
		printf("*     [13] reverse                        [14] clear                *\n"); 
		printf("*     [15] destroy                        [16] quit_system          *\n"); 
		printf("*********************************************************************\n"); 
		printf("please enter your choose:");
		scanf_s("%d",&input);
		switch (input)
		{
			case 1:
				show_list(&mylist);
				break;
			case 2:
				printf("please enter the number want to insert:\n");
				while (scanf_s("%d",&insert),insert != -1)
				{
					tail_insert(&mylist,insert);
				}
				break;
			case 3:
				printf("please enter the number want to insert:\n");
				while (scanf_s("%d",insert != -1)
				{
					head_insert(&mylist,insert);
				}
				break;
			case 4:
				sort(&mylist);
				break;
			case 5:
				head_delete(&mylist);
				break;
			case 6:
				tail_delete(&mylist);
				break;
			case 7:
				printf("please enter the number want to insert:\n");
				scanf_s("%d",&insert);
				printf("please enter the position :\n");
				scanf_s("%d",&pos);
				pos_insert(&mylist,insert,pos);
				break;
			case 8:
				printf("please enter the number want to insert:\n");
				scanf_s("%d",&insert);
				val_insert(&mylist,insert);
				break;
			case 9:
				printf("please enter the position :\n");
				scanf_s("%d",&pos);
				pos_delete(&mylist,pos);
				break;
			case 10:
				printf("please enter the number want to find:\n");
				scanf_s("%d",&insert);
				printf("at the %d\n",find(&mylist,insert));
				break;
			case 11:
				printf("please enter the number want to delete:\n");
				scanf_s("%d",&insert);
				val_delete(&mylist,insert);
				break;
			case 12:
				printf("the table's length is %d\n",length(&mylist));
				break;
			case 13:
				reverse(&mylist);
				break;
			case 14:
				clear(&mylist);
				break;
			case 15:
				destroy(&mylist);
				break;
			case 16:
				quit_system(&mylist,&input);
				break;
			default:
				break;
		}
	}
	return 0;
}

显示




尾插:




头插:




排序:




头删:




尾删:




定位插入:




按值插入:




按位删除




查找:




按值删除




求长度:




反转:




清除:




退出系统:


原文链接:https://www.f2er.com/datastructure/382664.html

猜你在找的数据结构相关文章