数据结构实验一:顺序表
2015-09-14 15:26
423 查看
代码实现了顺序表的一些常规操作,如下。(代码中调用了自定义的table.h,后面有)
/******************************************************* Copyright (c) 2015 Jingshuang Hu @filename:demo.c @datetime:2015.09.14 @author:HJS @e-mail:eleftheria@163.com @blog:http://blog.csdn.net/hujingshuang *******************************************************/ /******************************************************/ #include "table.h" /***********************主程序*************************/ int main() { int num = 0; SqList p; //创建线性表 List_Init(&p); //初始化 while(1) { List_Menu(); //菜单栏 scanf("%d", &num); if (List_Response(&p, num)) //响应 { printf("\n程序结束!\n"); break; } getchar();getchar(); system("cls"); //清屏 } return 0; }其中table.h中定义了有关顺序表的结构体及函数声明,代码如下:
/****************************************************** Copyright (c) 2015 Jingshuang Hu @filename:table.h @datetime:2015.09.14 @author:HJS @e-mail:eleftheria@163.com @blog:http://blog.csdn.net/hujingshuang *******************************************************/ #ifndef _TABLE_H_ #define _TABLE_H_ #include <stdio.h> #include <stdlib.h> /************************定义**************************/ #define Max_Length 100 typedef struct { int data[Max_Length]; int length; }SqList; /***********************函数声明***********************/ void List_Menu(void); //菜单 int List_Response(SqList *p, int num); //选择操作 void List_Init(SqList *p); //初始化 void List_Insert(SqList *p, int pos, int value); //插入 void List_Delete_Pos(SqList *p, int pos); //删除 void List_Delete_Elem(SqList *p, int value); //.. void List_Replace_Pos(SqList *p, int pos, int value); //替换 void List_Replace_Elem(SqList *p, int pre_value, int value); //.. void List_Find_Pos(SqList p, int pos); //查找 void List_Find_Elem(SqList p, int value); //.. void List_Show_All(SqList p); //显示 void List_Show_Length(SqList p); //.. void List_Clear(SqList *p); //清空 #endiftable.c是table.h中函数的具体实现,代码如下:
/********************************************************* Copyright (c) 2015 Jingshuang Hu @filename:table.c @datetime:2015.09.14 @author:HJS @e-mail:eleftheria@163.com @blog:http://blog.csdn.net/hujingshuang *********************************************************/ #include "table.h" /************************菜单****************************/ void List_Menu(void) { printf("\n************顺序表菜单************\n"); printf("1.插入元素\n"); printf("2.按位置删除\t3.按元素删除\n"); printf("4.按位置替换\t5.按元素替换\n"); printf("6.按位置查找\t7.按元素查找\n"); printf("8.查看表长\t9.清空表\n"); printf("10.退出程序\n"); printf("**********************************\n"); printf("请选择:"); } /**********************操作响应**************************/ int List_Response(SqList *p, int num) { int pos = 0, pre_value = 0, value = 0; int res = 0; switch(num) { case 1: printf("插入位置:"); scanf("%d", &pos); printf("插入值:"); scanf("%d", &value); List_Insert(p, pos, value); break;//插入元素 case 2: printf("删除位置:"); scanf("%d", &pos); List_Delete_Pos(p, pos); break;//按位置删除 case 3: printf("删除元素:"); scanf("%d", &value); List_Delete_Elem(p, value); break;//按元素删除 case 4: printf("替换的位置:"); scanf("%d", &pos); printf("替换为:"); scanf("%d", &value); List_Replace_Pos(p, pos, value); break;//按位置替换 case 5: printf("替换的元素:"); scanf("%d", &pre_value); printf("替换为:"); scanf("%d", &value); List_Replace_Elem(p, pre_value, value); break;//按元素替换 case 6: printf("查找的位置:"); scanf("%d", &pos); List_Find_Pos(*p, pos); break;//按位置查找 case 7: printf("查找的元素:"); scanf("%d", &value); List_Find_Elem(*p, value); break;//按元素查找 case 8: List_Show_Length(*p); break;//查看表长 case 9: List_Clear(p); break;//清空表 case 10: res = 1; break;//退出程序 } List_Show_All(*p); return res; } /************************初始化**************************/ void List_Init(SqList *p) { p->length = 0;//初始时线性表中未添加元素 } /*********************插入元素**************************/ void List_Insert(SqList *p, int pos, int value) { int i = 0; if (p->length >= Max_Length) { printf("List is full, can't insert!\n"); } else if (pos < 1 || pos > p->length + 1) { printf("Position is illegal!\n"); } else { if (p->length <= 0) { p->data[pos - 1] = value; } else { for (i = p->length; i >= pos; i--) { p->data[i] = p->data[i - 1]; } p->data[i] = value; } p->length += 1; } } /*************************删除*************************/ void List_Delete_Pos(SqList *p, int pos) { int i = 0; if (p->length <= 0) { printf("List is empty, can't delete!\n"); } else if (pos < 1 || pos > p->length) { printf("位置不合法!\n"); } else { //value = p->data[pos - 1];删除位置的元素 for (i = pos; i < p->length; i++) { p->data[i - 1] = p->data[i]; } p->length -= 1; } } /*************************删除*************************/ void List_Delete_Elem(SqList *p, int value) { int i = 0; for (i = 0; i < p->length; i++) { if (value == p->data[i]) { List_Delete_Pos(p, i + 1); i--;//移动后的当前位置 } } } /*************************替换**************************/ void List_Replace_Pos(SqList *p, int pos, int value) { if (pos < 1 || pos > p->length) { printf("位置不合法!\n"); } else { printf("已将位置%d上的元素%d替换成%d\n", pos, p->data[pos - 1], value); p->data[pos - 1] = value; } } /*************************替换**************************/ void List_Replace_Elem(SqList *p, int pre_value, int value) { int i = 0; for (i = 0; i < p->length; i++) { if (p->data[i] == pre_value) { p->data[i] = value; } } printf("已将表中所有的元素%d替换为%d\n", pre_value, value); } /************************查找***************************/ void List_Find_Pos(SqList p, int pos) { if (pos < 1 || pos > p.length) { printf("位置不合法!\n"); } else { printf("%d\n", p.data[pos - 1]); } } /************************按元素查找***************************/ void List_Find_Elem(SqList p, int value) { int i = 0; for (i = 0; i < p.length; i++) { if (p.data[i] == value) { printf("%d ", i + 1); } } printf("\n"); } /************************显示************************/ void List_Show_All(SqList p) { int i = 0; if (p.length <= 0) { printf("表空!\n"); } else { for (i = 0; i < p.length; i++) { printf("%d ", p.data[i]); } printf("\n"); } } /************************显示************************/ void List_Show_Length(SqList p) { printf("表长:%d\n", p.length); } /***********************清空************************/ void List_Clear(SqList *p) { p->length = 0; }上述代码完好,均可copy使用。
相关文章推荐
- 栈的基本操作-数据结构
- 数据结构之栈的链式存储7-(入栈,出栈等)
- 【前端也要学点数据结构】 神奇的树状数组
- 数据结构之栈的顺序存储6-(入栈,出栈等)
- Machine Learning On Spark——第二节:基础数据结构(二)
- 将合并的数据结构来实现一个单一的列表
- KMP算法
- 数据结构 前言
- 软考视频A总结
- 数据结构学习2--线性表的设计与实现(二)
- MySQL索引背后的数据结构及算法原理
- C++数据结构之List--线性实现
- Machine Learning On Spark——第一节:基础数据结构(一)
- 数据结构之---C语言实现银行模拟(离散化)
- 树不同的遍历方式及使用的不同数据结构
- 数据结构之——选择排序
- 数据结构之---C语言实现括号匹配(栈实现)
- 数据结构——二叉树
- 数据结构—Java版链表相交问题的终极解决方案
- 数据结构学习2--线性表的设计与实现(一)