线性表的顺序存储实验程序
2015-10-10 00:37
405 查看
先声明一下,我是菜鸟,学习数据结构,鼓捣了一天,用c语言写出这么一个东西,欢迎批评指正。
也给自己留第一个脚印。
也给自己留第一个脚印。
//***************************************************** //线性表顺序存储结构实验程序 //2015.10.9 //***************************************************** #include <stdio.h> #include <stdlib.h> #include <string.h> #define MAXSIZE 100 #define FILE_PATH "d:\\data.txt" typedef struct { int num; char name[10]; char pos[20]; }DataType; DataType something[MAXSIZE]; void Inition();//初始化顺序表,置空 int Readfile();//从文件读取数据(可选) int Inputdata();//键盘输入数据(可选) int length();//求表长 void Display(int length);//显示表 void Show(int n);//取出第n个元素 void Insert(int n);//在第n个位置插入一个元素 void Delete(int n);//删除第n个元素 int main() { int ch = 0,len = 0;//ch:选择项,len:表长 printf_s("\tSequence List Test Program\n"); printf_s("=======================================================\n"); Inition(); printf_s("How do you Creat a list? \n"); printf_s("if you will input data from keyboard press 1 \n"); printf_s(" and read from file press 2:\n"); do{ scanf_s("%d",&ch); if (ch == 1){len= Inputdata();break;} else if (ch == 2){len = Readfile();break;} else {printf_s("No such choice!choice again...\n");} }while (ch !=1 && ch !=2); len = length(); Display(len); ch = -1; do{ printf_s("=======================================================\n"); printf_s("Select what do you do?\n"); printf_s("1 Set the sequence list empty\n"); printf_s("2 Query the data number of the sequence list\n"); printf_s("3 Judge if the sequence list is empty\n"); printf_s("4 Show a data of the Sequence List\n"); printf_s("5 Insert a data from the Sequence List\n"); printf_s("6 Delete a data from the Sequence List\n"); printf_s("0 Exit\n"); scanf_s("%d",&ch); switch (ch) { case 1: { Inition(); break; } case 2: { printf("the sequence list has %d data.\n",length()); break; } case 3: { if (length()>0) printf("the sequence list is not empty,it has %d data.\n",length()); else printf("the sequence list is empty.\n"); break; } case 4: { int n = 0; printf("Which data would you to show? No. is "); scanf_s("%d",&n); Show(n); break; } case 5: { int n = 0; printf("Where would you to insert a data? No. is "); scanf_s("%d",&n); Insert(n); len = length(); Display(len); break; } case 6: { int n = 0; printf("Which data would you to delete? No. is "); scanf_s("%d",&n); Delete(n); len = length(); Display(len); break; } default: { printf("No such choice!choice again...\n"); break; } } }while(ch != 0); return 0; } void Inition() { int i = 0; for(i = 0;i<MAXSIZE;i++) { something[i].num = 0; strcpy_s(something[i].name,""); strcpy_s(something[i].pos,""); } printf_s("Inition sucessful!The sequence list now is empty.\n"); printf_s("Press any key to continue.\n"); getchar(); } int length() { int i, len; for (i = 0;i<MAXSIZE;i++) { if (something[i].name[0] == '\0') break; } len = i; return len; } void Display(int length) { int i = 0; printf_s("num\t\tname\t\t\t\tposition\n" ); printf_s("============================================================\n" ); for(i = 0;i<length;i++) { printf_s("%d\t\t",something[i].num); printf_s("%s\t\t\t\t",something[i].name ); printf_s("%s\n",something[i].pos ); } printf_s("OK,press any key to continue!\n"); getchar(); } int Inputdata() { int i = 0; int _num ; char _name[10] , _pos[20] ; for(i = 0;i<MAXSIZE;i++) { printf_s("Plesase input the num of seqlist No. %d\n",i+1); scanf_s("%d",&_num,2); if (_num == -1) break; printf_s("Plesase input the name of seqlist No. %d\n",i+1); scanf_s("%s",_name,9); printf_s("Plesase input the position of seqlist No. %d\n",i+1); scanf_s("%s",_pos,19); something[i].num = _num; strcpy_s(something[i].name,_name); strcpy_s(something[i].pos,_pos); } printf_s("OK,press any key to continue!\n"); getchar(); return i; } int Readfile() { int i = 0; int _num ; char _name[10] , _pos[20] ; FILE *fp; if (fopen_s(&fp,FILE_PATH,"r")) { printf_s("File not exist or open error!\n"); return 0; } for(i = 0;i<MAXSIZE;i++) { fscanf_s(fp,"%d",&_num,2); if (_num == -1) break; fscanf_s(fp,"%s",_name,9); fscanf_s(fp,"%s",_pos,19); something[i].num = _num; strcpy_s(something[i].name,_name); strcpy_s(something[i].pos,_pos); } fclose(fp); printf_s("Read file sucessful!Press any key to continue\n"); getchar(); return i; } void Show(int n) { if(n>length()||n<=0) printf("No such data!\n"); else printf("num=%d,name=%s,position=%s\n",something[n-1].num,something[n-1].name,something[n-1].pos); } void Insert(int n) { int i,len=length(); int _num ; char _name[10] , _pos[20] ; if(n>len||n<=0) printf("No such data!\n"); else { printf_s("Plesase input the num of data:\n"); scanf_s("%d",&_num,2); printf_s("Plesase input the name of of data:\n"); scanf_s("%s",_name,9); printf_s("Plesase input the position of of data:\n"); scanf_s("%s",_pos,19); for (i=len;i>=n;i--) { printf("%d",i); something[i].num=something[i-1].num ; strcpy_s(something[i].name,something[i-1].name); strcpy_s(something[i].pos,something[i-1].pos); } something[i].num = _num; strcpy_s(something[i].name,_name); strcpy_s(something[i].pos,_pos); } } void Delete(int n) { int i,len=length(); if(n>len||n<=0) printf("No such data!\n"); else { for (i=n-1;i<len-1;i++) { something[i].num=something[i+1].num ; strcpy_s(something[i].name,something[i+1].name); strcpy_s(something[i].pos,something[i+1].pos); } something[i].num=0 ; strcpy_s(something[i].name,""); strcpy_s(something[i].pos,""); } }
相关文章推荐
- sizeof用法的总结
- 其实你应该这样学C++(1)
- C++交换两个数的值
- c++基础i
- 头文件 string.h cstring string 区别
- C++函数间数据传递方式
- C++函数间数据传递方式
- 头文件 string.h cstring string 区别
- cout运算顺序(←)
- c++/c中流的概念
- 文件读写操作
- c++基础知识整理二
- C++基础知识学习整理一
- 一道360面试题引发的思考
- Up and Running with C++
- CPP-Calling constructors(调用构造函数)
- 单向链表创建(一)
- 复数运算类
- 类型转换构造函数
- 《零起点学通C++》 系列笔记 -- 第15章 链表