软基作业——顺序线性表的插入
来源:互联网 发布:淘宝店铺怎么绑定旺旺 编辑:程序博客网 时间:2024/06/08 11:14
题目如下:设线性表存放在向量A[1..MAXNUM]的前elenum个分量中,且递增有序。试写一算法,将x插入到线性表的适当位置上,以保持线性表的有序性。
我的程序如下:
1. test.c
#include "insert.h"#include <stdio.h>int main(void){ listtype list; int i; float elenum; /* Initialize the list.*/ list.elenum = 0; for (i = 0; i < 20; i++) { list.A[i] = i + 1; list.elenum++; printf("(%d):%f\t", i + 1, list.A[i]); } printf("\nThe length of the list:%d\n\n", list.elenum); /* To test the insert function.*/ insert(&list, 0); insert(&list, 6.5); insert(&list, 50); for (i = 0; i < 23; i++) printf("(%d):%f\t", i + 1, list.A[i]); printf("\nThe length of the list:%d\n\n", list.elenum); return 0;}
2.insert.h
/* avoid redefine.*/#ifndef INSERT_H#define INSERT_H/* C99 only*/#include <stdbool.h>#define MAXNUM 100/* Define the type of list here. */typedef struct list_type{ float A[MAXNUM]; /* The array.*/ int elenum; /* The num of elements.*/}listtype;/* Only C99 have the bool type. With C89, we can use int.*/bool insert(listtype *list, float element);#endif
3.insert.c
#include "insert.h"#include <stdio.h>bool insert(listtype *list, float element){ int i, j; /* Check whether the list is full.*/ if (list->elenum >= MAXNUM) { printf("The list is full so can't be inserted./n"); return false; /* return a failure result.*/ } /* search the location to insert.*/ for (i = 0; i < list->elenum; i++) if (list->A[i] > element) break; for (j = list->elenum; j > i; j--) list->A[j] = list->A[j-1]; list->A[i] = element; list->elenum ++; return true;}
另附makefile文件如下:
test.exe : test.o insert.o
gcc -o test.exe test.o insert.o
test.o : test.c insert.h
gcc -c test.c
insert.o : insert.c insert.h
gcc -c insert.c
运行效果如下
- 软基作业——顺序线性表的插入
- 顺序线性表的创建插入
- 第2章 线性表——顺序线性表插入和表示学生信息
- 线性表—顺序表-创建线性表的实现
- 数据结构——线性表的插入
- 线性表的添加、删除、插入功能的顺序实现
- 顺序线性表和单链表的插入,删除操作
- 线性表的输入输出插入删除查找(顺序表示)
- python实现线性表顺序存储的插入操作
- 【数据结构】顺序线性表的插入、删除、合并实现
- python实现线性表顺序存储的插入操作
- 线性表的顺序储存结构 初始化、查找、删除、插入
- 数据结构-顺序线性表的初始化,插入,删除
- C语言实现顺序线性表的表示、插入、删除
- 线性表的顺序存储结构——顺序表
- 线性表的顺序表示——顺序表
- 线性表的顺序存储——顺序表
- 线性表的顺序存储——顺序表
- 异常 Exception与RuntimeException(2)
- 安卓手机ROM和RAM的区别
- LightOJ 1004 - Monkey Banana Problem
- 递归算法-----递归算法优化------非递归算法(递推,迭代)----运行时间
- LightOJ 1005 - Rooks
- 软基作业——顺序线性表的插入
- [C++基础]019_指针和引用(int*、int&、int*&、int&*、int**)
- [K/3Cloud] 调用其他界面时通过Session传递对象参数
- aa
- arraylist vector linkedlist
- ASP.NET 网页生命周期&各种事件Event
- 关于channel实现数据的传输
- 适用于连续资源块的数组空闲链表的算法
- 背包问题——解析