数据结构:顺序表的操作
2015-04-06 00:26
288 查看
// 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);;//定义顺序表L init_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; else return false; } bool is_empty_list(seqlist * L) { if(L->valid_len == 0) return true; else return 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; }
相关文章推荐
- 数据结构 顺序栈基本操作
- c语言版数据结构(奇迹冬瓜)-串(定长顺序存储各项操作)
- 数据结构_动态数组形式的顺序表相关操作C语言源代码
- 数据结构:顺序表的基本操作
- 【数据结构】线性表顺序结构的操作---C/C++语言
- 数据结构--顺序表的操作
- 【数据结构】用C++实现顺序表的各种操作(包括头删,尾删,插入,逆序,摧毁,清空等等)
- hrbustoj 1545:基础数据结构——顺序表(2)(数据结构,顺序表的实现及基本操作,入门题)
- C语言版数据结构中顺序表的基本操作定义和初始化
- 数据结构上机测试2-1:单链表操作A (顺序建表+关键字删除)
- 数据结构学习(一)——顺序表的操作
- 数据结构之 顺序栈的操作
- 数据结构--顺序栈的基本操作
- 数据结构 顺序表的操作(源代码)
- 数据结构 顺序栈的基本操作
- 顺序表的简单操作(数据结构)
- 数据结构--顺序队列基本操作
- 进击的数据结构之六队列的基本操作-顺序结构
- 数据结构_顺序栈相关操作
- 算法数据结构 单链表的实现+操作 以及和顺序表的对比