为了账号安全,请及时绑定邮箱和手机立即绑定

“这道是线性表,怎么增加删除,按值查询等等,有基于源代码”

“这道是线性表,怎么增加删除,按值查询等等,有基于源代码”

erutdioup8556 2015-11-23 15:57:56
/* COPYRIGHT (C) 2013 BY MULDINI. ALL RIGHTS RESERVED. VERSION: 1.0 AUTHOR: Paolo Weng DATE: 2013-09-01 DESCRIPTION: 一个菜单,根据用户的选择实现对线性表的求表长、取表元、插入和退出等操作。 */ #include <stdio.h> #include <stdlib.h> #include <conio.h> #include <windef.h> #define ListSize 10  //假定表容量为10  typedef int DataType; //假定DataType的类型为int型 typedef struct {     DataType data[ListSize]; //向量data用于存放表结点     int length;  //当前表的长度(结点数) } SeqList; //返回顺序表L的表长 int getLength(SeqList *listPointer)    {     return listPointer->length; } //返回顺序表L的第i个结点的值 DataType getElementBySeqNo(SeqList *listPointer, int i)    {     return listPointer->data[i - 1]; } //将t插入顺序表的第i个结点的位置上 int insertElement(SeqList *listPointer, DataType t, int i)    {     if (i < 1 || i > listPointer->length + 1) {         puts("插入位置错");         return FALSE;     }     if (listPointer->length >= ListSize) {         puts("表满不能插入");         return FALSE;     }     for (int j = listPointer->length - 1; j >= i - 1; j--)         listPointer->data[j + 1] = listPointer->data[j];  //结点依次后移     listPointer->data[i - 1] = t;  //插入t     listPointer->length++;  //表长加1     return TRUE; } //显示顺序表 void show(SeqList *listPointer)    {     for (int j = 0; j < getLength(listPointer); j++)         printf("%5d", listPointer->data[j]);     printf("\n"); } //从顺序表中查找值为t的结点,找到返回位置值i,否则返回-1 int getSeqNoByValue(SeqList *listPointer, DataType t)    {     int i = 1;     while (i <= listPointer->length && listPointer->data[i - 1] != t)         i++;     if (listPointer->data[i - 1] == t)         return i;     else         return -1; } //菜单显示 void showMenu()    {     printf("\n\t\t\t   线性表的操作\n");     printf("\n\t\t1.求表长\t\t2.取表元");     printf("\n\t\t3.插入\t\t\t4.退出系统");     printf("\n\n"); } //根据用户选择,决定所做的操作 void executeMenuOption(SeqList *listPointer, char ch)    {     int i, x;     switch (ch) {     case '1':         printf("表长为:%d\n", getLength(listPointer));         break;     case '2':         printf("请输入取第几个元素:");         scanf("%d", &i);         if (i < 1 || i > listPointer->length)             puts("输入错!");         else             printf("第%d个元素为%d\n", i, getElementBySeqNo(listPointer, i));         break;     case '3':         printf("请输入要插入元素的位置和数值:");         scanf("%d%d", &i, &x);         printf("原线性表:\n");         show(listPointer);         if (insertElement(listPointer, x, i)) {             printf("插入后的线性表:\n");             show(listPointer);         } else      printf("插入失败。\n");         break;     case '4':         printf("欢迎再次使用,再见!\n");         exit(EXIT_SUCCESS);     } } int main() {     SeqList aList = {{12, 35, 25, 17, 49, 8, 20}, 7};          while (TRUE) {         system("cls"); // 清空屏幕          showMenu();         printf("请根据菜单选择所需的操作:");         char ch = getchar();         // _flushall(); // 清空缓存         executeMenuOption(&aList, ch); // 执行对应的菜单选项          printf("按任意键继续!");         getch();         _flushall(); // 清空缓存     }     return EXIT_SUCCESS; }
查看完整描述

1 回答

?
erutdioup8556

TA贡献41条经验 获得超27个赞

没有人会吗?

查看完整回答
反对 回复 2015-11-23
  • 1 回答
  • 1 关注
  • 1576 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信