数据结构:顺序表的操作

来源:互联网 发布:ubuntu下载火狐浏览器 编辑:程序博客网 时间:2024/05/16 12:47
// shujujiegou.cpp : 定义控制台应用程序的入口点。///* ============================================================================ Name        : 数据结构1.c Author      : xiaowu Version     : 1.0 Copyright   : Your copyright notice Description : Hello World in C, Ansi-style ============================================================================ *///#include "stdafx.h" 在vs2008环境下的入口函数库#include <stdio.h>#define maxlen 100//数组的最大长度#define bool int#define true 1#define false 0/*定义一个顺序表*/typedef struct seqlist{int array[maxlen];int valid_len;//有效长度(当前表长度)}seqlist;/*the function list*/seqlist * create_null_list(seqlist * L);//建立一个空表seqlist * init_list(seqlist * L);//对空表进行初始化void traverse_list(seqlist * L);//遍历表bool is_full_list(seqlist * L);//判断顺序表是否已满bool is_empty_list(seqlist * L);//判断顺序表是否为空seqlist * insert_element_list(seqlist * L);//插入表中一个元素void delete_element_list(seqlist * L);//删除表中的一个元素//void destroy_list();int get_length_list(seqlist * L);//获取顺序表当前的有效长度int choose_operate_list(void);//选择对表进行什么操作void operate_list(int i, seqlist * L);//对表进行操作的具体函数调用void research_element(seqlist * L);//查找元素的位置(也可以直接返回一个值,然后再获取值)int main(void){/*不调用这个函数,在eclipse环境下无法正常使用printf和scanf*/setbuf(stdout, NULL);int i = 0;seqlist L;create_null_list(&L);;//定义顺序表Linit_list(&L);i = choose_operate_list();operate_list(i, &L);return 0;}/*create a null list*/seqlist * create_null_list(seqlist * L){L->valid_len = 0;printf("success!\n");return L;}/*input the number of the node*/seqlist * init_list(seqlist * L){int i;do{printf("please input how much you want to create the node(0~100): len = ");scanf("%d", &L->valid_len);}while((L->valid_len>100) || (L->valid_len<0));//进行有效判断for (i = 0; i < L->valid_len; ++i){printf("please input the %dth node : ", i+1);scanf("%d", &L->array[i]);}printf("initial success!\n");return L;}bool is_full_list(seqlist * L){if(L->valid_len == maxlen)return true;elsereturn false;}bool is_empty_list(seqlist * L){if(L->valid_len == 0)return true;elsereturn false;}/*traverse the list*/void traverse_list(seqlist * L){int i;if(is_empty_list(L)){printf("数组为空,不能输出!\n");return;}else{printf("The node is : \n");for (i = 0; i < L->valid_len; ++i){printf("the %dth is : %d\n", i+1, L->array[i]);}}return;}/*insert the element*/seqlist * insert_element_list(seqlist * L){int i;int i_len_list;//顺序表的当前长度int i_location, i_element;//要插入元素的位置,和元素printf("please input where you want to insert the element :");scanf("%d", &i_location);printf("please input the element : ");scanf("%d", &i_element);i_len_list = get_length_list(L);if(is_full_list(L)){printf("数组已满,不能插入元素!\n");}else{if((i_location<=0) || (i_location>L->valid_len)){printf("插入元素的位置不在数组范围内!\n");return L;}else{for (i = i_location; i <= L->valid_len; i++){L->array[i_len_list] = L->array[i_len_list-1];//把前面的值赋值给后面,把剩下的那个位置填充为需要插入的元素i_len_list--;}L->array[i_location-1] = i_element;L->valid_len = L->valid_len + 1;printf("insert success!\n");}}return L;}/*delete the element*/void delete_element_list(seqlist * L){int i, i_len_list;//顺序表的当前长度int i_location;//要删除元素的位置printf("please input which element you want to delete (position in the node): \n");scanf("%d", &i_location);i_len_list = get_length_list(L);if(is_empty_list(L)){printf("数组为空,不能继续删除操作!\n");}else{if((i_location<0) || (i_location >L->valid_len)){printf("您要删除的元素不在数组范围内!\n");return;}else{for (i = i_location; i < i_len_list; i++){L->array[i_location-1] = L->array[i_location];i_location++;}L->valid_len = L->valid_len - 1;printf("delete success!\n");}}return;}/*get the length of the array*/int get_length_list(seqlist * L){int i_len_list = L->valid_len;return i_len_list;}/*research the locate of the element in the array*/void research_element(seqlist * L){int i;int i_element_list;//要查找的元素printf("请输入要查找的元素 : \n");scanf("%d", &i_element_list);for (i = 0; i < L->valid_len; ++i){if (L->array[i] == i_element_list){printf("您要查找的元素在第%d个位置。\n", i+1);break;}}if(i == L->valid_len){printf("您要查找的元素不再这个数组内。\n");}}/*choose operate to the list*/int choose_operate_list(void){int i;printf("请输入您要进行的操作:");printf("1:插入节点。");printf("2:删除节点。");printf("3:遍历节点。");printf("4:查找元素。");printf("5:退出。");scanf("%d", &i);printf("\n");return i;}/*operate to the list*//*对顺序表的操作函数的调用*/void operate_list(int i, seqlist * L){switch(i){case 0:break;case 1:insert_element_list(L);break;case 2:delete_element_list(L);break;case 3:traverse_list(L);break;case 4:research_element(L);break;case 5:return;//退出操作default:printf("请输入您要进行的操作:");printf("1:插入节点。");printf("2:删除节点。");printf("3:遍历节点。");printf("4:查找元素。");printf("5:退出。");//退出scanf("%d", &i);}i = choose_operate_list();operate_list(i, L);//进入递归,可能会导致溢出return;}

0 0